Sign in to follow this  
Rasmadrak

Find angle between two Y positions...?

Recommended Posts

Hi there! I'm wondering how to (insert name of thread here) ...? Two points, A and B, got x,y,z values in 3D space. If the points where on a plane, it should be easy trig to find the direction, but since the two points are in a 3D space, I'm wondering if there's a way to calculate the angle using only the Y's of the positions...?

Share this post


Link to post
Share on other sites
I'm not sure what you mean -- if you want to find the angle between the Y components of the vertices, this will always be zero. Because this is a one-dimensional problem the positions will both lie somewhere along the same (Y) axis and hence their angle is zero.

I suppose you want to find the angle between the two vectors when projected on some plane (let's say the XY plane). The angle between vectors is given by the dot product. I'm not sure but I think you can just construct two 2D vectors by discarding the Z-component and then compute a 2D dot product.

Greetz,

Illco

Share this post


Link to post
Share on other sites
you want to calculate the angle relative to what exactly? the x-z plane? I'm a little confused on exactly what youre asking, can you possibly re-phrase the question? exactly what angle do you want?

Tim

Share this post


Link to post
Share on other sites
Ok, sorry for the vague question! :)

Anyway, I need to find the angle between two Y positions ANYWHERE in the 3d area...


       
| Y
|
|
A |________B_____ X
|/ |
| x
/|
/ x
/__________________
Z


x = Intersection dot with ground

I have already calculated the angle between X,Z coords of the points, but since it's 3d, I dont know how to calculate the angle between Ay and By... :S

Any clues?

Share this post


Link to post
Share on other sites
Nice art. But as I said: the Y-coordinates themselves have no angle. Imagine the Y-axis:

|
|
|
|
|

Then Ay and By are somewhere along it:

|
| <- By
|
| <- Ay
|

They do not make any angle as this is a one dimensional axis. Put differently: the vectors run in parallel. So I think I'm still not clear on what you want.

Illco

Share this post


Link to post
Share on other sites
Don't have time to give the answer atm, but I'll venture a guess as to what the OP is asking. Perhaps this is to determine an appropriate yaw and pitch or yaw and pitch delta given a target point. So (minus the details), the solution may be to find the difference between the angles from A and B to the ground plane (or equivalently, the y axis).
Quote:
I have already calculated the angle between X,Z coords of the points
If you were to post your code for this, it would probably become clear what you're trying to do with the y coordinates.

Share this post


Link to post
Share on other sites
The above poster is correct... :)

I'm trying to set the Yaw of a vehicle that is roaming my heightmap....

I have vector3d Pos, Direction, Gravity...
where: Pos += Direction-Gravity;

To find a angle in the X,Z plane, I use:
180 * (1 + atan2(From.z-To.z, From.x-To.x) / PI);

But one cant simply use the same function on the Y plane, because the X,Z are not in the same plane, correct..? <-- not sure thou...



Share this post


Link to post
Share on other sites
Ok that is more clear. Then I'll venture a guess:

+ <-- Ay
|
|
|
|------+ <-- By



Is the angle of this triangle perhaps what you are looking for? Imagine the triangle being rotated about the Y axis such that the facing is already correct (according to your XZ calculations). Then this would yield the pitch right? If so the trig is pretty easy.

Illco

Share this post


Link to post
Share on other sites
Exactly, and what I orinally, rather stupidly, questioned was how to achieve this "triangle rotation" ... :)

I can find the angle (or direction) on a single plane, I just dont know how to transform it according to the second plane... sort of.


EDIT:


since I got the Direction, A and B points as vectors, shouldn't there be some simple addition or something to achieve what I want? Maybe if I lerp'ed the direction towards the new position.... ? hmm... be right back... :)

Share this post


Link to post
Share on other sites
Here's a possible solution:

// input vectors are a and b

angle1 = atan2(sqrt(a.x*a.x+a.z*a.z),a.y);
angle2 = atan2(sqrt(b.x*b.x+b.z*b.z),b.y);
pitch_delta = angle2-angle1; // Or angle1-angle2

Again, that's assuming I'm understanding the problem correctly.

Share this post


Link to post
Share on other sites
Didn't work... :/

Now the vehicle is more or less locked to the ground....
This is the code I'm currently using:


GroundLevel = GetAltitude(Pos.x,Pos.z);

Direction += GravityVector;
vector3d OldPos = Pos;
Pos += Direction*g_delta;

if (Pos.y < GroundLevel+Radius)
{
float angle2 = atan2(sqrt((OldPos.x*OldPos.x)+(OldPos.z*OldPos.z)),OldPos.y);
float angle1 = atan2(sqrt((Pos.x*Pos.x)+(Pos.z*Pos.z)),Pos.y);
TerrainElevation = angle2-angle1;

// TerrainElevation = 50; //should be AngleOf(OldPos.y,Pos.y);
Direction.SetDirection(VehicleYaw,TerrainElevation,CurrentSpeed);

Pos.y = GroundLevel+Radius;
}



Share this post


Link to post
Share on other sites
I'll look at your code more carefully in a bit, but meanwhile, are you missing any radian/degree conversions?

Share this post


Link to post
Share on other sites
Ah, I think you're trying to solve a different problem than I thought. Try this:

Vector3 v = pos - oldPos;
pitch = atan2(v.y,sqrt(v.x*v.x+v.z*v.z)); // You might have to negate this

I think pitch is what you're calling 'TerrainElevation'.

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