resetting the position of a physics object causes a total crash

I have a ball that drops by means of the physics engine and if it goes off screen it is automatically reset to its original position. This works fine.

My problem is the following.

I also want the ball to reset to its original position if it collides with a certain object on screen. The object is set up as a sensor and I know its working as I have it printing the word collided just before I call the same reset ball function as above.

However what happens this time is the collided message shows up correctly then the ball freezes and the simulator crashes.

I have tried to set the ball to a static object on collision and back again in case that was the issue but so far no joy.

Any help please?

Cheers

Mike R [import]uid: 9950 topic_id: 4911 reply_id: 304911[/import]

The error that comes up is this

collided
Assertion failed: (m_world->IsLocked() == false), function SetTransform, file /Users/ansca/.hudson/jobs/Main-DMG/workspace/platform/mac/…/…/external/Box2D_v2.1.2/Box2D/Box2D/Dynamics/b2Body.cpp, line 395.
/Applications/CoronaSDK/Corona Terminal: line 9: 1374 Abort trap “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
logout

I wanted to add that using a timer as mentioned elsewhere stops the crashing but the object becomes a dead object, no longer being affected by gravity or anything. SO far nothing I have tried has brought the ball back to life.

What I find strange is I am using boxx2d in another language and doing the exact same thing there is no problem at all. So although the error seems to point to boxx2d I am not sure why it would not work the same.

Cheers

Mike R

Cheers

Mike R [import]uid: 9950 topic_id: 4911 reply_id: 15860[/import]

you need to make changes to your world outside of the physics “step”.

i would try setting a flag during the collision and then check that flag in your runtime enterframe event (this is a safe point)

this works for me

[lua]local physics = require(“physics”)
physics.start()

local ball1 = display.newImage(“football.png”)
ball1.x = 100
ball1.y = 130
physics.addBody(ball1, “dynamic”, {bounce=0.5, density=0.5, radius=23})
ball1.isBullet=true
ball1.name=“ball1”

local ball2 = display.newImage(“football.png”)
ball2.x=70
ball2.y=50
physics.addBody(ball2, “dynamic”, {bounce=0.5, density=0.5, radius=23})
ball2.isBullet=true
ball2.name = “ball2”

local ground=display.newRect(0,400,400,50)
ground:setFillColor(255,255,255,255)
physics.addBody(ground,“static”, {density=1})

local wall1 = display.newRect(0,0,32,480)
physics.addBody(wall1,“static”, {density=1})

local wall2 = display.newRect(300,0,32,480)
physics.addBody(wall2,“static”, {density=1})

local resetBall=false

local function onLocalCollision( self, event )

local obj1 = event.target
local obj2 = event.other

if(obj1.name==“ball1” and obj2.name==“ball2”) then
resetBall=true
end

end

ball1.collision = onLocalCollision
ball1:addEventListener( “collision”, ball1 )

local function update(event)

if(resetBall) then
– stop it bouncing etc
ball1.isAwake=false

– reset position
ball1.y=-50
ball1.x = ball2.x+20

– start it going
ball1.isAwake=true
resetBall=false
end
end

Runtime:addEventListener(“enterFrame”, update)[/lua] [import]uid: 6645 topic_id: 4911 reply_id: 15881[/import]

Thank you for that. I thought it had not worked at first but a couple of tweaks and all is fine.

Thank you

Mike R [import]uid: 9950 topic_id: 4911 reply_id: 16010[/import]