【问题标题】:Java game rotate image and axisJava游戏旋转图像和轴
【发布时间】:2014-09-26 21:24:32
【问题描述】:

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

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 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 而不是对角线

【问题讨论】:

    标签: java image-rotation affinetransform


    【解决方案1】:

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

    【讨论】:

    • 嗨,它的大小是多少,我什么时候必须调用这个方法?
    • @terzi_matte 假设车辆是汽车。当您踩下油门踏板时,它会立即以 100 公里/小时的速度行驶,还是逐渐加快至 100 公里/小时?通过增加幅度,您可以确定船的行驶速度。因此,您的游戏中 100 级意味着飞船以每秒 100 像素的速度飞行。 10 的量级意味着每秒 10 像素。
    • 是的,非常感谢,1 小时后我明白了,但是谢谢,我认为你已经反转了 cos 和 sin :)
    • @terzi_matte 是的,我必须在脑海中编写大部分代码,所以我无法真正测试它,但概念就在那里:3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多