Remove physics body on collision?

I know you can’t do this on collision, but I heard you could do it with a timer delay, but it still doesn’t seem to be working, here is my code

ocal function speedCollision(event) local phase = event.phase local target = event.target local other = event.other if phase == "began" then if other.collisionType == "eggplant" then eggplant:applyForce( 0.05, 0, eggplant.x, eggplant.y) speed.alpha = 0 timer.performWithDelay(10, physics.removeBody(speed)) end end end speed:addEventListener("collision", speedCollision)

Also, the applyForce value applies the same amount of force to the object no matter what the x y values are, anyone know why?

That’s wrong.  Use a closure:

timer.performWithDelay(10, function() other:applyForce( 0.05, 0, other.x, other.y) speed.alpha = 0 timer.performWithDelay(10, physics.removeBody(speed)) end)

Cheers man. Would you  know why the same amount of force is being applied to the object no matter the value?

“eggplant:applyForce( 0.05, 0, eggplant.x, eggplant.y)”  applies the same force in the x direction as “eggplant:applyForce( 200, 0, eggplant.x, eggplant.y)” When used in the local collision code.

You don’t apply force once and forget it.

For ‘force’ to work (as designed) you must apply it every frame. 

If you want instantaneous acceleration (a hit or kick) use 

applyLinearImpulse()

Give the physics guide a read and you’ll see what I mean:

https://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html#objectapplyforce

PS - If you’re coming from a system where forces ‘stay applied’ till cancelled, this will be confusing. 

In Corona/Box2D, forces need to be ‘renewed’ every frame.  (Inertia does not of course.)

Cheers, I so just want an instaneous force, I had already tried applylinearimpulse before o tried apply force, the result is exactly the same

That means you’re making a mistake somewhere.  

You can:

  1. Bundle up your whole app in a zip and share a link to it here and then I or someone else can look at it.

  2.  Make a simple example:

  • basic config.lua
  • basic build.settings
  • main.lua with:
    • one object  with a dynamic body
    • one perform with delay call 
    • one impulse call

Prove to yourself physics is working, then backtrack and find the error in your game.

Note: If you write a simple example and it doesn’t work share that example as a zip file with us.

-Ed

Thanks a lot! I have figured it out (stupid mistake) I had the density of the eggplant as 0 so of course the same amount force is going to be applied at any value that applyLinearImpulse is set to because the mass of the eggplant is infinitely small and there’s an inversely proportional relationship between the two. 

That’s wrong.  Use a closure:

timer.performWithDelay(10, function() other:applyForce( 0.05, 0, other.x, other.y) speed.alpha = 0 timer.performWithDelay(10, physics.removeBody(speed)) end)

Cheers man. Would you  know why the same amount of force is being applied to the object no matter the value?

“eggplant:applyForce( 0.05, 0, eggplant.x, eggplant.y)”  applies the same force in the x direction as “eggplant:applyForce( 200, 0, eggplant.x, eggplant.y)” When used in the local collision code.

You don’t apply force once and forget it.

For ‘force’ to work (as designed) you must apply it every frame. 

If you want instantaneous acceleration (a hit or kick) use 

applyLinearImpulse()

Give the physics guide a read and you’ll see what I mean:

https://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html#objectapplyforce

PS - If you’re coming from a system where forces ‘stay applied’ till cancelled, this will be confusing. 

In Corona/Box2D, forces need to be ‘renewed’ every frame.  (Inertia does not of course.)

Cheers, I so just want an instaneous force, I had already tried applylinearimpulse before o tried apply force, the result is exactly the same

That means you’re making a mistake somewhere.  

You can:

  1. Bundle up your whole app in a zip and share a link to it here and then I or someone else can look at it.

  2.  Make a simple example:

  • basic config.lua
  • basic build.settings
  • main.lua with:
    • one object  with a dynamic body
    • one perform with delay call 
    • one impulse call

Prove to yourself physics is working, then backtrack and find the error in your game.

Note: If you write a simple example and it doesn’t work share that example as a zip file with us.

-Ed

Thanks a lot! I have figured it out (stupid mistake) I had the density of the eggplant as 0 so of course the same amount force is going to be applied at any value that applyLinearImpulse is set to because the mass of the eggplant is infinitely small and there’s an inversely proportional relationship between the two.