I need a way to check if an x,y coordinate is within a rotated rectangle.
The available data I have available of the rectangle is:
-x,y of the centre of the rectangle
-angle
-width and height
Now, I think that if I'm able to rotate the x,y coords I need to check and the rectangle, then I'll be able to find it out.
But how do I rotate these points by the angle?
Check if point is inside a rotated rectangle
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Check if point is inside a rotated rectangle
I have no finished code for your problem, but I hope the following can help.
If you want to calculate positions from a given angle you always need sine and cosine (math.sin(x) and math.cos(x))
Look at the vector (1,0) that is the vector pointing to the right, with length 1. If you want to rotate it by the angle a, it becomes (cos(a),sin(a)).
The vector (0,1) (pointing down) becomes (-sin(a),cos(a)).
That means a point (x,y) becomes after rotation (x*cos(a)-y(sin(a)), x*sin(a)+y*cos(a))
To find if a point is inside your rectangle, take the distance-vector from the rectangle center to this point and rotate it backward (by the angle -a). Then check if it is inside the corresponding unrotated rectangle.
If you want to calculate positions from a given angle you always need sine and cosine (math.sin(x) and math.cos(x))
Look at the vector (1,0) that is the vector pointing to the right, with length 1. If you want to rotate it by the angle a, it becomes (cos(a),sin(a)).
The vector (0,1) (pointing down) becomes (-sin(a),cos(a)).
That means a point (x,y) becomes after rotation (x*cos(a)-y(sin(a)), x*sin(a)+y*cos(a))
To find if a point is inside your rectangle, take the distance-vector from the rectangle center to this point and rotate it backward (by the angle -a). Then check if it is inside the corresponding unrotated rectangle.
Check out my blog on gamedev
Re: Check if point is inside a rotated rectangle
May be has something in it you can use but kind of shy after have been hammered on else were in the forum.
Might get some ideas.
Best
Might get some ideas.
Best
- Attachments
-
- IntersectingLines.love
- simple object contact
- (2.08 KiB) Downloaded 233 times
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Check if point is inside a rotated rectangle
Hi,
Using a rotation maxtrix, you can workout the new coordinates x',y' of a point x,y, rotated around point ox,oy by an angle a:
Apply this on all the four vertices to get the rotated rectangle vertices.
Then to test the collision with the point xp,yp, I think you can rotate this point by the same angle and perform a simple bound box collision check with the rotated rectangle.
Or, leave the point xp,yp as it is, and see this.
Hope it helps!
Using a rotation maxtrix, you can workout the new coordinates x',y' of a point x,y, rotated around point ox,oy by an angle a:
Code: Select all
function rotate(x,y,ox,oy,a)
return (x-ox)*math.cos(a) - (y-oy)*math.sin(a) + ox,
(x-ox)*math.sin(a) + (y-oy)*math.cos(a) + oy
end
Then to test the collision with the point xp,yp, I think you can rotate this point by the same angle and perform a simple bound box collision check with the rotated rectangle.
Or, leave the point xp,yp as it is, and see this.
Hope it helps!
Re: Check if point is inside a rotated rectangle
Thanks for all the help, I'll take a further look at it tonight and try to figure it out
Re: Check if point is inside a rotated rectangle
This is what I've used.
The test point was determined by the mouse position.
Just a leg up.
Best
The test point was determined by the mouse position.
Just a leg up.
Best
Code: Select all
-- /////////////////////////////////////////////////
-- ///// point_on_square - sum-of-areas methode ////
-- /////////////////////////////////////////////////
-- note: slower than Harden Collider
function point_on_square(tab) -- uses sum-of-areas approach
local test1 = point_on_triangle(tab[1],tab[2],tab[3])
local test2 = point_on_triangle(tab[3],tab[4],tab[1])
return test1 or test2
end
function point_on_triangle(A,B,C) -- if point not on triangle, area of sub-triangles > triangle area
local P = { mouseX, mouseY }
local area0=area( { A, B, C } )
local area1=area( { P, B, C } )
local area2=area( { A, P, C } )
local area3=area( { A, B, P } )
local sum = area1 + area2 + area3
local eps = 0.0001
return area0 > sum - eps and area0 <area0 + eps
end
function area( tri ) -- returns triangle area
local x1, y1 = tri[1][1], tri[1][2]
local x2, y2 = tri[2][1], tri[2][2]
local x3, y3 = tri[3][1], tri[3][2]
return math.abs( ( x1*y2 + x2*y3 + x3*y1 - x1*y3 - x3*y2 - x2*y1) / 2 )
end
-- //////////////////////////////////////////////////
-- //// point-in-square - Hardon Collider method ////
-- //////////////////////////////////////////////////
function point_in_square(tab) -- uses sum-of-areas approach
local test1 = point_in_triangle(tab[1],tab[2],tab[3])
local test2 = point_in_triangle(tab[3],tab[4],tab[1])
return test1 or test2
end
function point_in_triangle( a,b,c) -- Harden Collider approach
local p = {mouseX,mouseY}
return onSameSide(p,a, b,c) and onSameSide(p,b, a,c) and onSameSide(p,c, a,b)
end
function onSameSide(a,b, c,d)
local px, py = d[1]-c[1], d[2]-c[2]
local l = det(px,py, a[1]-c[1], a[2]-c[2])
local m = det(px,py, b[1]-c[1], b[2]-c[2])
return l*m >= 0 end
function det(x1,y1, x2,y2) return x1*y2 - y1*x2 end
-- //////////////////////////////
-- //// end point-in-square /////
-- //////////////////////////////
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests