由于x,y步进而射击不准确的子弹

时间:2017-08-14 12:55:26

标签: java

当我在Java游戏中射击(慢速)子弹时,它们会以不正确的角度移动,但是当加速时它们变得越来越准确。

我的x,y,速度和方向都是int,但我已经尝试转换为浮点数以获得更高的准确度,但我仍然遇到同样的错误。我相信它正在发生,因为我可以拥有的最低移动步长是整数,比如(+ 2x和+ 1y一步而不是+ 1.7x和+ 0.88y - 我不能在0.5像素上)

我怎样'微步'子弹以正确的角度射击它们? 我能想到的唯一能够以正确角度拍摄它们的其他解决方案是计算最终碰撞点并朝着这一点迈步。

期望的行为是让子弹以正确的角度(玩家对鼠标)射击,而不是根据子弹的速度以“关闭”角度进行射击。

public class Bullet extends GameObject
{
private int x;
private int y;
private int speed = 2;
private int direction;
private int length = 70;

public Bullet(int x, int y, int direction)
    {
    this.x = x;
    this.y = y;
    this.direction = direction; //Set the direction.
    }

public void update(Game game, GameController gc, float dt)
    {
    x += GameController.lengthdir_x(speed, direction);
    y += GameController.lengthdir_y(speed, direction);
    }

public void render(Game game, Renderer r)
    {
    //Draw the bullet with the tail behind it.
    r.drawLine(x, y, x + GameController.lengthdir_x(length, direction - 180), y + GameController.lengthdir_y(length, direction - 180), color);

    r.drawText("Dir: " + direction, x + 50, y + 20, 0xff0077ff); //Draws the players angle.
    }
}

Lengthdir代码:(角度计算正确,因为我可以完美地在两点之间绘制一条线,就在我添加运动时它会混乱)

    public static int lengthdir_x(int len, int dir)
    {
    return (int) (len * Math.cos(Math.toRadians(dir - 90)));
    }

public static int lengthdir_y(int len, int dir)
    {
    return (int) (len * Math.sin(Math.toRadians(dir - 90)));
    }

我还尝试过变量双打:https://pastebin.com/fbrF17bD

示例:http://puu.sh/x9OnN/be4e3f2c80.png 长蓝线是从玩家到鼠标,黄线是子弹,它们的射击角度是正确的 - 但是没有正确的方向行进,应该正好在蓝线上。这是2的子弹速度 - 如果子弹速度为20,它们将更接近蓝线,如下一个img:http://puu.sh/x9OwY/a54f201c91.png

1 个答案:

答案 0 :(得分:0)

我遇到了你的问题:你在计算结果上使用了Integer-cast,这意味着你只需删除.之后的所有内容,所以如果得到1.9,那么你将返回1作为长度。如果您增加speed,则此错误将会减少,这就是为什么您获得更高speed的结果的原因。在返回之前,您需要对结果进行舍入。另一方面,你应该真正改变为双倍。在您显示的使用double的代码中,您没有在长度函数中更改它,这就是为什么使用double没有获得更好的结果。所以你的代码应该是这样的:

public static double lengthdir_x(int len, int dir)
{
    //don't cast here to int!!!!
    return len * Math.cos(Math.toRadians(dir - 90));
}


public class Bullet extends GameObject
{
    private double x;
    private double y;
    private int speed = 2;
    private int direction;
    private int length = 70;

    public Bullet(double x, double y, int direction)
    {
        this.x = x;
        this.y = y;
        this.direction = direction; //Set the direction.
    }

    public void update(Game game, GameController gc, float dt)
    {
        x += GameController.lengthdir_x(speed, direction);
        y += GameController.lengthdir_y(speed, direction);
    }

    public void render(Game game, Renderer r)
    {
        //Draw the bullet with the tail behind it.
        r.drawLine((int)Math.round(x), (int)Math.round(y), x + GameController.lengthdir_x(length, direction - 180), y + GameController.lengthdir_y(length, direction - 180), color);

        r.drawText("Dir: " + direction, (int)x + 50, (int)y + 20, 0xff0077ff); //Draws the players angle.
    }
}

也许您需要将某些内容转换为int或double,但请确保lengthdir返回double作为结果或至少(int)Math.round(...)