Sign in to follow this  

Angle between two 3d Vectors

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(;
Vector3 axis = own.cross(tar);

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

This was done following this page :

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this