Skip to main content
GameDev.net gamedev.net
🔒 Locked

rotating object toward character

Started by lack the hack Aug 11, 2006 at 4:26 PM 7 replies 1.4k views
Original Post
lack the hack
lack the hack
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
Demus79
Demus79

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
Limitz
Limitz
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)
lack the hack
lack the hack
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 vectorslookAt.Normalize(); //normalizefloat rotateRadians = atan2(lookAt.x,lookAt.y); //gives radiansfloat realAngle1 = (180.0f * (rotateRadians + 90.0f) / PI); //i think this is right? formula to get degrees instead of radians

anything wrong there?

thanks in advance
--nathan
lack the hack
lack the hack
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....

thanks in advance
--nathan
SiCrane
SiCrane
I can't even figure out if you're trying to do this in 2D or 3D.
lack the hack
lack the hack
i'm doing it in 3d....trying to rotate it on the y - axis.
hplus0603
hplus0603
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).
enum Bool { True, False, FileNotFound };
juanpaco
juanpaco
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.