After spending a bit of time with this I have spotted some problems with this implementation. I’m not sure whether it’s something I have done or haven’t done. I’ve implemented the bucket and inside I have two objects (a pentagon and a ball) to illustrate the problems:
Problem 1:
When I rotate the bucket to the right, both the pentagon and the ball move to the right as expected. At this point the bucket is tilted to the right. Now when I rotate the bucket back to the left, both objects begin to slide to the left but before the bucket has even tilted to the left. In fact they begin to slide left while the bucket is still tilted right. I know the gravity is ok because the ball will roll back to the right at this point if I stop the rotation while sill tilted to the right.
Here’s the code if anyone would like to try it and see:
local physics = require("physics") physics.start() physics.setScale( 60 ) physics.setGravity( 0, 9.8 ) -- initial gravity points downwards physics.setDrawMode( "hybrid" ) local bucket = display.newRect(0, 0, 100, 100) bucket.x = display.contentCenterX bucket.y = display.contentCenterY local bucket\_side\_r = { 130,0, 150,0, 150,200, 130,200 } local bucket\_bottom = { -150,195, 150,195, 150,215, -150,215 } local bucket\_side\_l = { -150,0, -130,0, -130,200, -150,200 } bucket\_density = 100.0 bucket\_friction = 100.0 bucket\_bounce = 0.0 physics.addBody( bucket, "static", { density=bucket\_density, friction=bucket\_friction, bounce=bucket\_bounce, shape=bucket\_side\_r}, { density=bucket\_density, friction=bucket\_friction, bounce=bucket\_bounce, shape=bucket\_bottom}, { density=bucket\_density, friction=bucket\_friction, bounce=bucket\_bounce, shape=bucket\_side\_l} ) local pentagon = display.newCircle(50, 200, 10) pentagonShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 } physics.addBody( pentagon, "dynamic", { density=100.0, friction=100.0, bounce=0.0, shape=pentagonShape } ) pentagon.isSleepingAllowed = false local ball = display.newCircle(50, 200, 10) ball:setFillColor(192, 99, 55) ball.isSleepingAllowed = false physics.addBody(ball, "dynamic", {density=100.0, friction=.3, bounce=.6, radius=10}) ball.isBullet = true local function rotateBucket(event) local t = event.target local phase = event.phase if (phase == "began") then display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position of finger 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 --rotate it 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 -- Stop further propagation of touch event return true end bucket:addEventListener("touch", rotateBucket)
Problem 2:
When I keep rotating the bucket the ball will eventually pass through the walls. I’ve spend a good while reading up on this and tried a lot of the suggestions but nothing seems to work. Many of the posts are old thought so I was just wondering if this there has been a fix for this since?
Things I’ve tried:
ball.isBullet = true
ball.isSleepingAllowed = false
physics.setPositionIterations(128)
physics.setVelocityIterations(128)
Slowing speed of rotation
Increasing densities
Nothing has solved the problem so far. Any help/advice for either problem would be greatly appreciated.
Thanks,
Jack