#How would I get how far a part is from another part?

16 messages · Page 1 of 1 (latest)

green portal
#
local dist = (model.PrimaryPart.Position - placement.Plot.Position).Magnitude```does not work for me, as it will say how long the part is from the Plot's Position property, so only that specific Vector3, so even thought the model is touching the plot it will still not say 0, even thought I want it to. What should I do instead?
shut cairn
#

if you want to detect if a model is touching something

#

use :GetPartBoundsInBox

green portal
#

I don't tho

#

I want to know how far there is from the model from the part

toxic imp
# green portal ```lua local dist = (model.PrimaryPart.Position - placement.Plot.Position).Magni...
function sdBox( pos: vector3, size: Vector3 ) 
  local q = pos:Abs() - size;
  return (math.max(q,0.0)) + min(math.max(q.X,q.Y,q.Z), 0.0).Magnitude;
end
``` idk if that works
just ported it from glsl
it asumes that your part isnt rotated and is at pos 0,0,0
you can use Cframes to fix that
also its just between a cube and a point, but a bit of math can even fix that
https://iquilezles.org/articles/distfunctions/
shut cairn
#

what duh hell boy

green portal
# shut cairn and thats doing it

Not in the way I want it to. It is detecting how far the model is from the part's Position property, which is just one Vector3, which does not return the size of the plot or anything. So if the model is touching the plot at another place than that Vector3 from the Position property, it will not say 0

shut cairn
green portal
#

Okay tbh Idk how tf I would use that function. Like how would I specify the part and the model? I don't understand sad

toxic imp
# green portal Okay tbh Idk how tf I would use that function. Like how would I specify the part...

we call it ✨math✨
sometimes in programming, you cant just set two parts as parameter
you gotta be smart
instead of setting the part, you just set the size of the part (actually not the position)
then you do some other fancy math, also known as Matrix Multiplication (CFrames)
to translate your position in to a position relative to the parts Transform
and then do some other math, but thats not that hard, gimme a sec, imma try do do it

green portal
#

we call it ✨math✨
Yeah well I'm shit at math, so the math class is not my favorite sad

toxic imp
# green portal > we call it ✨math✨ Yeah well I'm shit at math, so the `math` class is not my...

i should go to bed...
its ugly and probably slow, but i think that works
idk

local sdBox: (Vector3, Vector3) -> number  = function(p, b)
    local q = p:Abs() - b;
    return q:Max(Vector3.zero).Magnitude + math.min(math.max(q.x,q.y,q.z),0.0);
end


function getDis(part1: BasePart, part2: BasePart) 
    
    local cf1 = part1.CFrame
    local cf2 = part2.CFrame
    
    local origin_dis = (cf1.Position - cf2.Position).Magnitude
    
    local dis1 = sdBox(cf2:Inverse() * cf1.Position, part2.Size/2)
    local dis2 = sdBox(cf1:Inverse() * cf2.Position, part1.Size/2)
    
    return -math.min(origin_dis - (dis1 + dis2), 0)
end
green portal
#

prints "-0" even tho it's not touching