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 from the side of a part accounting for rotation?

Previous Thread :: Next Thread 
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:03 PM
I need a function that lets me input a basePart and a Vector3 (Representing a position) and outputs how far away that Vector3 is from the front face of the part, I can not seem to figure a way that works with all rotations of the basePart.
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
09 May 2017 10:04 PM
use the dot product

Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
09 May 2017 10:07 PM
Heres an example:

( Target - Start ):Dot( Direction )

( B.Position - A.Position ):Dot( A.CFrame.lookVector )
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:10 PM
It doesn't work, any other ideas?

while (true) do
print((game.Workspace.point.Position - game.Workspace.base.Position):Dot(game.Workspace.base.CFrame.lookVector))
wait()
end
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
09 May 2017 10:12 PM
Wulf's recommendation of the dot product is more versatile, but for your very specific case of determining the distance from the center of the front face, you could say

((part.CFrame*CFrame.new(0,0,part.CFrame.lookVector*(part.Size.Z/2))).p-destination).magnitude

I'm assuming the lookVector is a unit vector that points from the center of the part to the center of the front face. I don't use it much :S


Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:14 PM
print((game.Workspace.point.Position - (game.Workspace.base.Position + (game.Workspace.base.CFrame.lookVector * (game.Workspace.base.Size.Z / 2)))):Dot(game.Workspace.base.CFrame.lookVector))

this works, should I use yours instead Soybeen? Which one do you guys think is more efficient, this function is gonna be called a lot, fast.
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
09 May 2017 10:19 PM
it works just fine for me

goo.gl/xao0TO
Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
09 May 2017 10:21 PM
mine is from the center in the direction of the face
his is from the center of the face in the direction of the face
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:26 PM
this is what it does when I don't make the changes

https://www.youtube.com/watch?v=Wq8IRxE84iI
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 10:31 PM
Wunder's suggestion misses the point. Using the Dot product here as he did is only going to tell you how far away Target is from Start in the direction of the lookVector. This is wrong especially when the part is rotated.

Here:
(vec3 - part.CFrame*Vector3.new(0,0,-part.Size.Z/2)).Magnitude
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:36 PM
@cntkillme
it works with all rotations when I make the changes I showed, so should I use my changes or what you posted
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 10:37 PM
now rotate the part and compare the results
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 10:38 PM
Here I'll make a little demo 1 sec
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:39 PM
I am rotating it, or rather welding the point part to it and dropping the part and letting it fling all over the place

I did just notice that if the point part is outside of the base part it doesn't work correctly but for my purposes the point part is always going to be inside the basepart, so which one will be more efficient
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:40 PM
Actually nevermind it looks like it does still work when not inside the part
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 10:44 PM
puu.sh/vLgb3/4ec151dc7e.mp4

The red part's length is using your method
The yellow part's length is using mine (which is correct as you can tell from the vid)

I positioned the yellow part just 1 stud higher than the red so you can see them both
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:52 PM
I'm unable to recreate that problem, is it possible for you to upload that example for me to look at?

I have to go to sleep now but I'll work some more on this project tomorrow so if you can upload it for me I would appreciate it
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 10:54 PM
puu.sh/vLgDF/df13310ba0.png
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
09 May 2017 10:58 PM
but you didn't make the changes that I made to the dot version
you just did what wunder posted

anyway I'll look at it all tomorrow and do some more tests maybe I'm missing something
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 11:00 PM
I did exactly what you did
You:
(game.Workspace.point.Position - (game.Workspace.base.Position + (game.Workspace.base.CFrame.lookVector * (game.Workspace.base.Size.Z / 2)))):Dot(game.Workspace.base.CFrame.lookVector)

"Simplified" to:
(point - baseFrontSurface):Dot(baseLookVector)

His:
(point - baseCenterPosition):Dot(baseLookVector)

Mine:
(point - baseFrontSurface):Dot(baseLookVector)
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
09 May 2017 11:01 PM
Mine being yours, but what you saw in my code
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
10 May 2017 04:35 AM
I did a bad job explaining what I wanted

I only wanted the distance on one axis
So if the distance changes on the X or Y axis I need it to not change the result

From what I have tested the dot thing seems to be what I want, I didn't explain well sorry
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
10 May 2017 09:36 AM
Oh. Lol
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