It looks like you're new here. If you want to get involved, click one of these buttons!
rect1 = Shape.new() rect1:setLineStyle(3, 0x000000) rect1:setFillStyle(Shape.SOLID, 0xff0000, 0.5) rect1:beginPath() rect1:moveTo(0, 0) rect1:lineTo(100, 0) rect1:lineTo(100, 50) rect1:lineTo(0, 50) rect1:closePath() rect1:endPath() rect1:setPosition(100, 100) rect1:setRotation(45) stage:addChild(rect1) rect2 = Shape.new() rect2:setLineStyle(3, 0x000000) rect2:setFillStyle(Shape.SOLID, 0xff0000, 0.5) rect2:beginPath() rect2:moveTo(0, 0) rect2:lineTo(100, 0) rect2:lineTo(100, 50) rect2:lineTo(0, 50) rect2:closePath() rect2:endPath() rect2:setPosition(200, 200) stage:addChild(rect2) function onMouseDown(self, event) if self:hitTestPoint(event.x, event.y) then self.isFocus = true self.x0 = event.x self.y0 = event.y event:stopPropagation() end end function onMouseMove(self, event) if self.isFocus then local dx = event.x - self.x0 local dy = event.y - self.y0 self:setX(self:getX() + dx) self:setY(self:getY() + dy) self.x0 = event.x self.y0 = event.y event:stopPropagation() end end function onMouseUp(self, event) if self.isFocus then self.isFocus = false event:stopPropagation() end end rect1:addEventListener(Event.MOUSE_DOWN, onMouseDown, rect1) rect1:addEventListener(Event.MOUSE_MOVE, onMouseMove, rect1) rect1:addEventListener(Event.MOUSE_UP, onMouseUp, rect1) function checkForIntersection(a, b) local function checkRotated(a, b) -- we put everything into a coordinate system where rectangle a is centered at the origin -- with no rotation. i.e., it's corners are (-a.w/2,-a.h/2) and (a.w/2,a.h/2) -- -- we'll need these a lot a_w2 = a:getWidth()/2.0 a_h2 = a:getHeight()/2.0 b_w2 = b:getWidth()/2.0 b_h2 = b:getHeight()/2.0 -- -- first we put the center of b into this system -- translate b_xc_tmp = (b:getX()+b_w2) - (a:getX()+a_w2) b_yc_tmp = (b:getY()+b_h2) - (a:getY()+a_h2) -- rotate by -a.theta c = math.cos(-a:getRotation()) s = math.sin(-a:getRotation()) b_xc = b_xc_tmp*c - b_yc_tmp*s b_yc = b_yc_tmp*c + b_xc_tmp*s -- -- next we compute each corner of rectangle b relative to (b_xc,b_yc) theta = b:getRotation() - a:getRotation() c = math.cos(theta) s = math.sin(theta) -- because we're rotating around the center, we have some symmetry b_x1 = b_w2*c - b_h2*s b_y1 = b_w2*s + b_h2*c b_x2 = b_w2*c + b_h2*s b_y2 = b_w2*s - b_h2*c -- find the bounding rectangle b_xmin = b_xc + math.min(b_x1, b_x2, -b_x1, -b_x2) b_xmax = b_xc + math.max(b_x1, b_x2, -b_x1, -b_x2) b_ymin = b_yc + math.min(b_y1, b_y2, -b_y1, -b_y2) b_ymax = b_yc + math.max(b_y1, b_y2, -b_y1, -b_y2) -- -- check for intersection with rectangle a return b_xmax < -a_w2 or b_xmin > a_w2 or b_ymax < -a_h2 or b_ymin > a_h2 end return not (checkRotated(a, b) or checkRotated(b, a)) end function onEnterFrame(self, event) myvar = checkForIntersection(rect1, rect2) if myvar then rect1:setColorTransform(0,139,0,1) else rect1:setColorTransform() end end stage:addEventListener(Event.ENTER_FRAME, onEnterFrame) |
Comments
Using the same code but without a rotation works fine, so I'm guessing that I'm not working with the correct points of my rotated rectangle??
If you want to rotate it by center, you should draw it like this:
As the code works in the another LUA SDK I guess I'm making an incorrect assumption with Gideros positioning and then calculating the corners of the rectangle.
I'm seeing the same problem with "collisions" occurring even though objects aren't overlapping.
In the following both .png are 60px x 60px
What am I doing wrong?
The problem is you call getWidth() and getHeight() after rotating the sprite. Therefore, they return bigger bounds. If you store the result of getWidth() and getHeight() before rotating and use the stored width/height, everything works well: