Hello, im having trouble updating the physics of my objects in my game.
There are four rectangles and they are inside a display group, i added some coding so I could rotate the objects when there was screen drag. But the physcs start in the correct position, and when the rotation happens the x and y of the physic box does move, but the rotation stays fixed.
Here is the picture before rotation:
And after rotating this happens:
Here is my code:
display.setStatusBar(display.HiddenStatusBar) local height = display.contentHeight local width = display.contentWidth local physics = require "physics" physics.start() physics.setDrawMode("hybrid") physics.setGravity(0, 0) bg = display.newRect(width/2, height/2, width\*2, height\*2) rect1 = display.newRect( width/3.5, height/2, width/20, width/5) rect2 = display.newRect( (width/3.5)\*2.5, height/2, width/20, width/5) rect3 = display.newRect( width/2, (height/10)\*2, width/5, width/20) rect4 = display.newRect(width/2, (height/10)\*8, width/5, width/20) bg:setFillColor(0,0,0) rect1:setFillColor(1,0,0) rect2:setFillColor(0,1,0) rect3:setFillColor(0,0,1) rect4:setFillColor(1,1,0) local bounceRect = display.newGroup() bounceRect:insert(bg) bounceRect:insert(rect1) bounceRect:insert(rect2) bounceRect:insert(rect3) bounceRect:insert(rect4) bounceRect.anchorChildren = true bounceRect.anchorX = 0.5 bounceRect.anchorY = 0.5 bounceRect.x = width/2 bounceRect.y = height/2 physics.addBody(rect1, "static") physics.addBody(rect2, "static") physics.addBody(rect3, "static") physics.addBody(rect4, "static") local function rotateSquares( event ) local t = event.target local phase = event.phase if(phase == "began") then display.getCurrentStage():setFocus(t) t.isFocus = true t.x1=event.x t.y1=event.y elseif t.isFocus then if(phase=="moved") then t.x2 = event.x t.y2 = event.y angle1 = 180/math.pi \* math.atan2(t.y1 - t.y , t.x1 - t.x) angle2 = 180/math.pi \* math.atan2(t.y2 - t.y , t.x2 - t.x) print("angle1 ="..angle1) rotationAmt = angle1 - angle2 t.rotation = t.rotation - rotationAmt print("t.rotation = "..t.rotation) t.x1 = t.x2 t.y1 = t.y2 elseif (phase=="ended")then display.getCurrentStage():setFocus(nil) t.isFocus = false end end return true end bounceRect:addEventListener("touch", rotateSquares)
I know moving the group is not the best way to do this, but i couln’t figure out how to rotate the objects if I didn’t create the group. Hope you can help