klaw Posted May 22, 2017 Share Posted May 22, 2017 Does anybody know how I can check if a rectangle (x, y, width, height) is touching another rectangle (x, y, width, height) on the screen? I have two DX rendered rectangles and want to check if they're touching, so I can change the colour of them if they are. Link to comment
Jayceon Posted May 22, 2017 Share Posted May 22, 2017 -- if doesCollide(firstRectangleX, firstRectangleY, firstRectangleWidth, firstRectangleHeight, secondRectangleX,secondRectangleY, secondRectangleWidth, secondRectangleHeight) then ... function doesCollide(x1, y1, w1, h1, x2, y2, w2, h2) local horizontal = (x1 < x2) ~= (x1 < x2 + w2) or (x1 + w1 < x2) ~= (x1 + w1 < x2 + w2) or (x1 < x2) ~= (x1 + w1 < x2) or (x1 < x2 + w2) ~= (x1 + w1 < x2 + w2) local vertical = (y1 < y2) ~= (y1 < y2 + h2) or (y1 + h1 < y2) ~= (y1 + h1 < y2 + h2) or (y1 < y2) ~= (y1 + h1 < y2) or (y1 < y2 + h2) ~= (y1 + h1 < y2 + h2) return (horizontal and vertical) end 1 Link to comment
Moderators IIYAMA Posted May 22, 2017 Moderators Share Posted May 22, 2017 (edited) local rectangleX1, rectangleY1 = 20, -48 local rectangleX2, rectangleY2 = 60, -5 local rectangleSizeX, rectangleSizeY = 50, 50 function isRectangleInRectangle (X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2) if (X1 > X2 and Y1 > Y2 and X1 < X2 + sizeX2 and Y1 < Y2 + sizeY2) or (X1 + sizeX1 < X2 + sizeX2 and Y1 + sizeY1 < Y2 + sizeY2 and X1 + sizeX1 > X2 and Y1 + sizeY1 > Y2) then return true end return false end iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY))) try this, untested. It will check if the TOP+LEFT or BOTTOM+RIGHT is inside the other rectangle. (This doesn't work when you rotate the rectangle) Edited May 22, 2017 by IIYAMA Link to comment
coNolel Posted May 22, 2017 Share Posted May 22, 2017 3 minutes ago, IIYAMA said: local rectangleX1, rectangleY1 = 20, -48 local rectangleX2, rectangleY2 = 60, -5 local rectangleSizeX, rectangleSizeY = 50, 50 function isRectangleInRectangle (X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2) if (X1 > X2 and Y1 > Y2 and X1 < X2 + sizeX2 and Y1 < Y2 + sizeY2) or (X1 + sizeX1 < X2 + sizeX2 and Y1 + sizeY1 < Y2 + sizeY2 and X1 + sizeX1 > X2 and Y1 + sizeY1 > Y2) then return true end return false end iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY))) try this, untested. It will check if the TOP+LEFT or BOTTOM+RIGHT is inside the other rectangle. (This doesn't work when you rotate the rectangle) can you tell me how can i rotate a rectangle ? " not talking about dxDrawImage " but dxDrawRectangle, Link to comment
klaw Posted May 22, 2017 Author Share Posted May 22, 2017 58 minutes ago, IIYAMA said: local rectangleX1, rectangleY1 = 20, -48 local rectangleX2, rectangleY2 = 60, -5 local rectangleSizeX, rectangleSizeY = 50, 50 function isRectangleInRectangle (X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2) if (X1 > X2 and Y1 > Y2 and X1 < X2 + sizeX2 and Y1 < Y2 + sizeY2) or (X1 + sizeX1 < X2 + sizeX2 and Y1 + sizeY1 < Y2 + sizeY2 and X1 + sizeX1 > X2 and Y1 + sizeY1 > Y2) then return true end return false end iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY))) try this, untested. It will check if the TOP+LEFT or BOTTOM+RIGHT is inside the other rectangle. (This doesn't work when you rotate the rectangle) The one Jayceon posted didn't work. Also, I've tried top, left, bottom and right but it doesn't do what I want it to do. I need the sides too. Link to comment
Moderators IIYAMA Posted May 22, 2017 Moderators Share Posted May 22, 2017 sides should be included afaik, my bad for forgetting to mention that. It is untested, so test it with all possible combinations you can come up with. Link to comment
Moderators IIYAMA Posted May 22, 2017 Moderators Share Posted May 22, 2017 nvm you are right. I think I am a bit tired, that must be the problem. I will solve it for you tomorrow if nobody already solved it for you. Link to comment
Moderators IIYAMA Posted May 23, 2017 Moderators Share Posted May 23, 2017 (edited) I came up with an easier concept for it. This will compare the centres of the rectangles with each other, which only requires 4 conditions. local rectangleX1, rectangleY1 = 25, 0 local rectangleX2, rectangleY2 = -25, 10 local rectangleSizeX, rectangleSizeY = 50, 50 function isRectangleInRectangle(X1, Y1, sizeX1, sizeY1, X2, Y2, sizeX2, sizeY2) local centreX1, centreY1 = X1 + sizeX1 / 2, Y1 + sizeY1 / 2 local centreX2, centreY2 = X2 + sizeX2 / 2, Y2 + sizeY2 / 2 local distanceX = centreX1 - centreX2 local distanceY = centreY1 - centreY2 if distanceX < 0 then distanceX = -distanceX end if distanceY < 0 then distanceY = -distanceY end if distanceX <= sizeX1 / 2 + sizeX2 / 2 and distanceY <= sizeY1 / 2 + sizeY2 / 2 then return true end return false end iprint(tostring(isRectangleInRectangle(rectangleX1, rectangleY1, rectangleSizeX, rectangleSizeY, rectangleX2, rectangleY2, rectangleSizeX, rectangleSizeY))) Edited May 23, 2017 by IIYAMA Link to comment
Jayceon Posted May 23, 2017 Share Posted May 23, 2017 Hmm.. My code is work very well for myself. Link to comment
Moderators IIYAMA Posted May 25, 2017 Moderators Share Posted May 25, 2017 You might want to reply @klaw , it is a bit rude if you do not. Link to comment
klaw Posted May 26, 2017 Author Share Posted May 26, 2017 On 2017-5-25 at 16:04, IIYAMA said: You might want to reply @klaw , it is a bit rude if you do not. Sorry, yes. I've figured it out. Apologies to @Jayceon, your code was perfect, I just copied it incorrectly. Thank you. 2 Link to comment
klaw Posted May 28, 2017 Author Share Posted May 28, 2017 It seems that using all 4 corners doesn't accomplish what I need, @Jayceon, because there are some occurrences when the two rectangles are touching but none of the corners are conflicting. For example: https://gyazo.com/981ce6b1691e68c833ba4934a3216979 As you can see in the diagram, the two rectangles are touching but none of the corners are within each other. Appreciate any further help. Link to comment
Moderators IIYAMA Posted May 29, 2017 Moderators Share Posted May 29, 2017 My last code post will help you out with that problem, since it isn't corner based but distance based. 1 Link to comment
klaw Posted May 29, 2017 Author Share Posted May 29, 2017 3 hours ago, IIYAMA said: My last code post will help you out with that problem, since it isn't corner based but distance based. Oh! Didn't see that, sorry! But thanks, it works perfect. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now