How do i work out the distance between a point and a rotated rectangle from the closest point ( using just maths ) i have access to all the part properties but its going to be ran thousands of times in less than a frame so it needs to be optimised
no not magnitude i mean from the closest point, of the rectangle that’s rotated and it needs to be just maths because it will be ran thousands of times in under a frame thanks anyway though
Do you want to know the point inside or outside the rectangle?
no im making a game with destruction and im making it as optimised as possible, i need to work out the distance between the a point, and a rotated square, yes magnitude will be used but first i need to find the point closest to the rotated square using just maths
Do you have a group of points and want to know the closest
no a single point and a single rectangle, but the rectangle is rotated to if for example the point is inside the cube but above it, but then is rotated 90 degrees it may no longer be inside the rectangle and i just want to know how to get the closest point on the rectangle to the point
thanks for the help but im looking for
basicly what is the distance from the closest point of that rectangle to the pointYou want the distance between the point and the edge of the rectangle
yeah the distance between the closest point of the edge of the rotated rectangle and the point
Sorry I’m on phone, but you might wanna research the Dot function of vector 3s.
I’ve never used then myself but I believe they’re used for getting the intersection point of two vectors.
Edit_
Better yet, the cross function.
Try this idea you might find useful
local Point = game.Workspace.Point
local Rectangle = game.Workspace.Rectangle
while true do
wait()
local ray = Ray.new(Point.CFrame.p, (Rectangle.Position - Point.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray)
local distance = (Point.CFrame.p - position).magnitude
print(distance)
end
good idea but a the parts only exist in mathematic form just simple values like a size, position, and maybe cframe. thanks anyway i think im close to the anwser, once i have the anwser ill close this thread or something and post the anwser so no one else has to go through this nightmare of finding this anwser
This becomes trivial when the rectangle is rotated; clamp the position of the point to the bounds of the rectangle. You probably already have code to do this.
To do that with a rotated rectangle, you can unrotate both the rectangle and the point, so that the problem is reduced to what I mentioned earlier.
Alternatively (and possibly even faster?), you may first get the direction vector of the walls of the rectangle, then use the dot product to find how far away or in the direction of the wall the point is. Which is also a way of getting the above result. It also sort of “unrotates” everything by just returning where the point is in the rectangle’s space, as if the rectangle were square and the world around it was rotated.
Note that the first method needs only an angle and uses trig functions for each point it needs to work with. But changing the angle is free.
The second function needs only one vector, which can be pre-calculated from the angle (with trig) each time the rectangle is rotated, but work with any amount of points afterward without trig. :Dot()
is just addition and multiplication
i forgot about this post i made, thanks i found a few solutions for myself as well ill make a post about it once i got it working