简体   繁体   English

如何使用Graphics2D和AffineTransform围绕不断变化的原点旋转椭圆形?

[英]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. 我的射击者可以沿着屏幕底部的x轴完美移动。 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): 以x轴为0度旋转(相对于y轴正旋转角为正):

The turret should start at the top of the shooter's head (ie 90 degrees). 炮塔应始于射手头部的顶部(即90度)。 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). 如果按A ,则应继续向左旋转(逆时针),如果按D ,则应继续向右旋转(顺时针)。 If I press S it should stop rotating. 如果按S它将停止旋转。 A maximum rotation of 180 degrees is allowed (from positive x-axis to negative x-axis). 允许最大旋转180度(从正x轴到负x轴)。

With the code I have thus far my turret starts at 0 degrees (pointing in the positive x-direction). 到目前为止,使用代码我的炮塔是从0度开始的(指向正x方向)。 If I press A it rotates to 90 degrees (vertical), if I press D it rotates all the way to -90 degrees (vertical downwards). 如果按A它将旋转90度(垂直),如果按D ,将一直旋转至-90度(垂直向下)。 If I press S it stops. 如果我按S它将停止。

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: 假设您有来自组件或图像的Graphics2D

  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; 获取基础(红色)对象的中心:getX()+ getWidth()/ 2,getY()+ getHeight()/ 2;
  2. Do Graphics.translate(-xCenter, -YCenter); 做Graphics.translate(-xCenter,-YCenter);
  3. Graphics.rotate(turret rotation); Graphics.rotate(炮塔旋转);
  4. Now that the graphics object has been rotated, translate back: (getWidth() / 2, getHeight() / 2) 现在已经旋转了图形对象,请向后平移:(getWidth()/ 2,getHeight()/ 2)
  5. Draw the image from step 0 从步骤0绘制图像

You can do this using AffineTransform as well. 您也可以使用AffineTransform进行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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