Calculating direction from 1 point in a 3d space to another
I am looking for a way to calculate the direction from one point in a 3d space (xyz) to another point, so I can use that data in Mesh.Intersect(Vector3 rayPos, Vector3 rayDir). The direction parameters are in radians.
[217 byte] By [
Kaetemi] at [2007-12-16]
Are you sure the rayDir parameter is supposed to be in radians? I took a quick look at the documentation and didn't find anything that said or contradicted that, but I don't think you need three angles to define a direction in 3D. It seems to be that with the second parameter being a Vector3, what they're looking for is offsets in each of the 3 coordinates. Try passing in a vector with x2-x1, y2-y1, and z2-z1, where your two points are (x1,y1,z1) and (x2,y2,z2).
Yep, it's like that for for that command, but I also need those calculations anyway for Matrix.RotationYawPitchRoll(float yaw, float pitch, float roll)
You'll have to forgive me as I don't know my pitch from my yaw, but calculating an angle is fairly easy. If you'll remember from trig, tan( angle) = opposite / adjacent. So, angle = arctan( opposite / adjacent). For example, the angle in the X/Y plane is arctan( (y2-y1) / (x2-x1)), and similarly for the other planes. Note from the equation that it won't work if x2 = x1. In that case, the angle is either pi/2 or 3pi/2 (depending on whether y2 is greater than or less than y1). Hope this helps!
OK, just looked it up...
- yaw = Yaw around the y-axis (x/z plane), in radians.
- pitch = Pitch around the x-axis (y/z plane), in radians.
- roll = Roll around the z-axis (x/y plane), in radians.
So, assuming X points to the right, Y points up, and z points towards you...
yaw = arctan( (x2-x1) / (z2 - z1))
pitch = arctan( (z2-z1) / (y2 - y1))
roll = arctan( (y2-y1) / (x2 - x1))
Thank you very much for your help :D
Now the next problem is when using them in Matrix.RotationYawPitchRoll ...
When I use only 1 of them, it works perfectly correct for that part of the rotation, but when I use 3 or just 2 of them together it doesnt work..
Or is there an easier way of letting one side of a box always point to a vertex?
The code for the Matrix.RotationYawPitchRoll:
| | Matrix.RotationYawPitchRoll(_GetYaw0(v.X, v.Z), _GetPitch0(v.Z, v.Y), _GetRoll0(v.Y, v.X)) |
The code for the Yaw/Pitch/Roll calculation:
| | public float _GetYaw(float x, float z, float xs, float zs) { return _GetYaw0(x - xs, z - zs); } public float _GetYaw0(float x, float z) { if (z > 0) { return (float)(Math.Atan((double)(x / z))); } if (z < 0) { return (float)(Math.Atan((double)(x / z)) + Math.PI); } if (x > 0) { return (float)Math.PI / 2; } if (x < 0) { return -(float)Math.PI / 2; } return 0; } public float _GetPitch(float z, float y, float zs, float ys) { return _GetPitch0(z - zs, y - ys); } public float _GetPitch0(float z, float y) { if (y > 0) { return (float)(Math.Atan((double)(z / y))); } if (y < 0) { return (float)(Math.Atan((double)(z / y)) + Math.PI); } if (z > 0) { return (float)Math.PI / 2; } if (z < 0) { return -(float)Math.PI / 2; } return 0; } public float _GetRoll(float y, float x, float ys, float xs) { return _GetRoll0(y - ys, x - xs); } public float _GetRoll0(float y, float x) { if (x > 0) { return (float)(Math.Atan((double)(y / x)) + ((Math.PI *3) / 2)); } if (x < 0) { return (float)(Math.Atan((double)(y / x)) + (Math.PI / 2)); } if (y < 0) { return (float)Math.PI; } return 0; } |
First, fix the bug in your code. ;) (You're missing a code block in _GetRoll0()). I would assume that Matrix.RotationYawPitchRoll() would be the correct thing to use. However, I've never played with this library, so I just don't know. I rather suspect it's a bug in your code or a misunderstanding of what exactly the function does. You might want to start another thread to get the attention of someone who is more familiar with the library.
it's not a bug, when y > 0 it will also return 0 :p
but i'm doing it in a different way now, so i dont really need them anymore at the moment..
but i now need matrix to pitch/yaw/roll, lol :p