Physics - Gravity influence

Thanks to the Corona team for an easy dev. environment - I really need to say that!

Never having used the physics capabilities I tried a simple test with many (over 100) small boxes that are falling 1-by-1 about 500ms apart. I would like to have a way to attract a box where my finger would be. The big unknown is the API’s have all sorts of actions that can be applied to a body after (for example) physics.addBody is used.

With so many objects being created how do I access ONLY the 1 ball initially dropping down, ignoring the others? The function physics.addBody does not return a body identifier (if I read the API’s right) so how do I know what body identifier to use? For example:

Body:applyLinearImpulse( xForce, yForce, bodyX, bodyY )

How do I get/find the object “Body” (the last body added to physics)??? Is this the same as the object identifier used when calling physics.addBody?

Thanks in advance! [import]uid: 156018 topic_id: 35211 reply_id: 335211[/import]

If I understand your question correctly, it works like this as far as I know. When you use addBody on a display object, you’re adding that display object to the physics “world” – which means you can still refer to that “body” using your initial display object.

For example:

[lua]local hero = display.newImage( “hero.png” )
physics.addBody( hero, “dynamic”, { friction=0.2, bounce=0.7, density=2 } )

hero:applyLinearImpulse( xForce, yForce, hero.x, hero.y ) – hero IS the “body”[/lua]

The physics.addBody() doesn’t do anything but tell the physics engine to include this display object in its processing. It’s not creating anything “new” really.

Hope that helps. [import]uid: 202223 topic_id: 35211 reply_id: 139985[/import]

Hi @lessmsios,
I can help you here…

  1. If you just want to keep a reference to the most current body, I’d just suggest you keep a variable like “currentBody” and then, when you assign the body to an object, you assign that object’s reference to “currentBody”. Don’t forget to “nil” out the value too, if you clear/remove that object after it leaves the screen bounds, or similar!

  2. For attracting a body to your touch location, you’ll eventually want to investigate the “touch joint”. This is like a theoretical point (your finger) with a flexible “band” joining that point and the object in question. You can apply various damping and pull parameters to have the object snap/follow your finger, even as you move it around.
    http://developer.coronalabs.com/content/game-edition-physics-joints

  3. Creating bodies during Runtime can be slightly performance-costly on slower devices. You might want to consider creating 10-20 boxes in advance or something, keeping them “inactive” until they start falling, and then recycle them if they leave the screen (pop them back up to the top and put them back to sleep or whatever). Not sure what exactly your scenario will do, but it’s always better for performance to pre-create physics bodies, whenever possible.

Any other questions, don’t hesitate to ask!

Best regards,
Brent Sorrentino [import]uid: 200026 topic_id: 35211 reply_id: 139986[/import]

WOW - thank you both, in less than 30 minutes! [import]uid: 156018 topic_id: 35211 reply_id: 139988[/import]

@develephant : Works but is there a way to control the magnitude of the impulse? [import]uid: 156018 topic_id: 35211 reply_id: 139994[/import]

If I understand your question correctly, it works like this as far as I know. When you use addBody on a display object, you’re adding that display object to the physics “world” – which means you can still refer to that “body” using your initial display object.

For example:

[lua]local hero = display.newImage( “hero.png” )
physics.addBody( hero, “dynamic”, { friction=0.2, bounce=0.7, density=2 } )

hero:applyLinearImpulse( xForce, yForce, hero.x, hero.y ) – hero IS the “body”[/lua]

The physics.addBody() doesn’t do anything but tell the physics engine to include this display object in its processing. It’s not creating anything “new” really.

Hope that helps. [import]uid: 202223 topic_id: 35211 reply_id: 139985[/import]

Hi @lessmsios,
I can help you here…

  1. If you just want to keep a reference to the most current body, I’d just suggest you keep a variable like “currentBody” and then, when you assign the body to an object, you assign that object’s reference to “currentBody”. Don’t forget to “nil” out the value too, if you clear/remove that object after it leaves the screen bounds, or similar!

  2. For attracting a body to your touch location, you’ll eventually want to investigate the “touch joint”. This is like a theoretical point (your finger) with a flexible “band” joining that point and the object in question. You can apply various damping and pull parameters to have the object snap/follow your finger, even as you move it around.
    http://developer.coronalabs.com/content/game-edition-physics-joints

  3. Creating bodies during Runtime can be slightly performance-costly on slower devices. You might want to consider creating 10-20 boxes in advance or something, keeping them “inactive” until they start falling, and then recycle them if they leave the screen (pop them back up to the top and put them back to sleep or whatever). Not sure what exactly your scenario will do, but it’s always better for performance to pre-create physics bodies, whenever possible.

Any other questions, don’t hesitate to ask!

Best regards,
Brent Sorrentino [import]uid: 200026 topic_id: 35211 reply_id: 139986[/import]

WOW - thank you both, in less than 30 minutes! [import]uid: 156018 topic_id: 35211 reply_id: 139988[/import]

@develephant : Works but is there a way to control the magnitude of the impulse? [import]uid: 156018 topic_id: 35211 reply_id: 139994[/import]