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: How to calculate the distance between two objects?

Previous Thread :: Next Thread 
Top_Click is not online. Top_Click
Joined: 15 Jul 2017
Total Posts: 47
01 Nov 2017 01:49 PM
So I have a tool and I want it's GripPos to be equal to another player's torso (so when the tool is equipped it's in another player's torso)
Since GripPos offsets where the tool is gripped, how would I calculate the distance between say the tool the player is holding and another player's torso? Then how would I put the distance into something like this:
tool.GripPos = Vector3.new(the distance between player's torso and tool original grip position)

im really stuck on how to do this, i appreciate any help!
Report Abuse
Lord_Narwhal is not online. Lord_Narwhal
Joined: 05 May 2012
Total Posts: 3242
01 Nov 2017 02:10 PM
distance is just (vector1-vector2).magnitude

it should be something like this for you but im not 100% sure i understand your question and i fear it may be more complicated then just this

local dist = (torso.Position-tool.GripPos).magnitude


Report Abuse
Top_Click is not online. Top_Click
Joined: 15 Jul 2017
Total Posts: 47
01 Nov 2017 02:13 PM
I think that's what I want, how do I make the grippos "dist" then?
is it just tool.GripPos = Vector3.new(dist)
Report Abuse
nextgenplatform is not online. nextgenplatform
Joined: 27 Feb 2017
Total Posts: 797
01 Nov 2017 02:15 PM
You would want the position
Report Abuse
InfiniteYield is not online. InfiniteYield
Joined: 01 Mar 2012
Total Posts: 762
01 Nov 2017 02:20 PM
local distance = (pos1 - pos2).magnitude


Report Abuse
LifeInDevelopment is not online. LifeInDevelopment
Joined: 10 Nov 2015
Total Posts: 670
01 Nov 2017 02:46 PM
Everyone else is telling you to use magnitude, but you can also get the position relative to the tool's position.

local gripPos = tool.CFrame:toObjectSpace(torsoCFrame).p
Report Abuse
D3D9 is not online. D3D9
Joined: 20 Aug 2016
Total Posts: 87
01 Nov 2017 02:47 PM
#code
local Position1 = Vector3.new(0, 0, 0);
local Position2 = Vector3.new(0, 1, 0);

local X = (Position2.X - Position1.X);
local Y = (Position2.Y - Position1.Y);
local Z = (Position2.Z - Position1.Z);

local Distance = math.sqrt((X ^ 2) + (Y ^ 2) + (Z ^ 2));

print(Distance);

I believe that's correct.


Report Abuse
LifeInDevelopment is not online. LifeInDevelopment
Joined: 10 Nov 2015
Total Posts: 670
01 Nov 2017 02:50 PM
Getting the hypotenuse of the two vectors via pythagoras from tip to tail also works, but .magnitude is a lot faster to do.
Report Abuse
D3D9 is not online. D3D9
Joined: 20 Aug 2016
Total Posts: 87
01 Nov 2017 03:04 PM
@Life

I'm very unfamiliar with Magnitude, having only used it once or twice. Could you explain how it's applied in ROBLOX? I've never quite understood.


Report Abuse
LifeInDevelopment is not online. LifeInDevelopment
Joined: 10 Nov 2015
Total Posts: 670
01 Nov 2017 03:11 PM
Well vector3s are the same as mathematical vectors, a common misconception among developers is that Vector3s represent coordinates but in fact that's not really true. Think of a vector3 as a line starting from the origin (0, 0, 0), its magnitude is the size of that vector which can be calculated by getting the hypotenuse of the vector when multiplying the x and y component, or you can use the .magnitude property to get a magnitude of a vector. Remember that a mathematical vector has a direction, and so do vector3s in roblox. You can get a vector's normalised (unit) direction by getting the .unit property. You can also find the magnitude between two vectors by subtracting a vector from another vector to get the vector that represents the difference between the two, and then get its magnitude by using .magnitude.
Report Abuse
D3D9 is not online. D3D9
Joined: 20 Aug 2016
Total Posts: 87
01 Nov 2017 03:14 PM
(Horrible doodle incoming)

So basically:

origin
-
-
-
vec3

So basically that "line" I made is what a vector3 really is? In essence, a vector3 is the end point of a line starting at the origin?


Report Abuse
LifeInDevelopment is not online. LifeInDevelopment
Joined: 10 Nov 2015
Total Posts: 670
01 Nov 2017 03:16 PM
Yeah, exactly. When you create (or construct) a vector 3, you're basically creating a line that starts from the origin and goes to wherever you specify.
Report Abuse
D3D9 is not online. D3D9
Joined: 20 Aug 2016
Total Posts: 87
01 Nov 2017 03:35 PM
Nice to know, thank you. Does the same apply to CFrame (Although CoordinateFrame makes me think otherwise..)?


Report Abuse
LifeInDevelopment is not online. LifeInDevelopment
Joined: 10 Nov 2015
Total Posts: 670
01 Nov 2017 04:26 PM
CFrame represents both the position of an part in physical space and its rotation. The first 3 arguments are for the position, and the other 9 are the 'rotation matrix'. The rotation matrix sets the unit vectors for the lookVector (where the part looks towards), the upVector (the vector that points upwards from a part) and rightVector (the vector that goes right from a part). You don't need to learn CFrame in detail since it is sort of 'advanced' but you can just set the parts cframe by doing this:

part.CFrame = CFrame.new(partVector3Pos, lookAtPos)

the line above will set the part's position to partVector3Pos, and the part's lookvector to lookAtPos so the part will look towards that position.
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