Hello,
I need some simple and fast system to detect if point is in square, and I came up with idea to use distance to measure and it works only if all sides are same (a=a)
how can I fix it and is there better way to get if point is in square?
script:
d1=(workspace.Part1.Position-workspace.Part4.Position).magnitude
d2=(workspace.Part3.Position-workspace.Part2.Position).magnitude
l1R=(workspace.Part1.Position-workspace.PartN.Position).magnitude
l2R=(workspace.Part2.Position-workspace.PartN.Position).magnitude
l3R=(workspace.Part3.Position-workspace.PartN.Position).magnitude
l4R=(workspace.Part4.Position-workspace.PartN.Position).magnitude
succesfull=0
R1=(workspace.Part1.Position-workspace.Part3.Position).magnitude
R2=(workspace.Part1.Position-workspace.Part2.Position).magnitude
R3=(workspace.Part3.Position-workspace.Part4.Position).magnitude
R4=(workspace.Part2.Position-workspace.Part4.Position).magnitude
local function round(n)
return math.floor(n + 0.5)
end
l1=round(l1R)
l2=round(l2R)
l3=round(l3R)
l4=round(l4R)
warn(l1,R1,R2)
warn(l2,R2,R1)
warn(l3,R3,R1)
warn(l4,R3,R4)
if l1>d1 or l1>d2 then
warn("out")
else
if l2>d1 or l2>d2 then
warn("out")
else
if l3>d1 or l3>d2 then
warn("out")
else
if l4>d1 or l4>d2 then
warn("out")
else
warn("in D range")
if R1-l1>=0 then
succesfull=succesfull+0.5
warn("+1 1/1")
end
if R2-l1>=0 then
succesfull=succesfull+0.5
warn("+1 1/2")
end
if R2-l2>=0 then
succesfull=succesfull+0.5
warn("+1 2/1")
end
if R2-l2>=0 or R1-l2>0 then
succesfull=succesfull+0.5
warn("+1 2/2")
end
if R3-l3>=0 then
succesfull=succesfull+0.5
warn("+1 3/1")
end
if R1-l3>=0 then
succesfull=succesfull+0.5
warn("+1 3/2")
end
if R3-l4>=0 then
succesfull=succesfull+0.5
warn("+1 4/1")
end
if R4-l4>=0 then
succesfull=succesfull+0.5
warn("+1 4/2")
end
end
end
end
end
warn( succesfull )
if succesfull>=3 then
warn("in")
workspace.PartN.BrickColor=BrickColor.new("Really blue")
else
workspace.PartN.BrickColor=BrickColor.new("Really red")
end
To check if a point is inside of a square you could be using Region3 instead of all of this. Not sure on efficiency differences, but from an organization standpoint you’ll be doing yourself a favor.
Essentially, the Region3 will take two vector points, and build a square/cube region out of them. Once you understand how to build a region, this thread will get you through checking if the part is inside of it (the thread’s answer checks if a Vector3 exists inside of the region, this would of course be your blue part’s position).
Instead of having 4 separate bricks. Have a single brick shaped as a square be the point to compare against.
If that square rotates, you can use a Region3 calculation from the square’s CFrame information. Which will provide the correct rotation for the positioning of your Region3 calculation.
The Region3 positions would be: (Square's position - left, Square's position + right)
This calculation’s very small. It will barely affect performance.
If you do want to avoid objects, you’ll have to track the position and rotation of whatever points are in question. Then, you’d have to create a method that does a square to square collision check (with rotations as well).
Essentially, you’d have to get all corners of PointB and check if PointA has any of its corners outside of PointB
You need to get all 4 corner points for square A and square B (With the rotations). Then, you’d need to implement math to check if square A’s points all fit inside of square B’s points.
The math gets complicated with rotations, since it starts becoming more of a triangular calculation, meaning you’ll need to implement some trigonometry. There are formulas for this type of stuff that you can find online.