# rotating object toward character

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

## Recommended Posts

hello everyone :] what i'm trying to do is to rotate an object so that it is always facing the character (kind of sounds like billboarding, but in this case its facing the character instead of the camera) i tried dot product, but that only came up with wierd results resulting in 1000 - 360,000. so then i tried dividing it by 1000, and it just plus's the angle every update..... so maybe the answer i'm looking for is billboarding toward the main character? i would rather just find an angle that i could rotate instead though... thanks in advance --nathan

##### Share on other sites

If you are just looking for a rotation around your up axis, you'll need to calculate the vector from the object to the character, normalize it, and with atan2(x,y) you should find the correct angle, in radians naturally.

Yes, this is billboarding

Cheers

##### Share on other sites
if you got character position - object position, you have the "lookAt" vector for your object. You can use this vector to rotate your object.

If you do it 2d hypothetically you have a vector (x,y). An you can use tan to calculate the angle.

In 3d you should do something similar, but i cant give you calculations on the fly about that.

Hope this helps a bit though.

Greetings.

EDIT: what he said :) (too late)

##### Share on other sites
hey, thanks for the answers :]

its still not really working though....it won't rotate fully, only 90 - 180 degrees usually...

here's the code i'm using:

CVector lookAt = CVector(anglex,angley,anglez); //anglex,angley,anglez calculate lookAt vectors
lookAt.Normalize(); //normalize
float realAngle1 = (180.0f * (rotateRadians + 90.0f) / PI); //i think this is right? formula to get degrees instead of radians

anything wrong there?

--nathan

##### Share on other sites
bump
does anyone see anything wrong with the code above in my last post? i still can't and haven't figured out this problem....

--nathan

##### Share on other sites
I can't even figure out if you're trying to do this in 2D or 3D.

##### Share on other sites
i'm doing it in 3d....trying to rotate it on the y - axis.

##### Share on other sites
If you want to rotate around the Y axis, then you should put in X and Z values into the call to atan2(). Also, because atan2() is defined in the mathematical X/Y plane, and you're using a rendering X/Z plane, you may need to negate one of the values, or swap the X/Z when passing to atan2().

An alternative is to not even do rotation, but instead construct a matrix directly. You can do this by putting normalized vectors into a matrix, and orthonormalizing it (google for Gram-Schmidt). The vectors are:

V = normalize(target pos - object pos)
Matrix.X basis: cross(up, V);
Matrix.Y basis: up;
Matrix.Z basis: V;
Matrix: orthonormalize(Matrix)

That's actually how I'd do it, as it deals with the case where the target is above/below the object, too (except for the exactly-above and exactly-below cases, which you need to detect and handle specially if it can happen).

##### Share on other sites
Your formula for converting from radians to degrees looks incorrect. Recall that 2PI radians = 360 degrees, therefore, to convert from radians to degrees, take x (where x is your number of radians) and do:

(x * 180) / pi

To go the other way, if x is your number of degrees:

(x * pi) / 180

I haven't looked at the trig very closely, but that conversion seems off. The way you have that set up, you're multiplying 90 by 180. That's a fairly large number.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×