gpt4 book ai didi

Java游戏旋转图像和轴

转载 作者:行者123 更新时间:2023-12-01 12:29:22 25 4
gpt4 key购买 nike

我制作了一款带有宇宙飞船的游戏,但我在移动它时遇到了问题。这是脚本:

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)静态 AffineTransform 位于;

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));
}

关于Java游戏旋转图像和轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com