Jump to content
  • Advertisement
Sign in to follow this  

Angle between two 3d Vectors

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make an object "look at" another given they're translation vectors.

The way I'm doing it now is this:

Vector3 own = owner->getTransform().getTranslation();
Vector3 tar = target->getTransform().getTranslation();

float angle = Math::acosF(own.dot(tar));
Vector3 axis = own.cross(tar);

glRotatef(Math::radToDeg(angle), axis.X(), axis.Y(), axis.Z());

This was done following this page : http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm

The results are not what you would expect and I was hoping you could help me figure out why. If you need more information just ask. Thanks!

Share this post

Link to post
Share on other sites
The translation vectors are not what you want to be using here. The translation vectors correspond to the location of the objects, not where they are looking. You need to pull one of the columns out of the rotation matrix of the transform. (a column or a row of the rotation part of the transform depending on your API.) So, instead of owner->getTransform().getTranslation(), I imagine you'd have something like owner->getTransform().getRotation().getLookAt(), or perhaps owner->getTransform().getRotation().GetZAxis()...or something along those lines depending on your API (and GetX/GetY/GetZAxis depending on which of those object space axes represents the direction being looked in).

I am also concerned with the code you posted. You are using a library (a C++ library?) to represent your objects and you are using state properties stored in those objects to determine the angle change. BUT, you have called an OpenGL function directly to apply the rotation. I do not see where you set the updated orientation on the "owner" object. Without something else...your code objects aren't going to track what you render.

I hope that helps!

Share this post

Link to post
Share on other sites
Ha, thanks, that clarifies things!

As for the library, yes, it's c++ and its custom made. The state of the objects is managed by using glGetFloatv to pass the ModelViewMatrix to the objects.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!