#How would I get how far a part is from another part?
16 messages · Page 1 of 1 (latest)
its working as intended
if you want to detect if a model is touching something
use :GetPartBoundsInBox
and thats doing it
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/
what duh hell boy
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
yeah, thats what that formula does, try what robbe said.
Okay tbh Idk how tf I would use that function. Like how would I specify the part and the model? I don't understand 
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
we call it ✨math✨
Yeah well I'm shit at math, so themathclass is not my favorite
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
prints "-0" even tho it's not touching