Java游戏旋转图像和坐标轴
问题描述:
我用飞船做了游戏,但移动它的时候遇到了问题。 这是脚本:Java游戏旋转图像和坐标轴
public class spaceship {
private final local_client local_client;
private final int startX;
private final int startY;
private final int startR;
private double x,y,r;
public static double rotation;
public static double velo;
public static AffineTransform at;
public spaceship(local_client local_client,int startX,int startY,int startR) {
this.local_client = local_client;
this.startX = startX;
this.startY = startY;
this.startR = startR;
x=200;
y=200;
r=90;
}
public void drawImage(Graphics2D g2d) {
g2d.drawImage(local_client.spaceship_img,200,200, local_client.game);
at = g2d.getTransform();
at.translate(x , y);
at.rotate(Math.toRadians(r));
at.translate(-(local_client.spaceship_img.getWidth()/2),-(local_client.spaceship_img.getHeight()/2));
System.out.println(at.getTranslateX() + " - " + at.getTranslateY());
g2d.setTransform(at);
g2d.drawImage(local_client.spaceship_img,0,0, local_client.game);
}
所以用这个剧本我可以旋转图像(见屏幕): http://gyazo.com/c982b17afbba8cdc65e7786bd1cbea47
我的问题是,如果我增加在垂直轴X或Y移动(屏幕): http://gyazo.com/1fb2efb5aff9e95c56e7dddb6a30df4a 和不是在对角线
答
at.translate(x , y);
这行代码移动你的船周围,是否正确?增加x和y只会根据轴向上和向下移动它们。
如果您想沿着船的路径移动,您将不得不使用三角函数来计算实际的x和y来移动。
以下是大概的伪代码,应该让你朝正确的方向。取决于你的r,可能会不准确或不准确。
//drive along the ship's nose line
void moveAlongShipAxis(int magnitude) {
x += magnitude * Math.cos(Math.toRadians(r));
y += magnitude * Math.sin(Math.toRadians(r));
}
//strafe left and right, if you have that, otherwise you can skip this one
void moveYAlongTransverseAxis(int magnitude) {
y += magnitude * Math.cos(Math.toRadians(r));
x += magnitude * Math.sin(Math.toRadians(r));
}
嗨,它是什么大小,当我不得不称呼这种方法? – 2014-09-28 18:45:04
@terzi_matte假设汽车是汽车。当你按下油门踏板时,它会立即以100kmph的速度行进,还是会逐渐加快到100kmph?通过增加一个数量级,你可以确定船的行进速度。所以在你的游戏中100的幅度意味着该船以每秒100px的速度飞行。 10的幅度意味着每秒10px。 – Compass 2014-09-28 20:44:18
是的,非常感谢你,我在1小时后了解它,但是谢谢,我认为你已经扭转了角色和罪恶:) – 2014-09-30 18:45:12