Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Finding the distance between XYZ Vectors and calculating new Vectors with a given distance
Topic Started: Feb 28 2010, 09:03 PM (3,516 Views)
Electricity
Chaos vs. Cosmos
[ *  *  *  * ]
Hi, I go to college but still in our Math classes we have not yet reached the part about Vectors and Matrices, so I'm pretty clueless concerning these. So I come here to ask this question and hope that you will help me achieve bla bla bla...

Anyway how would I calculate/get the distance between 2 XYZ Vectors?
AND how would I calculate a new Vector giving a originXYZ, distance and Lat-/Longitude?

Should I just use Math like a2+b2=c2 (Pythagoras)?
Offline Profile Quote Post Goto Top
 
MysteriXYZ
Member Avatar
Master Matrix Masher
[ *  *  *  *  *  * ]
Electricity
Feb 28 2010, 09:03 PM
how would I calculate/get the distance between 2 XYZ Vectors?
The distance between two position vectors is equal to the length of the difference vector. In other words, you subtract the components of one vector from the corresponding components of the other vector, and then you use distance=(x2+y2+z2)1/2, where x, y and z are the resulting values.

Electricity
 
how would I calculate a new Vector giving a originXYZ, distance and Lat-/Longitude?

See the contents of the Move() script to calculate a direction vector pointing in the given direction, with a length equal to the given distance (which corresponds to the given speed in that script).
If you want to offset this vector (I think this is what you mean by "a originXYZ"), then you simply add those XYZ offsets to the corresponding components of the vector (you are effectively adding two vectors then).



When working with abstract math objects like vectors, you should always try to visualize them; a vector can simply be imagined as an arrow.

So if you add two vectors together, it's like you stick the arrowhead of the first into the nock of the second one; then their sum is the arrow that you can place between the nock of the first vector and the arrowhead of the second one.
When you need to subtract the second vector, just reverse the direction of the corresponding arrow.

Yup, William Tell was shooting with vectors, and he didn't even realize it :P .
U3D is like candy; after extensive consumption, it's Best to brush.
Offline Profile Quote Post Goto Top
 
Electricity
Chaos vs. Cosmos
[ *  *  *  * ]
MysteriXYZ
Feb 28 2010, 10:39 PM
Electricity
Feb 28 2010, 09:03 PM
how would I calculate/get the distance between 2 XYZ Vectors?
The distance between two position vectors is equal to the length of the difference vector. In other words, you subtract the components of one vector from the corresponding components of the other vector, and then you use distance=(x2+y2+z2)1/2, where x, y and z are the resulting values.
I seem to be getting some wrong results with this:
Code:
 
//Globe Radius Script
//Use only with Globe/Sphere/Ball Models!
//Usage: radius = GlobeRadius();
//
//x1,y1,z1 = Origin XYZ
//x2,y2,z2 = Vertex XYZ
//x3,y3,z3 = Difference Vector XYZ
var x1,y1,z1,x2,y2,z2,x3,y3,z3,Vect1,Vect2,Vect3;

x1 = self.x;
y1 = self.y;
z1 = self.z;

LockMesh(0,0,0,0); //Lock mesh to retrieve Vertex positions
x2 = GetLockedMeshVertex(0,0,0); //Retrieve Vertex X
y2 = GetLockedMeshVertex(0,0,1); //Retrieve Vertex Y
z2 = GetLockedMeshVertex(0,0,2); //Retrieve Vertex Z
UnlockMesh(0); //Unlock so it can be used again

Vect1 = CreateVector(-1,x1,y1,z1); //Create Origin Position Vector
Vect2 = CreateVector(-1,x2,y2,z2); //Create Destination Position Vector
Vect3 = CalculateVectorDifference(-1,Vect1,Vect2); //Calculate Difference Vector

x3 = GetVector(Vect3,1); //Get X
y3 = GetVector(Vect3,2); //Get Y
z3 = GetVector(Vect3,3); //Get Z

ReleaseVector(Vect1); //Release Vectors
ReleaseVector(Vect2);
ReleaseVector(Vect3);

return (x3^2+y3^2+z3^2)^.5; //Calculate Distance
Have I done something wrong? (It might just be the part where I get the Vertex XYZ?), Also maybe I should have told you what I wanted this script for :P I'm trying to find the distance between the center of my "globe" (which is a GeoSphere exported from 3ds Max) to the first of the Vertexes (aka the Radius of the globe).

MysteriXYZ
 

Electricity
 
how would I calculate a new Vector giving a originXYZ, distance and Lat-/Longitude?

See the contents of the Move() script to calculate a direction vector pointing in the given direction, with a length equal to the given distance (which corresponds to the given speed in that script).
If you want to offset this vector (I think this is what you mean by "a originXYZ"), then you simply add those XYZ offsets to the corresponding components of the vector (you are effectively adding two vectors then).

Ah so I would just make a new script like this:
Code:
 
//Script that calculates the Vector product of the Origin Position Vector + the Direction Vector with Length.
//Dont forget to "ReleaseVector()" on the returned product!
//Usage: CalculateVectorPosDirLen(OriginX,OriginY,OriginZ,RotX,RotY,Length);
//
//x1,y1,z1 = Origin XYZ
//x2,y2,y3 = Direction Vector
var x1,y1,z1,x2,y2,z2,rotx1,roty1,length,Vect1,Vect2;

x1 = argument0;
y1 = argument1;
z1 = argument2;

Vect1 = CreateVector(-1,x1,y1,z1); //Create Origin Position Vector

rotx1 = argument3;
roty1 = argument4;
length = argument5;

x2 = sin(degtorad(roty1))*cos(degtorad(rotx1))*length; //Calculate Direction Vector X with Length
y2 = cos(degtorad(roty1))*cos(degtorad(rotx1))*length; //Calculate Direction Vector X with Length
x2 = sin(degtorad(rotx1))*length; //Calculate Direction Vector X with Length

Vect2 = CreateVector(-1,x2,y2,z2); //Create Direction Vector

return CalculateVectorSum(-1,Vect1,Vect2); //Return the sum of the Origin Vector and the Direction Vector
There I create the Position Vector for the Origin and the Direction Vector for the... direction... and I use theres also a length to the Direction Vector. These 2 Vectors are then added to each other and returned to the user ;) I hope I got it right (havent tested it yet).

MysteriXYZ
 

When working with abstract math objects like vectors, you should always try to visualize them; a vector can simply be imagined as an arrow.

So if you add two vectors together, it's like you stick the arrowhead of the first into the nock of the second one; then their sum is the arrow that you can place between the nock of the first vector and the arrowhead of the second one.
When you need to subtract the second vector, just reverse the direction of the corresponding arrow.

Yup, William Tell was shooting with vectors, and he didn't even realize it :P .
You really confused me with those arrows there... I have heard the expression "nocking" an arrow (I read alot of fantasy books in english even though I'm danish) but I never really knew where the "nock" part of an arrow was until I checked up on Wikipedia :P
Offline Profile Quote Post Goto Top
 
MysteriXYZ
Member Avatar
Master Matrix Masher
[ *  *  *  *  *  * ]
Electricity
Mar 1 2010, 12:46 AM
I seem to be getting some wrong results with this:

Globe Radius Script
Ahh, sorry, your question somehow made me think you wanted to know more about how vectors work on a low level, since I assumed you already knew about the built-in high-level math functions in U3D :P (although it never hurts to have a deeper understanding of these things).

To get the length of a vector, you can just use CalculateVectorLength(VectorID), while you can create a direction vector using CreateDirectionVector(OutputVectorID, Longitude, Latitude).
So yeah, you wrote a bit of unnecessary code I'm afraid :D .


The second (and most important) reason that you are doing some needless work, is that the retrieved vertex coordinates (x2, y2, z2) are not in world space, but in mesh space (relative to the model origin with coordinates (x1, y1, z1)). This means that these are already the ones you need to calculate the sphere radius (instead of (x3, y3, z3)). So there is no need to work with vectors at all in this case.
Just don't forget to take the scaling of the model into account, since this is done in world space, not in mesh space.


Apart from that, there is one common mistake in your code: the coordinates of a vertex are stored in the order [x, z, y], so you need to switch the indices for y2 and z2.

Try this version of the above script:
Code:
 
//Globe Radius Script
//Use only with Globe/Sphere/Ball Models!
//Usage: radius = GlobeRadius();
//
//x1,y1,z1 = Vertex XYZ
var x1,y1,z1;

LockMesh(0,0,0,0); //Lock mesh to retrieve Vertex positions
x1 = GetLockedMeshVertex(0,0,0); //Retrieve Vertex X
y1 = GetLockedMeshVertex(0,0,2); //Retrieve Vertex Y
z1 = GetLockedMeshVertex(0,0,1); //Retrieve Vertex Z
UnlockMesh(0); //Unlock so it can be used again

return scalx*(x1^2+y1^2+z1^2)^.5; //Calculate Distance


If possible, use only sphere models that have a radius equal to one and use scalx/y/z to scale them; then you can simply check the value of scalx instead of having to calculate anything.


Electricity
 
Ah so I would just make a new script like this:

Position Vector + Direction Vector Script

There I create the Position Vector for the Origin and the Direction Vector for the... direction... and I use theres also a length to the Direction Vector. These 2 Vectors are then added to each other and returned to the user ;) I hope I got it right (havent tested it yet).
Instead of z2, you are calculating x2 a second time ;) .

Here is a simpler version of that script, if you still need it:
Code:
 
//Script that calculates the Vector sum of the Origin Position Vector
//and the Direction Vector scaled by Length.
//Dont forget to "ReleaseVector()" on the returned sum!
//Usage: CalculateVectorPosDirLen(OriginX,OriginY,OriginZ,RotX,RotY,Length);
//
//x1,y1,z1 = Origin XYZ
//rotx1,roty1,length = Direction Vector
var x1,y1,z1,rotx1,roty1,length,Vect1,Vect2;

x1 = argument0;
y1 = argument1;
z1 = argument2;

Vect1 = CreateVector(-1,x1,y1,z1); //Create Origin Position Vector

rotx1 = argument3;
roty1 = argument4;
length = argument5;

Vect2 = CreateDirectionVector(-1,rotx1,roty1); //Create Direction Vector
CalculateVectorScalarProduct(Vect2,Vect2,length); //scale the Direction Vector

return CalculateVectorSum(-1,Vect1,Vect2); //Return the sum of the Origin Vector and the Direction Vector
U3D is like candy; after extensive consumption, it's Best to brush.
Offline Profile Quote Post Goto Top
 
Electricity
Chaos vs. Cosmos
[ *  *  *  * ]
Thank you so much MysteriXYZ, you sure have a better understanding of this than I do :D Anyway I did think that the Mesh's XYZ might be Mesh space and not World space but I was'nt sure about it. I must have misread the manual when I read about the GetLockedMeshVertex(..) function, thanks again for correcting me :thumb_up: Also I should have told you before what I actually wanted to do with this script and not just say "Find distance from vector to vector...". Once again thank you :)

Thanks for pointing out my typos aswell :P damn I suck at this ;)

Thanks for making me some Correct Scripts and for helping me out with this :thumb_up: If this makes it into my project I'll be sure to credit you under Special Thanks ;)
Offline Profile Quote Post Goto Top
 
« Previous Topic · Questions about Ultimate 3D · Next Topic »
Add Reply