Limiting rotation

Problems building or running the engine, queries about how to use features etc.
Post Reply
ChronoDK
Gnoblar
Posts: 14
Joined: Tue Mar 20, 2007 8:33 pm

Limiting rotation

Post by ChronoDK »

Hi,

I have a few agents flying around in my game world, but I'm not satisfied with their rotation. Basically, they don't seem to know the difference between up and down :shock:

This is what I do on each update:

Code: Select all

agentSceneNode->setOrientation(mInitialHeading.getRotationTo(mVelocity));
The Vector3 mVelocity is calculated before this, and is used to translate the agent in some direction. The Vector3 mInitialHeading is at the moment always (0,0,-1).

My guess is that I might need a few more lines of code to stop the agent from turning upside down. Is there a simple way to limit roll? They should roll a little, but not all the way around.
User avatar
odyeiop
Halfling
Posts: 97
Joined: Fri Dec 22, 2006 4:56 am
Location: stoney creek

Post by odyeiop »

You could just make a variable that holds the amount of rotation that has been applied, and clamp it as is necessary before applying it.
I'm Immortal as you are Divine.
ChronoDK
Gnoblar
Posts: 14
Joined: Tue Mar 20, 2007 8:33 pm

Post by ChronoDK »

I'm sorry, I think I might need a bit more details.

I just tried using the below code instead of the above. I thought it would do the same thing:

Code: Select all

agentSceneNode->pitch(mInitialHeading.getRotationTo(mVelocity).getPitch());
agentSceneNode->yaw(mInitialHeading.getRotationTo(mVelocity).getYaw());
agentSceneNode->roll(mInitialHeading.getRotationTo(mVelocity).getRoll());
Instead, the agent just spins crazily.
User avatar
Samir
Halfling
Posts: 59
Joined: Sat Jun 25, 2005 12:11 pm

Post by Samir »

Note that getRotationTo calculates the smallest angle to rotate to the destination. Therefore, sometimes, when you need it to rotate one way, it will rotate the other way because it is a smaller angle. odyeiop is right, you should store a local angle value and increment it yourself which would result in a rotation consistent in one direction.
ChronoDK
Gnoblar
Posts: 14
Joined: Tue Mar 20, 2007 8:33 pm

Post by ChronoDK »

How would I do that using Ogre? I might need a code example.

I found code quite similar to this in the wiki:

Code: Select all

Vector3 newdirection = mVelocity.normalisedCopy();
newdirection.y = 0;
newdirection.normalise();
Vector3 currentdirection = agentSceneNode->getOrientation() * Vector3::NEGATIVE_UNIT_Z;
currentdirection.y = 0;
currentdirection.normalise();
	
Quaternion q = currentdirection.getRotationTo(newdirection);             
agentSceneNode->rotate(q);
With that, the yaw works and the rolling is gone. There is no pitch though, so it is not enough. Could someone explain what is going on in detail? :)
big_o
Goblin
Posts: 279
Joined: Sun Feb 19, 2006 1:08 am

Post by big_o »

I agree with odyeiop. I posted some code here showing what he means. It is a lot simpler than messing with quaternions in cases like this.
Post Reply