Implementing a rotateTo() Method

Hello, I need help with math. I have a spatial and a vector and need to set the angular velocity of the spatial so that it rotates towards the vector in just the Y axis. The second step would be to calculate the time the rotation would take so I can stop the velocity. Can someone point me to threads/tutorials that might help with calculating the value that setAngularVelocity() needs, please? Thanks for your time.

Edit: Got the rotation working thanks to oskarkv from the IRC

                    contactPoint = collisionResults.getCollision(0).getContactPoint();
                    rayDir = Batter.getWorldTranslation().subtract(contactPoint);
                    rayDir.y = 0f;
                    click3d = Batter.getWorldRotation().multLocal(new Vector3f(0f, 0f, 1f));
                    click3d.y = 0f;

                    if (rayDir.y > 0) {
                        Batter.getControl(RigidBodyControl.class).setAngularVelocity(new Vector3f(0f, 5f, 0f));
                    else if (rayDir.y < 0) {
                        Batter.getControl(RigidBodyControl.class).setAngularVelocity(new Vector3f(0f, -5f, 0f));

setting angular velocity directly is questionable and may produce errors when you want to look at some point precisely using it

for rotation you should use applyTorque and for movement applyForce
you must have on mind, that applying force must be framerate independent, so you should have vector and tpf, tpf scales your vector, so full value is applied in 1 sec interval of force applying


public void update(float tpf) {
    rigidBody.applyTorque(new Vector3f(0f, 5f, 0f).multLocal(tpf));

after 1 sec, force of 5f is fully applied (if fps is 60, then tpf step is 1/60)

after you understand this, you can go to and learn about PID controller
your biggest problem from this time will be oscillation
PID Controller

If you are interested, i will write you tonight, what to do next and what i use in EnginesControl in my space ship game. I simply give Quaternion with required rotation to engines and then i let them to work it out themselves. I think its pretty sophisticated :slight_smile:

1 Like