• Advertisement
Sign in to follow this  

[java] +java3d +Angle between two points

This topic is 4304 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

Hi there. Can you help me? I got two points(X,Y,Z) A and B in space. How can I find the look vector from A to B grom a given moment? I mean, at zero frame, "A" got no rotation at all. And at frame 10, for example, "B" move 10 units on -X. How can I calculate the angle I have to rotate in "A" to continue viewing the "B" coordinate ?

Share this post


Link to post
Share on other sites
Advertisement
Finding the look vector is fairly straightforward:

lookVector.X = B.X - A.X;
lookVector.Y = B.Y - A.Y;
lookVector.Z = B.Z - A.Z;
lookVector.Normalize();

Sorry, that's in C# and DirectX, but that should be similar to Java3D.

Share this post


Link to post
Share on other sites
Also, take a look at the

void lookAt(Point3d eye, Point3d center, Vector3d up)

method in Java3D. I think it does exactly what you want.

Share this post


Link to post
Share on other sites
> Also, take a look at the
> void lookAt(Point3d eye, Point3d center, Vector3d up)
> method in Java3D. I think it does exactly what you want.

It should. But it doesnt. Its resulting a odd view.

I think I got misunderstood.

Let me make myself clear:

I got the camera on origin<0,0,0> and the target point at <0,0,-5> on frame zero. Thats make the camera look pretty forward, right?

Ok, now on frame 10, the target point move 5 units to left, which means <-5,0,-5>. So, how much I need to rotate the camera on Y axis(right?!) to keep focusing on the target point.

Share this post


Link to post
Share on other sites
Sorry, I was just being sarcastic. Of course it's 45 degrees! One way to compute the angle is to use the dot product. If I remember my vector math correctly:

A dot B = |A| * |B| * cos(pheta)
cos(pheta) = (A dot B)/(|A| * |B|)
pheta = acos((A dot B)/(|A| * |B|))

where A and B are vectors, |A| is length of vector A and pheta is the angle you are looking for. You will still have to find the direction somehow, but I'm too tired to figure out how.

For more info look at this Wikipedia article: Dot Product

Or you can just use the Java3D API: Vector4d.angle(Vector4d v1)
Just pass in the coordinates of points A and B as vectors.

Share this post


Link to post
Share on other sites
Sure, this will do:

Vector3f A = new Vector3f(0,0,-5);
Vector3f B = new Vector3f(-5,0,-5);
float angleAB = A.angle(B);

The API doc is right here.

Share this post


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

  • Advertisement