Java Libgdx-使用对象的移动旋转对象
我正在尝试为SpriteAnimation类实现一个自动旋转方法(尝试移植) 一切正常,精灵绘制正确(它使用了正确的动画),只是旋转不正确,如果在正常情况下,它似乎只有在目标匹配其位置后才指向原点(0,0),而我希望它的旋转随着移动而更新,我尝试了度和弧度,两者都不起作用。它应该旋转以匹配当前的方向。我已经为此挣扎了大约一个星期,但仍然没有得到想要的结果 相关代码: 从精神化Java Libgdx-使用对象的移动旋转对象,java,libgdx,rotation,autorotate,shouldautorotate,Java,Libgdx,Rotation,Autorotate,Shouldautorotate,我正在尝试为SpriteAnimation类实现一个自动旋转方法(尝试移植) 一切正常,精灵绘制正确(它使用了正确的动画),只是旋转不正确,如果在正常情况下,它似乎只有在目标匹配其位置后才指向原点(0,0),而我希望它的旋转随着移动而更新,我尝试了度和弧度,两者都不起作用。它应该旋转以匹配当前的方向。我已经为此挣扎了大约一个星期,但仍然没有得到想要的结果 相关代码: 从精神化 // The x position of the sprite's upper left corner pixel.
// The x position of the sprite's upper left corner pixel.
public int getX() { return (int)position.x; }
public void setX(int value)
{
prevPosition.x = position.x;
position.x = value;
updateRotation();
}
// The y position of the sprite's upper left corner pixel.
public int getY() { return (int)position.y; }
public void setY(int value)
{
prevPosition.y = position.y;
position.y = value;
updateRotation();
}
void updateRotation()
{
if (rotateByPosition)
{
Vector2 rotationVector = new Vector2(position.x - prevPosition.x, position.y - prevPosition.y);
rotationRad = rotationVector.angle();
rotationDeg = rotationRad * MathUtils.radiansToDegrees;
}
}
public void MoveBy(int x, int y)
{
prevPosition = new Vector2(position);
position.x += x;
position.y += y;
updateRotation();
}
public void Update(float deltaTime)
{
// Don't do anything if the sprite is not animating
if (animating)
{
// If there is not a currently active animation
if (getCurrentFrameAnimation() == null)
{
// Make sure we have an animation associated with this sprite
if (animations.size() > 0)
{
// Set the active animation to the first animation
// associated with this sprite
String[] sKeys = new String[animations.size()];
int index = 0;
for (Entry<String, FrameAnimation> mapEntry : animations.entrySet()) {
sKeys[index] = mapEntry.getKey();
index++;
}
setCurrentAnimation(sKeys[0]);
}
else
{
return;
}
}
// Run the Animation's update method
getCurrentFrameAnimation().Update(deltaTime);
// Check to see if there is a "follow-up" animation named for this animation
if (getCurrentFrameAnimation().getNextAnimation() != null && !getCurrentFrameAnimation().getNextAnimation().isEmpty())
{
// If there is, see if the currently playing animation has
// completed a full animation loop
if (getCurrentFrameAnimation().getPlayCount() > 0)
{
// If it has, set up the next animation
setCurrentAnimation(getCurrentFrameAnimation().getNextAnimation());
}
}
}
}
public void Draw(SpriteBatch spriteBatch, int xOffset, int yOffset)
{
updateRotation(); // Calling this while testing to make sure that it is being called
spriteBatch.draw(getCurrentTextureRegion(), getPosition().x + xOffset - center.x, getPosition().y + yOffset - center.y, center.x, center.y, getCurrentFrameAnimation().getFrameWidth(), getCurrentFrameAnimation().getFrameHeight(), 1.0f, 1.0f, rotationRad);
}
要获取弧度角度,请执行以下操作:
void updateRotation()
{
if (rotateByPosition)
{
Vector2 rotationVector = new Vector2(position.x - prevPosition.x, position.y - prevPosition.y);
rotationRad = rotationVector.angleRad();
rotationDeg = rotationRad * MathUtils.radiansToDegrees;
}
}
绘制方法需要一个角度:
spriteBatch.draw(
getCurrentTextureRegion(),
getPosition().x + xOffset - center.x,
getPosition().y + yOffset - center.y,
center.x, center.y,
getCurrentFrameAnimation().getFrameWidth(), getCurrentFrameAnimation().getFrameHeight(),
1.0f, 1.0f,
rotationDeg
);
屏幕鼠标位置位于渲染器需要的左上角(左下角):
int xTouch = Gdx.input.getX(0);
int yTouch = Gdx.graphics.getHeight() - Gdx.input.getY(0);
它现在是实时旋转的,但它仍然没有朝着它要去的方向旋转,它或多或少是这样,但它所面对的方向有点落后于它的实际运动。此外,由于坐标使用向下为正,相机反转,因此输入很好。发现问题,在应用修复后,我编辑了MobileSprite类的更新方法,我更改了增量向量2。
getPosition()。添加(增量)代码>应该是setPosition(getPosition().x+Delta.x,getPosition().y+Delta.y)。
spriteBatch.draw(
getCurrentTextureRegion(),
getPosition().x + xOffset - center.x,
getPosition().y + yOffset - center.y,
center.x, center.y,
getCurrentFrameAnimation().getFrameWidth(), getCurrentFrameAnimation().getFrameHeight(),
1.0f, 1.0f,
rotationDeg
);
int xTouch = Gdx.input.getX(0);
int yTouch = Gdx.graphics.getHeight() - Gdx.input.getY(0);