Hi All,
I am writing a game that has two stages, design and play. In the design stage the user can move around objects that get put into action when going into play mode.
In the design mode the user moves around standard (non-physics) game pieces, that get removed and created as physics objects when in play mode. This all works perfectly except there is a slight movement (rotation) of the objects when I start the physics engine (from pause).
I played around with physics.stop, which solves this issue but created a whole heap of others (see https://forums.coronalabs.com/topic/61040-sporadic-error/)..)
I have decided to use physics.pause() instead, I have done a lot of research, and followed different peoples advice, the best solution I have come up with is to add a weld joint to a static object when I create the object, and the straight away remove it, this seems to reduce the movement down to a very little sideways movement (maybe 2 pixels) which I can live with, but is still not ideal. The code below is a very simplified case to demonstrate the issue. If you comment out the 3 lines related to the weldJoint you will see the original issue I was dealing with.
Is there another approach I can try or should I just run with this?
local centerX = display.contentCenterX local centerY = display.contentCenterY local fullh = display.contentHeight local fullw = display.contentWidth local physics = require("physics") physics.start() display.setStatusBar( display.HiddenStatusBar ) local bkg = display.newRect(centerX, centerY, fullw, fullh ) bkg:setFillColor(0,0,.5) local plank -- forward reference to plank local lock = display.newRect(0, 0, 100, 100 ) physics.addBody( lock, "static", { density = 10, bounce = 0, friction = 2}) local function drawPlank(event) if event.phase == "began" then if plank and plank.removeSelf ~= nil then print("removing") plank:removeSelf( ) plank = nil physics.pause() end plank = display.newRect( 0, 0, 200, 33 ) plank.anchorX, plank.anchorY = 0.5, 0.5 plank.x, plank.y = centerX , centerY plank.rotation = 90 physics.addBody( plank, "dynamic", { density = 10, bounce = 0, friction = 2}) --plank:setLinearVelocity( 0, 0 ) -- no effect --plank:applyTorque( 0 ) -- no effect local weldJoint = physics.newJoint( "weld", lock, plank , 0, 0 ) --comment this out weldJoint.dampingRatio = 1 --comment this out weldJoint:removeSelf() --comment this out timer.performWithDelay( 1500, function(event) physics.start() end) end end bkg:addEventListener( "touch", drawPlank )
Once again, many thanks,
Craig