Java 由于x,y步进,射击不准确的子弹
在我的Java游戏中,当射击(慢速)子弹时,它们会以错误的角度移动,但是当加速时,它们会变得越来越精确 我的x,y,速度和方向都是int,但是我已经尝试转换成浮点以获得更高的精度,但是我仍然有相同的错误。我相信这是因为我能得到的最低运动步数是整数,比如(+2x和+1y一步,而不是+1.7x和+0.88y-我不能在0.5像素上) 我如何“微步”移动子弹,使其以正确的角度射击? 我能想到的另一个解决办法是以正确的角度拍摄它们,即计算碰撞终点并朝着该点移动 所需的行为是子弹以正确的角度(玩家到鼠标)射击,而不是根据子弹的速度以“关闭”的角度射击Java 由于x,y步进,射击不准确的子弹,java,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代码:(角度计算正确,因为我可以完美地在两点之间画一条线,当我添加运动时,它会弄乱)
我还尝试了变量的双精度:
例如:
长的蓝线是从玩家到鼠标的,黄线是子弹,子弹的角度是正确的,但方向不是正确的,应该正好在蓝线上。这是在子弹速度为2的情况下-如果子弹速度为20,则根据下一个img,它们更接近蓝线:我遇到了你的问题:你对计算结果使用整数强制转换,这意味着你只需删除
之后的所有内容,因此如果结果为1.9,你将返回1作为长度。如果提高速度
,此错误将减少,这就是为什么提高速度
会得到更好的结果。在返回结果之前,需要对结果进行四舍五入。另一方面,你真的应该改成双倍。在您显示的使用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(…)
是否可以显示GameController.lengthdir\u x(速度、方向)代码>和游戏控制器。长度方向(速度、方向)代码>?永远不要用int
计算,即使你认为像素是int。这只会带来不准确。当所有内容都已计算完毕并且您想要绘制对象/形状时,请使用double
或BigDecimal
并仅转换为int
。添加lengthdir代码后,我将从现在起将其保存在double Tom中以保持简单。@Tom:我认为这在很大程度上取决于计算类型。对我来说,在坐标计算中使用大小数听起来像是一种巨大的性能过剩。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用“编辑”链接改进您的问题-不要通过评论添加更多信息。谢谢我会在回家后测试它-我相信我在测试双打时确实删除了lengthdir中的(int)类型,但我会再做一次尝试,再做一些调整。我已经对你的添加进行了一些小的调整。删除(int)类型转换只占解决方案的1/3,其他的是将x,y int更改为double,并将方向更改为double。虽然玩家不能在x=53.3像素的位置上,所以浮动中的位置仍然令人困惑。但你一定要坚持使用double
,因为每次你将其转换为int
,并将其添加到x
/y
时,即使你使用Math.round(…)
,你也会累积额外的错误。你不必担心像素,在某些系统中(尤其是在移动设备上),你通常具有高像素密度,而你的绘图功能通常与dip(密度无关像素)一起工作,在dip中,你实际上可以拥有53.3的真实像素
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.
}
}