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 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10 myRoundedRect[iz]:rotate( 0 ) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz myRoundedRect[iz].name="wall"..iz myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=2 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz myRoundedRect[iz].name="wall"..iz myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=3 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*2) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz myRoundedRect[iz].name="wall"..iz myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=4 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2, display.contentHeight/2-100-10+225 myRoundedRect[iz]:rotate( -120\*3) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz myRoundedRect[iz].name="wall"..iz myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=5 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56+112 myRoundedRect[iz]:rotate( -120\*4) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz myRoundedRect[iz].name="wall"..iz myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) iz=6 myRoundedRect[iz] = display.newRoundedRect( 20, 5, 135, 3, 1 ) myRoundedRect[iz].anchorX = .5 myRoundedRect[iz].anchorY = .5 myRoundedRect[iz]:setFillColor( 0.5 ) myRoundedRect[iz].x, myRoundedRect[iz].y = display.contentWidth / 2+98-196, display.contentHeight/2-100-10+56 myRoundedRect[iz]:rotate( -120\*5) physics.addBody(myRoundedRect[iz], py, {density = 1.0, friction = 0, bounce = 1, isSensor = false}) myRoundedRect[iz].color=iz myRoundedRect[iz].name="wall"..iz myRoundedRect[iz].isSleepingAllowed = false lineGroup:insert(myRoundedRect[iz]) myRoundedRect[1]:setFillColor( 1, 1, 0 ) myRoundedRect[2]:setFillColor( 0, 1, 1 ) myRoundedRect[3]:setFillColor( 1, .0, 1 ) myRoundedRect[4]:setFillColor( 0, .0, 1 ) myRoundedRect[5]:setFillColor( 1, .0, 0 ) myRoundedRect[6]:setFillColor( 0, 1, 0 ) star={} i=1 for inx=1, 6 do star[inx] = display.newLine( cord[i],cord[i+1],cord[i+2],cord[i+3]) star[inx].alpha=0.3 lineGroup2:insert(star[inx]) --physics.addBody(star[inx], "static", {density = 1.0, friction = 0, bounce = 1, isSensor = false}) i=i+4 end lineGroup.anchorChildren = true lineGroup.isSleepingAllowed = false lineGroup.anchorX = .5 lineGroup.anchorY = .5 lineGroup.x=\_W/2 lineGroup.y=\_H/2 lineGroup2.anchorChildren = true lineGroup2.anchorX = .5 lineGroup2.anchorY = .5 lineGroup2.x=\_W/2 lineGroup2.y=\_H/2 lineGroup2.alpha=.5 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 = 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) - adjustment --myText.text = lineGroup.rotation end end ballrect = display.newRoundedRect( 30, 30, 30, 30, 1 ) ballrect.anchorX = .5 ballrect.anchorY = .5 ballrect:setFillColor( 0.5 ) ballrect.x, 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 ) 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 ) if ( obj1 == nil ) then -- Make sure the first object exists return false end if ( obj2 == nil ) then -- Make sure the other object exists return false end local right = obj1.x-delta \>= obj2.contentBounds.xMin and obj1.x-delta \<= obj2.contentBounds.xMax local up = obj1.y-delta \<= obj2.contentBounds.yMin and obj1.y-delta \>= obj2.contentBounds.yMin local down = obj1.y-delta \>= obj2.contentBounds.yMin and obj1.y-delta \<= obj2.contentBounds.yMax return (left or right) and (up or down) end local function gameLoop( event ) for i = 1,6 do if (hasCollided( ball, myRoundedRect[i]) ) then -- print("hitt"..myRoundedRect[i]) if ball.color==myRoundedRect[i].color then startscore=startscore+1 scorecenter.text = startscore else --foul end end end return true end Runtime:addEventListener( "enterFrame", gameLoop ) Runtime:addEventListener('touch', onTouch)