generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Distance between 2 3D points.

Previous Thread :: Next Thread 
ghostleader is not online. ghostleader
Joined: 16 Feb 2008
Total Posts: 1131
16 Dec 2012 06:40 PM
ok say i have
Point1=Vector3.new(10, 50, 10)
Point2=Vector3.new(-10, 40, 10)

How could i get the distance between them?

Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
16 Dec 2012 06:41 PM
.magnitude
Report Abuse
Riderj is not online. Riderj
Joined: 15 Aug 2011
Total Posts: 1534
16 Dec 2012 06:42 PM
(Pont2-Point1).magnitude



TooquicktorespondandforgotdetailssonowImfacedwiththefloodcheck.
Report Abuse
ghostleader is not online. ghostleader
Joined: 16 Feb 2008
Total Posts: 1131
16 Dec 2012 06:44 PM
Hmmm ill try.
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
16 Dec 2012 06:49 PM
it can be either (p1 - p2).magnitude or (p2 - p1).magnitude... due to the nature of the distance formula, this holds true for all p1's and p2's.

¬ LuaLearners Elite/Teacher(+3) ♣ SHG Scripter Tier-2
Report Abuse
ghostleader is not online. ghostleader
Joined: 16 Feb 2008
Total Posts: 1131
16 Dec 2012 06:51 PM
lol i did it more complicated, and it works. Idk why i asked.

teleporting=true

local player = game.Players.LocalPlayer
if player == nil or player.Character == nil then return end
local char = player.Character.Torso
maxDistance= level * 1.5

p1=math.pow((pos.X - char.Position.X), 2)
p2=math.pow((pos.Z - char.Position.Z), 2)
p3=math.pow((pos.Y - char.Position.Y), 2)
distance=math.sqrt(p1+p2+p3)
print(distance)

if distance > maxDistance then
teleporting = false
return
end
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
16 Dec 2012 06:56 PM
I did the same thing when I was beginning to script :)

Just use distance=(pos-char.Position).magnitude

.. And this should go in SH
Report Abuse
Aerideyn is not online. Aerideyn
Joined: 16 Jan 2010
Total Posts: 1882
16 Dec 2012 07:24 PM
As above - you can just use magnitude

Distance = (P1 - P2).magnitude


But since you wanted to do it manually (and said you didn't understand why.. ) Here is why it works:

the length of a triangle's hypotenuse (long side) is given by Pythagoras's theorem A^2 + B^2 = C^2
this can be extended to 3d quite easily..
A^2 + B^2 + C^2 = D^2

Where A B and C/(D) are the length's of each sides of the triangle.
This formula only works on right angle triangles.
How does that help?

We will do this in 2d since its easier to show, but basically we find the lengths of the straight sides of the triangles and then use the formula above to get the length of the longest side:
P1 and P2 are the two points you want to measure the distance between:


------------------
----------P2-----
------------------
------------------
------------------
------------------
------------------
--P1-------------
------------------

since we can only use this formula on a right angle triangle, we need to build a triangle with straight sides.
This means we need to isolate and get the length of each axis separately.

Now imagine we make a new point, this point is the same as P2 except for a Z value we subtract P1's Z value from P2's Z value so we can isolate the distance only along the X axis. We will call this new point X.
what we end up with is just the distance along the X axis - Lets draw a line between P2 and X like this:

------------------
---X______P2----
------------------
------------------
------------------
------------------
------------------
--P1-------------
------------------

Then we do the exact same again except this time for the Z axis:

------------------
---X______P2----
---|--------------
---|--------------
---|--------------
---|--------------
---|--------------
--P1-------------
------------------

From here it is quite easy to see we can make a triangle:

------------------
---X______P2----
---|-------/---------
---|-----/----------
---|----/-----------
---|--/------------
---|-/-------------
--P1-------------
------------------

where the long side of the triangle we can see is the shortest path between the two points.
Since we know the lengths of the two sides of the triangle we can find the length of the long side using the formula above.

The principle works the exact same in 3d - it's just a 3d triangle. (looks like a quarter of a pyramid) So ill just add in the Y values into the formula:



.......A^2...........+.........B^2.........+.........C^2.........= D^2
(P2.X - P1.X)^2 + (P2.Y - P1.Y)^2 + (P2.Z - P1.Z)^2 = Distance^2

Therefore if we square root both sides the formula becomes:

Distance = math.sqrt((P2.X - P1.X)^2 + (P2.Y - P1.Y)^2 + (P2.Z - P1.Z)^2)



Phew so there you go. you can probably find easier to follow examples on the internet - just search for
"distance formula"

this is exactly what .magnitude does "under the hood" so i do very much suggest you use that instead - but it never hurts to know how things really work.
Report Abuse
BrandonFireflower is not online. BrandonFireflower
Joined: 09 Feb 2010
Total Posts: 731
16 Dec 2012 07:26 PM
Don't you see? .magnitude *IS* the distance formula, which is sqrt((xOne-xTwo)^2+(yOne-yTwo)^2+(zOne-zTwo)^2)

When you do (p1-p2) it compares it to the origin. Essentially the (p1-p2) puts it into object coordinates instead of world coords.

local distance=(p1-p2).magnitude

==

local distance=math.sqrt((pos1.x-pos2.x)^2+(pos1.y-pos2.y)^2+(pos1.z-pos2.z)^2)
Report Abuse
BrandonFireflower is not online. BrandonFireflower
Joined: 09 Feb 2010
Total Posts: 731
16 Dec 2012 07:27 PM
ninja'd
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image