# [java] +java3d +Angle between two points

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

## 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 on other sites
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 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 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.

45 degrees?

##### 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.

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 on other sites
Are you using any unit vectors here?
Normal Vector for the camera vector?

L-

##### Share on other sites
No. I am not using.
Is there any snippet you guys can provide?

##### 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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011247
• ### Who's Online (See full list)

There are no registered users currently online

×