Bouncing ball game error - physics

Hi All,

Been trying to make a bouncing game. Like this one . But there a a problem in the execution as the physics body goes for a wack when trying to rotate the hexagon. Can anyone help?

Thanks

local physics = require("physics") physics.start( noSleep ) physics.setGravity(0, 0) physics.setContinuous( enabled ) --physics.setVelocityIterations( 6 ) physics.setDrawMode( "hybrid" ) colorrange={} colorrange[1]= { 1, 1, 0 } colorrange[2]= { 0, 1, 1 } colorrange[3]= { 1, .0, 1 } colorrange[4]= { 0, .0, 1 } colorrange[5]= { 1, .0, 0 } colorrange[6]= { 0, 1, 0} py="static" \_W = display.contentWidth; \_H = display.contentHeight; local lineGroup = display.newGroup() local lineGroup2 = display.newGroup() cord={} cord={265,87,135,87, 135,87,70,200, 70,200,135,313, 135,313,265,313, 265,313,330,200, 330,200,265,87} startscore=0 local scorecenter = display.newText( startscore, \_W/2, \_H/2, native.systemFont, 45 ) scorecenter.alpha=.35 scorecenter.align="center" myRoundedRect={} iz=1 &nbsp;myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) &nbsp;myRoundedRect[iz].anchorX = .5 &nbsp;myRoundedRect[iz].anchorY = .5 &nbsp;myRoundedRect[iz]:setFillColor( 0.5 ) &nbsp;myRoundedRect[iz].x, &nbsp;myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10 myRoundedRect[iz]:rotate( 0 ) &nbsp; physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz&nbsp; myRoundedRect[iz].name="wall"..iz&nbsp; myRoundedRect[iz].isSleepingAllowed = false &nbsp; lineGroup:insert(myRoundedRect[iz]) &nbsp;&nbsp; &nbsp;iz=2 &nbsp;myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) &nbsp;myRoundedRect[iz].anchorX = .5 &nbsp;myRoundedRect[iz].anchorY = .5 &nbsp;myRoundedRect[iz]:setFillColor( 0.5 ) &nbsp;myRoundedRect[iz].x, &nbsp;myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120) &nbsp; physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz&nbsp; myRoundedRect[iz].name="wall"..iz&nbsp; myRoundedRect[iz].isSleepingAllowed = false &nbsp;lineGroup:insert(myRoundedRect[iz]) &nbsp;&nbsp; &nbsp; iz=3 &nbsp;myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) &nbsp;myRoundedRect[iz].anchorX = .5 &nbsp;myRoundedRect[iz].anchorY = .5 &nbsp;myRoundedRect[iz]:setFillColor( 0.5 ) &nbsp;myRoundedRect[iz].x, &nbsp;myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*2) &nbsp; physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) &nbsp;myRoundedRect[iz].color=iz&nbsp; &nbsp;myRoundedRect[iz].name="wall"..iz&nbsp; &nbsp;myRoundedRect[iz].isSleepingAllowed = false &nbsp; lineGroup:insert(myRoundedRect[iz]) &nbsp;&nbsp; &nbsp; &nbsp;iz=4 &nbsp;myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) &nbsp;myRoundedRect[iz].anchorX = .5 &nbsp;myRoundedRect[iz].anchorY = .5 &nbsp;myRoundedRect[iz]:setFillColor( 0.5 ) &nbsp;myRoundedRect[iz].x, &nbsp;myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10+225 myRoundedRect[iz]:rotate( -120\*3) &nbsp; physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) &nbsp;myRoundedRect[iz].color=iz&nbsp; &nbsp;myRoundedRect[iz].name="wall"..iz&nbsp; &nbsp;myRoundedRect[iz].isSleepingAllowed = false &nbsp; lineGroup:insert(myRoundedRect[iz]) &nbsp;&nbsp; &nbsp; &nbsp;iz=5 &nbsp;myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) &nbsp;myRoundedRect[iz].anchorX = .5 &nbsp;myRoundedRect[iz].anchorY = .5 &nbsp;myRoundedRect[iz]:setFillColor( 0.5 ) &nbsp;myRoundedRect[iz].x, &nbsp;myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*4) &nbsp; physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) &nbsp;myRoundedRect[iz].color=iz&nbsp; &nbsp;myRoundedRect[iz].name="wall"..iz&nbsp; &nbsp;myRoundedRect[iz].isSleepingAllowed = false &nbsp; lineGroup:insert(myRoundedRect[iz]) &nbsp;&nbsp; &nbsp; iz=6 &nbsp;myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) &nbsp;myRoundedRect[iz].anchorX = .5 &nbsp;myRoundedRect[iz].anchorY = .5 &nbsp;myRoundedRect[iz]:setFillColor( 0.5 ) &nbsp;myRoundedRect[iz].x, &nbsp;myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120\*5) &nbsp;physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) &nbsp; &nbsp;myRoundedRect[iz].color=iz&nbsp; &nbsp; &nbsp;myRoundedRect[iz].name="wall"..iz&nbsp; &nbsp; &nbsp;myRoundedRect[iz].isSleepingAllowed = false &nbsp; &nbsp;lineGroup:insert(myRoundedRect[iz]) &nbsp;&nbsp; &nbsp; myRoundedRect[1]:setFillColor( 1, 1, 0 ) &nbsp; myRoundedRect[2]:setFillColor( 0, 1, 1 ) &nbsp; myRoundedRect[3]:setFillColor( 1, .0, 1 ) &nbsp; myRoundedRect[4]:setFillColor( 0, .0, 1 ) &nbsp; myRoundedRect[5]:setFillColor( 1, .0, 0 ) &nbsp; myRoundedRect[6]:setFillColor( 0, 1, 0 ) star={} i=1 for inx=1, 6 do &nbsp;star[inx] = display.newLine( cord[i],cord[i+1],cord[i+2],cord[i+3]) &nbsp;star[inx].alpha=0.3 &nbsp;lineGroup2:insert(star[inx]) &nbsp;--physics.addBody(star[inx], "static", {density = 1.0, friction = 0, bounce = 1, isSensor = false}) &nbsp;i=i+4 &nbsp;end lineGroup.anchorChildren = true &nbsp; &nbsp;lineGroup.isSleepingAllowed = false &nbsp;lineGroup.anchorX = .5 lineGroup.anchorY = .5 &nbsp;lineGroup.x=\_W/2 &nbsp;lineGroup.y=\_H/2 &nbsp;lineGroup2.anchorChildren = true &nbsp;lineGroup2.anchorX = .5 lineGroup2.anchorY = .5 &nbsp;lineGroup2.x=\_W/2 &nbsp;lineGroup2.y=\_H/2 &nbsp;lineGroup2.alpha=.5 &nbsp;local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - lineGroup.x local dy = e.y - lineGroup.y adjustment = &nbsp; math.atan2(dy,dx) \* 180 / PI - lineGroup.rotation end if(e.phase == "moved") then local dx = e.x - lineGroup.x local dy = e.y - lineGroup.y lineGroup.rotation = (math.atan2(dy,dx) \* 180 / PI) &nbsp;- adjustment --myText.text = lineGroup.rotation end end ballrect = display.newRoundedRect( 30, 30, 30, 30, 1 ) ballrect.anchorX = .5 ballrect.anchorY = .5 &nbsp;ballrect:setFillColor( 0.5 ) &nbsp;ballrect.x, &nbsp;ballrect.y = display.contentWidth / 2, display.contentHeight/2 ballrect.alpha=0 ballspeed=50 -- create a ball and set it in motion ball = display.newCircle( 0, 0, 15 ) &nbsp;ball.anchorX = .5 ball.anchorY = .5 ball.x, ball.y = display.contentWidth / 2, display.contentHeight/2 physics.addBody(ball, "dynamic", {density = 1, friction = 0, bounce = 1, isSensor = false, radius = 15}) ball.isBullet = true ball:applyForce(ballspeed, 10) ball.color=3 ball.name="ball" ball:setFillColor( colorrange[ball.color][1], colorrange[ball.color][2], colorrange[ball.color][3] ) ball.isSleepingAllowed = false delta=26 -- Rectangle-based collision detection local function hasCollided( obj1, obj2 ) &nbsp; &nbsp; if ( obj1 == nil ) then &nbsp;-- Make sure the first object exists &nbsp; &nbsp; &nbsp; &nbsp; return false &nbsp; &nbsp; end &nbsp; &nbsp; if ( obj2 == nil ) then &nbsp;-- Make sure the other object exists &nbsp; &nbsp; &nbsp; &nbsp; return false &nbsp; &nbsp; end &nbsp; &nbsp; local right = obj1.x-delta \>= obj2.contentBounds.xMin and obj1.x-delta \<= obj2.contentBounds.xMax &nbsp; &nbsp; local up = obj1.y-delta \<= obj2.contentBounds.yMin and obj1.y-delta \>= obj2.contentBounds.yMin &nbsp; &nbsp; local down = obj1.y-delta \>= obj2.contentBounds.yMin and obj1.y-delta \<= obj2.contentBounds.yMax &nbsp; &nbsp; return (left or right) and (up or down) end local function gameLoop( event ) &nbsp; &nbsp; for i = 1,6 do &nbsp; &nbsp; &nbsp; &nbsp; if (hasCollided( ball, &nbsp;myRoundedRect[i]) ) then &nbsp; &nbsp; &nbsp; &nbsp; -- print("hitt"..myRoundedRect[i]) if ball.color==myRoundedRect[i].color then &nbsp;startscore=startscore+1 &nbsp; scorecenter.text = startscore &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; else &nbsp; --foul &nbsp;&nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp; return true end Runtime:addEventListener( "enterFrame", gameLoop ) Runtime:addEventListener('touch', onTouch)

lineGroup.rotation = (math.atan2(dy,dx) * 180 / PI)  - adjustment

i’ve not tried to run your code, but this part looks suspicious - physics bodies do not rotate (or translate, or scale) in physics-world-coordinates when their containing group is rotated (or translated, or scaled) in screen-coordinates - group transforms are “visual-only” (and can also lead to an out of sync “hybrid” display, which i see you’re also using)

Are you saying they never rotate - or only when part of the group??  I may be experiencing a similar issue.  Thanks!

Hi Dave, thank you for the reply. Yes I also think that is the problem…but I am not sure know how to use physics to rotate the hexagon  (with multi object) using physics

I am able to rotate the group…but the physics of the group goes out of sync when I try to rotate the object…As dave said the problem is that I am using rotation (non-physics action) to rotate the hexagon…but I am not sure how to rotate using physics

i’d suggest doing the rotation WITH physics.  have some ghost object (ie a sensor) at the center, with a motorized pivot joint or as a kinematic with angular velocity, attaching your hex “walls” to it – either with other welded ghost “spoke” bodies, or a big giant circular kinematic ghost “plate” underneath welded, or just the joint itself as the connection, or… there are numerous ways you might set this up, but the idea is to have a single central physics-controlled rotator, may need to test various setups for “stability” before deciding.  (the alternative is a bit of trig to manually rotate)

lineGroup.rotation = (math.atan2(dy,dx) * 180 / PI)  - adjustment

i’ve not tried to run your code, but this part looks suspicious - physics bodies do not rotate (or translate, or scale) in physics-world-coordinates when their containing group is rotated (or translated, or scaled) in screen-coordinates - group transforms are “visual-only” (and can also lead to an out of sync “hybrid” display, which i see you’re also using)

Are you saying they never rotate - or only when part of the group??  I may be experiencing a similar issue.  Thanks!

Hi Dave, thank you for the reply. Yes I also think that is the problem…but I am not sure know how to use physics to rotate the hexagon  (with multi object) using physics

I am able to rotate the group…but the physics of the group goes out of sync when I try to rotate the object…As dave said the problem is that I am using rotation (non-physics action) to rotate the hexagon…but I am not sure how to rotate using physics

i’d suggest doing the rotation WITH physics.  have some ghost object (ie a sensor) at the center, with a motorized pivot joint or as a kinematic with angular velocity, attaching your hex “walls” to it – either with other welded ghost “spoke” bodies, or a big giant circular kinematic ghost “plate” underneath welded, or just the joint itself as the connection, or… there are numerous ways you might set this up, but the idea is to have a single central physics-controlled rotator, may need to test various setups for “stability” before deciding.  (the alternative is a bit of trig to manually rotate)