Game Development Community

Rotating an object when moving

by Do Not Delete · in Torque X 2D · 11/21/2007 (10:11 am) · 11 replies

I was wondering how can I rotate an object when it is moving? I'm using the Starter Template for Torque X and I can only rotate it when it moves up and down, but it won't rotate when it moves left and right.

Here is the code (Movement Component)

public void ProcessTick(Move move, float elapsed)
        {
            if (move != null)
            {
                // set our test object's Velocity based on stick/keyboard input
                _sceneObject.Physics.VelocityX = move.Sticks[0].X * 20.0f;
                _sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 20.0f;
                _sceneObject.Rotation = -move.Sticks[0].X * 2000.0f;
                _sceneObject.Rotation = -move.Sticks[0].Y * 20.0f;
            }
        }

Thanks in advanced,
Dro Sarhadian

#1
11/21/2007 (10:36 am)
The real problem here is that you are setting the _sceneObject.Rotation twice and the second assignment is overwritting the first assignment, so _sceneObject.Rotation = -move.Sticks[0].Y * 20.0f; will always be the value and _sceneObject.Rotation = -move.Sticks[0].X * 2000.0f; will always be ignored.

The real question is what do you want to accomplish?
1. Do you just want the scene object to roll in the same direction regardless of direction?
2. Do you want the scene object to roll in the direction of movement (clock-left and counter-clock-right)?
3. Do you just want the sccene object to rotate to face the direction of travel?

Each of these cases will be coded differently.

John K.
#2
11/21/2007 (6:29 pm)
I would like the scene object to rotate to face the direction of travel.
#3
11/21/2007 (7:27 pm)
That's cool, you can do it with one line of code ;) Just replace the lat two lines in your original post with this:

_sceneObject.Rotation = 
T2DVectorUtil.AngleFromVector(new Vector2(move.Sticks[0].X * 20.0f, -move.Sticks[0].Y * 20.0f));

You just have to Love that T2DVectorUtil class. You already have the X and Y elements from the Move object. You can pass them into the AngleFromVector() method to get an angle (in degrees) that you can set to the object's rotation.

John K.
#4
11/21/2007 (7:29 pm)
Thank you :)
#5
11/23/2007 (11:48 pm)
You can actually do even less:

_sceneObject.Rotation = T2DVectorUtil.AngleFromInput(move.Sticks[0]);

AngleFromInput is there precisely for handling the inverted Y of the input devices (and the scaling by 20.0f doesn't affect angle).
#6
12/15/2009 (4:42 am)
when i used that code if i let go of the stick the player will reset to the original position, how can i get the player to stay facing the last position it was facing?
#7
12/15/2009 (4:52 pm)
This should leave them facing the last direction they were facing.

if(move != null)
{
    _sceneObject.Physics.VelocityX = move.Sticks[0].X * 20.0f;
    _sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 20.0f;
    _sceneObject.Physics.AngularVelocity = move.Sticks[0].X * 40.0f;
}
#8
12/15/2009 (5:13 pm)
i tried that but if i hold down the sticks it will continue to spin even if i am holding down an angle that should be the way it is headed...
_sceneObject.Physics.VelocityX = move.Sticks[0].X * 10.0f;
_sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 10.0f;
_sceneObject.Rotation = T2DVectorUtil.AngleFromInput(new Vector2(move.Sticks[0].X, move.Sticks[0].Y));

works perfectly but once i let go of the stick it resets...is there anyway i can create a variable that will hold the angle of the player?
#9
12/16/2009 (3:48 pm)
_sceneObject.Rotation holds the rotation angle of the player.


if you let go of the stick AngleFromInput returns 0 that is what you call resetting. You could try something like:

if (Math.Abs(move.Sticks[0].X) != 0.0f && Math.Abs(move.Sticks[0].Y) != 0.0f)
{
_sceneObject.Physics.VelocityX = move.Sticks[0].X * 10.0f;
_sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 10.0f;
_sceneObject.Rotation = T2DVectorUtil.AngleFromInput(new Vector2(move.Sticks[0].X, move.Sticks[0].Y));
}

However, I am not sure if that is a good way of rotating you object. In most cases using AngularVelocity will be better. Just using Rotation will interpolate very quickly.


#10
12/17/2009 (5:57 pm)
sorry I didn't fully understand what you were asking for Amrius, this should keep the current angle when you release the stick

*edited*
didn't notice I posted the same, code as Christian :P
#11
12/24/2009 (3:26 pm)
Hey Christian,

I think your code is accurate except one tiny detail (haven't tested it myself so if I'm wrong please let me know and I'll remove this). I think you should have an OR instead of an AND in your if statement. Currently, if the player is moving just along the X-axis or Y-axis then the condition will fail and the player won't move/rotate.

For example moving in direction (1,0) would not do anything.

Slightly altered version below:

// If the player is moving in some direction, set the velocity and rotation based on that direction
if (move.Sticks[0].X != 0.0f || move.Sticks[0].Y != 0.0f)
{
_sceneObject.Physics.VelocityX = move.Sticks[0].X * 10.0f;
_sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 10.0f;
_sceneObject.Rotation = T2DVectorUtil.AngleFromInput(new Vector2(move.Sticks[0].X, move.Sticks[0].Y));
}