简体   繁体   中英

How do I rotate an oval using Graphics2D and AffineTransform around an ever changing origin?

I am making my own version of space invaders. I have a shooter at the bottom of a screen and enemies that are approaching from above. My shooter can move along the x-axis at the bottom of the screen perfectly fine. However I have to give it a turret that rotates about the shooter's center based on keys pressed.

Taking the x-axis as 0 degrees rotation(with a positive angle of rotation towards the positive y-axis):

The turret should start at the top of the shooter's head (ie 90 degrees). If I press A it should keep on rotating to the left(counterclockwise) and if I press D it should keep on rotating right(clockwise). If I press S it should stop rotating. A maximum rotation of 180 degrees is allowed (from positive x-axis to negative x-axis).

With the code I have thus far my turret starts at 0 degrees (pointing in the positive x-direction). If I press A it rotates to 90 degrees (vertical), if I press D it rotates all the way to -90 degrees (vertical downwards). If I press S it stops.

My question is where would I have to make my changes to correct this rotation?

This is how it should start, and it should be able to rotate all the way horizontal to the left and right and stop at either side:

在此处输入图片说明

Thank you for any help!

Edited code:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;

public class Turret{
  private int omega;    //rotation speed
  private int width = 16;
  private int height = 12;
  private int currAngle;
  private Player playa;

  public Turret(Player p){
    omega = 0;
    playa = p;
    currAngle = 0;
  }

  public void keyHasBeenPressed(KeyEvent e){
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_A){
      omega = 1;   //rotate anti-clockwise
    }
    if(key == KeyEvent.VK_D){
      omega = -1;    //rotate clockwise
    }
    if(key == KeyEvent.VK_S){
      omega = 0;
    }
  }

  public void rotate(){
    //angle of rotation is between 90 (negative x-axis) and -90 ( positive x-axis)
    if(currAngle + omega < 90 && currAngle + omega > -90){
      currAngle += omega;
    }
  }

  public void show(Graphics2D g2d){
    g2d.setColor(Color.GREEN);
    AffineTransform old = g2d.getTransform();
    g2d.translate(playa.getCenterXcoord(),playa.getCenterYcoord());
    g2d.rotate(Math.toRadians(-currAngle));
    g2d.translate(-playa.getCenterXcoord(), -playa.getCenterYcoord());
    g2d.fillOval(playa.getCenterXcoord() - width/2, 
                 playa.getYcoord() - height/2, width, height);
    g2d.setTransform(old);
  }

  public int getAngle() { return currAngle; }
}

I got it fixed but some of the angles still don't make sense to me.

Assuming you have a Graphics2D either from a Component or an Image:

  1. (Precondition): Put together the painting to paint your turret just as you've shown in your image.
  1. Get the center of the base (red) object: getX() + getWidth() / 2, getY() + getHeight() / 2;
  2. Do Graphics.translate(-xCenter, -YCenter);
  3. Graphics.rotate(turret rotation);
  4. Now that the graphics object has been rotated, translate back: (getWidth() / 2, getHeight() / 2)
  5. Draw the image from step 0

You can do this using AffineTransform as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM