|
| 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 |
|
|
| |
|
|
| 09 May 2017 10:07 PM |
Heres an example:
( Target - Start ):Dot( Direction )
( B.Position - A.Position ):Dot( A.CFrame.lookVector )
|
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 09 May 2017 10:19 PM |
it works just fine for me
goo.gl/xao0TO |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 May 2017 10:37 PM |
| now rotate the part and compare the results |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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 |
|
|
|
| 09 May 2017 10:40 PM |
| Actually nevermind it looks like it does still work when not inside the part |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 May 2017 10:54 PM |
| puu.sh/vLgDF/df13310ba0.png |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|