# Rotating the avatar based on where you are pointing him... (OpenGL)

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, so if someone could help me out, it would be great. I'm modeling objects in my world with both a position vector and a unit vector that points in the direction that object is facing. I'm having trouble drawing the object with OpenGL so that it actually faces in the direction that the unit vector says it does. I assume you translate it to the position, and then somehow use a matrix to rotate it into the right direction. If you have a function (like the glRotate functions) that takes the a vector for the axis of rotation and then a magnitude, what do you input into the function so that it will rotate the object to face the direction unit vector? The way I have it set up now works (sort of), but I'm clearly faking it, and I don't think the asin fuction is very fast. Anyone know how to do this properly? my work so far looks like this:
	if(active->getEntity()->GetDirX() > 0.0f)
glRotatef(asin(active->getEntity()->GetDirY())*180/pi, 0.0f, 0.0f, 1.0f);
else
glRotatef(180-asin(active->getEntity()->GetDirY())*180/pi, 0.0f, 0.0f, 1.0f);
active->getEntity()->Animate(10.0/FPS);

##### Share on other sites
How about storing the actual rotation in radians ? Like, having 1 float for rotation around the world-up axis. With that you can easily create a rotation matrix. 1 vector kind of has more information, but it is lacking the information of the 2 other orientations. Also, if you need this orientation vector you can easily calculate it from the rotation angle.

You can express the code

glRotatef(asin(active->getEntity()->GetDirY())*180/pi, 0.0f, 0.0f, 1.0f);

as

glRotatef(active->entity()->GetUpRotation(), 0.0f, 0.0f, 1.0f);

assuming that 0.0,0.0,1.0 is the up direction of your world.

##### Share on other sites
Okay, yeah =)

That seems simple enough. Thanks.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633301
• Total Posts
3011269
• ### Who's Online (See full list)

There are no registered users currently online

×