Touch and joints

Hello everyone,

What I am doing is, I am using sensors to detect collisions and checking whether the collision has took place in touch began phase if yes then the object should create a weld joint with a stationary object.

The error i have got is posted below:

"ERROR: C:\Users\Swanand\Documents\Corona Projects\circular2\main.lua:77: physics.newJoint() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event"

I haven’t put the whole code because its quite large enough I have just put the part in which the above error has arrived.

The code is given below please have a look over it and every suggestion is welcome:

function revolve(re) newJoint= physics.newJoint( "weld",re,ball,re.x,re.y) end local function collide(event) if event.phase == "began" then if event.object1.name=="rect" and event.object2.name=="ball" then print("true") for \_,re in ipairs(rects1) do revolve(re) end end end end function tap( event ) if ( "began" == event.phase ) then Runtime:addEventListener( "collision", collide ) elseif("ended"==event.phase) then display.remove(myJoint) end end bg:addEventListener("touch",tap)

Thanks and Regards,

Swanand Thakur.

The error message is pretty straightforward. There are things you can’t do in the middle of physics calculation functions. The standard solution is to drop your code you need to run inside a short timer say 10ms or so. This gives the physics function that is locked, time to complete.

Rob

Hello Sir @Rob,

Thanks for your reply, would you please help me to understand things more clearer. I didn’t understand your point.

Is their any other way by which I can get the same output.

Regards,

Swanand Thakur

You can’t add physics bodies or joints in the middle of  collision processing

He is saying, use timer.performWithDelay() to delay by 10 ms, which effectively will be the next frame.

I often use 1ms instead :

function revolve(re) timer.performWithDelay( 1, function() local newJoint= physics.newJoint( "weld",re,ball,re.x,re.y) end ) end

This waits 1 ms to add the newJoint.  However, because scripts are only processed on frame boundaries, this is effectively ~17ms (@60FPS) or 33ms (@33 FPS) later.

timer.performWithDelay() calls in this frame with non-zero delays are always processed in a later frame.

Tip: I could use the value 10 as the delay and it would still wait till the next frame.  

Sir @roaminggamer,

Thanks for the detailed answer it has cleared the issue and it will definitely help me in future too.

Regards,

Swanand Thakur 

The error message is pretty straightforward. There are things you can’t do in the middle of physics calculation functions. The standard solution is to drop your code you need to run inside a short timer say 10ms or so. This gives the physics function that is locked, time to complete.

Rob

Hello Sir @Rob,

Thanks for your reply, would you please help me to understand things more clearer. I didn’t understand your point.

Is their any other way by which I can get the same output.

Regards,

Swanand Thakur

You can’t add physics bodies or joints in the middle of  collision processing

He is saying, use timer.performWithDelay() to delay by 10 ms, which effectively will be the next frame.

I often use 1ms instead :

function revolve(re) timer.performWithDelay( 1, function() local newJoint= physics.newJoint( "weld",re,ball,re.x,re.y) end ) end

This waits 1 ms to add the newJoint.  However, because scripts are only processed on frame boundaries, this is effectively ~17ms (@60FPS) or 33ms (@33 FPS) later.

timer.performWithDelay() calls in this frame with non-zero delays are always processed in a later frame.

Tip: I could use the value 10 as the delay and it would still wait till the next frame.  

Sir @roaminggamer,

Thanks for the detailed answer it has cleared the issue and it will definitely help me in future too.

Regards,

Swanand Thakur