Libgdx Box2D平滑的角度旋转 [英] Libgdx Box2D smooth angular rotation
问题描述
我目前正在libgdx中开发一个飞行游戏,您可以用鼠标光标控制小鸟的旋转.现在,小鸟将立即旋转到光标位置.我的目标是使鸟类旋转稍微滞后一些,以使飞行看起来更平稳.旋转阻尼将不起作用,因为我正在使用setTransform()方法.我会对任何建议感到非常高兴,这是控制旋转的代码,在render方法中被称为每一帧:
I'm currently developing a flying game in libgdx where you can control the rotation of a bird with the mouse cursor. Right now the bird will instantly rotate to the cursor position. I'm aiming to make the bird rotation lag behind a little bit to make the flying seem more smooth. Rotational damping wont work because i am using the setTransform() method. I would be very happy about any suggestions, this is the code controlling the rotation, it's called every frame in the render method:
//get cursor pos
cursorPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(cursorPos);
//get bird pos
birdPos.set(body.getPosition().x, body.getPosition().y, 0);
//birdpos-cursorpos
birdPos.sub(cursorPos);
//new vector which is pointing from bird to cursor
direction.set(birdPos.x, birdPos.y);
//get current linear velocity
movement.set(body.getLinearVelocity());
//apply rotation to bird if cursor is not too close
if (Math.sqrt(Math.pow(direction.x, 2) + Math.pow(direction.y, 2)) > 1) {
body.setTransform(body.getPosition().x, body.getPosition().y, direction.angleRad());
}
推荐答案
我假设这段代码在您的更新循环中.
I'm assuming this code is in your update loop.
创建实例变量angleTarget.
Create an instance variable angleTarget.
每个更新循环:
-
将angleTarget设置为鼠标角度.
Set angleTarget to your mouse angle.
使用setPosition将实际的身体角度设置为:body.angle = body.angle +(angleTarget-body.angle)* 0.05
Use setPosition to set the actual body angle to: body.angle = body.angle + (angleTarget - body.angle) * 0.05
通常,将表达式放入名为lerp的函数中,该函数表示线性插值.查找"lerp函数"以了解有关此内容的更多信息.
Usually that expression is put in a function called lerp, which stands for linear interpolation. Look up 'lerp function' to read more about this.
这篇关于Libgdx Box2D平滑的角度旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!