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 memberonly 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: 
Finding the distance between XYZ Vectors and calculating new Vectors with a given distance  

Topic Started: Feb 28 2010, 09:03 PM (3,481 Views)  
Electricity  Feb 28 2010, 09:03 PM Post #1 
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 a^{2}+b^{2}=c^{2} (Pythagoras)? 
MysteriXYZ  Feb 28 2010, 10:39 PM Post #2 
Master Matrix Masher

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=(x^{2}+y^{2}+z^{2})^{1/2}, where x, y and z are the resulting values.
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 . 
U3D is like candy; after extensive consumption, it's Best to brush.  
Electricity  Mar 1 2010, 12:46 AM Post #3 
Chaos vs. Cosmos

I seem to be getting some wrong results with this: 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 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).
Ah so I would just make a new script like this: 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). 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 
MysteriXYZ  Mar 1 2010, 03:07 PM Post #4 
Master Matrix Masher

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 builtin highlevel math functions in U3D (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 . 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:
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. Instead of z2, you are calculating x2 a second time . Here is a simpler version of that script, if you still need it:

U3D is like candy; after extensive consumption, it's Best to brush.  
Electricity  Mar 1 2010, 04:27 PM Post #5 
Chaos vs. Cosmos

Thank you so much MysteriXYZ, you sure have a better understanding of this than I do 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 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 damn I suck at this Thanks for making me some Correct Scripts and for helping me out with this If this makes it into my project I'll be sure to credit you under Special Thanks 
« Previous Topic · Questions about Ultimate 3D · Next Topic » 