gpt4 book ai didi

java - 设置移动和旋转物体上的推力位置

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

我有一艘宇宙飞船,那艘宇宙飞船可以 360 度在太空中移动。

宇宙飞船需要 2d 推力动画。信任动画需要位于飞船的底部中线。我有以下变量。

_Rotation
_Spaceship.width
_Spaceship.height
_Spaceship.Position(x,y)

我也上传了我的问题的图片,以防人们不理解我的错误解释:

/image/wIEx3.jpg

两个动画都像这样渲染:

this._itemAnimation.render(this.getPosition(), canvas, this._degrees);
this._thrustAnimation.render(this.thrustPosition(), canvas, this._degrees);

到目前为止我已经尝试过但失败了:

_thurstPosition.set(((int)_object_x + Math.cos(_degrees) * _itemAnimation.getWidth() / 2) , 
((int)_object_y + Math.sin(_degrees) * _itemAnimation.getWidth() / 2));

我失败了,有人帮助我。

--- 更新 ---

我更新了代码,以便更好地理解:

    int SpaceshipCenterX = getPosition().x + (_SpaceshipAnimation.getWidth() / 2) - (_thrustAnimation.getWidth() / 2);
int SpaceshipCenterY = getPosition().y + ((_SpaceshipAnimation.getHeight() / 2) - (_thrustAnimation.getHeight() / 2));

double OffSetCos = Math.cos(_spaceshipDegrees);
double OffSetSin = Math.sin(_spaceshipDegrees);

_thurstPosition.set
(
(int)(SpaceshipCenterX + (SpaceshipAnimation.getWidth() / 2) * OffSetCos)
,
(int)(SpaceshipCenterY + (SpaceshipAnimation.getHeight() / 2) * OffSetSin)
);

我仍然无法让它发挥作用。它绕着飞船旋转,但速度非常快,并且到处闪烁。

--- 更新 2 ---

这几乎可以工作,但还差得太远:

    int xOffset = -1 * (_itemAnimation.getWidth() / 2);
double DegreeToRadien = Math.toRadians(_degrees);

_thurstPosition.set
(
(int)(((xOffset) * Math.cos(DegreeToRadien)) + getPosition().x),
(int)(((xOffset) * Math.sin(DegreeToRadien)) + getPosition().y)
);

最佳答案

假设您正在使用此坐标/角度系统:

                90 (pi/2) - Up
^
|
Left - 180 (pi) <----|----> 0 - Right
|
v
Down - 270 (3pi/2)

你的宇宙飞船将以 0 度向右行驶

>[ } 0

然后,对于任何方向,您需要相对于飞船中心平移推力,假设我们在 x 方向上平移

offset = -1 * width/2;

然后将其旋转飞船的角度,最后将其平移飞船的位置。

要计算此变换,请以相反顺序将 3 个变换写为矩阵,然后将它们相乘,变换从 (0,0) 开始的点

   [1 0 spaceshipX] [cos(dir) -sin(dir) 0] [1 0 -width/2] [0]
[0 1 spaceshipY] [sin(dir) cos(dir) 0] [0 1 0 ] [0]
[0 0 1 ] [ 0 0 1] [0 0 1 ] [1]

这样你就会得到推力的位置

thrustX = (-width/2)cos(dir) + spaceshipX
thrustY = (-width/2)sin(dir) + spaceshipY

所以我想您只是错过了需要减去 width/2 而不是添加它的事实。

我已使用正确且更易读的语法对其进行了编辑。到处使用下划线确实会损害可读性。我假设你有一个宇宙飞船类,并且宇宙飞船有宽度、高度、x 位置、y 位置和旋转。您有一个推进器类,它也有宽度、高度、x 位置、y 位置和旋转。 (它们可以从 Sprite 抽象类继承)。为了设置推进器对象的位置,我们调用 Thruster.setPosition(x,y);

int xOffset = -1 * (spaceship.width / 2);

thruster.setPosition(
(int)(((xOffset) * Math.cos(spaceship.rotation)) + spaceship.x),
(int)(((xOffset) * Math.sin(spaceship.rotation)) + spaceship.y)
);

希望这能让您清楚需要在何处设置哪些值。如果没有看到更多内容、这些变量的声明位置以及它们的实际含义,我就无法解读您的代码。

更新

只是作为结论,我想您可能已经发现了。 Math.cos 和 Math.sin 要求角度以弧度为单位,而不是度数。我在这里给出的解决方案是正确的,并且我已经展示了如何通过执行矩阵计算来计算任何相对定位的对象的位置。您只需记住 spaceship.rotation 必须以弧度为单位,或者您必须在将其传递给 Math.cos() 或 Math.sin() 之前将其从度数转换为弧度。

int xOffset = -1 * (spaceship.width / 2);
double radians = Math.toRadians(spaceship.rotation);

thruster.setPosition(
(int)(((xOffset) * Math.cos(radians)) + spaceship.x),
(int)(((xOffset) * Math.sin(radians)) + spaceship.y)
);

关于java - 设置移动和旋转物体上的推力位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10018173/

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