Advertisement

Find Distance in space

Started by May 16, 2002 11:25 PM
1 comment, last by slashandburn 22 years, 9 months ago
Im trying to get my bones to work correctly in my demo(test), but they shift around to much(very simple code(only shift the x,y,z)). I was thinking that if my bones could calculate their distance from one bone and the direction from them i could make my bones translate better(link at the ends. So my question is how would i find the distance between 2 3d points and how would i find the angle they are at from each other(2 angles(both shift the cordinates diffrently))? Then i could make my bones fight evil (more like zelda on n64 with big guns). ( ) = extra comment
distance between two point is the old and familiar pythagorean theorem. sqrt(dx*dx + dy*dy + dz*dz); where dx is the difference between the two x values, and same for dy an dz.

For the angle between two vectors, look into the dot product.
the dot product is defined as: d = x1*x2 + y1*y2 * z1*z2. Then its known that:

dot(a,b) = len(a) * len(b) * cos(theta);

now solve for theta using basic algebra and trig and you got it. I dont know if thats what you were asking, but that should get you moving in the right direction, and is good to know regardless
Advertisement
bonevector.x = bone1.x - bone2.x;
bonevector.y = bone1.y - bone2.y;
bonevector.z = bone1.z - bone2.z;

That will give you the vector from bone2 to bone1 (you'd want to calculate this using the closest end points of the bones). You just add the vector onto bone2 and they'll meet.

As for degrees, I only know how to do that on a 2D plane...
#include <math.h>
#define PI 3.14159;

degree = atan2( bonevector.x, bonevector.y ) * 180 / PI;
Might have to switch bone1 and 2 in the vector calculation above for this though, I'm not sure.
So you can use that if you know the points are on a single plane (x,y || y,z || x,z).

And my apologies if this was totally useless or utterly stupid. Some of your code would help. =)

[edited by - LockePick on May 17, 2002 1:00:34 AM]
_______________________________________Pixelante Game Studios - Fowl Language

This topic is closed to new replies.

Advertisement