Simple Physics: How to stop an object from moving?

Set-up: A game piece is moving as the result of a collision, and a small while later the piece must stop moving.

Question: How can this game piece be programmatically stopped from moving? Also, how would a game piece stop moving through a secondary collision with a different object?

Thank you!

-David
[import]uid: 96411 topic_id: 18168 reply_id: 318168[/import]

Hey, David, I’m not sure how your physics objects populate your screen and how they move, but I use the following to stop my physics objects:

[lua]myObject.angularVelocity = 0;
myObject:setLinearVelocity(0, 0);[/lua]

I apply the above when the objects are out of sight, so I’m not entirely sure if the objects stop moving completely. If there are more than one object and if they are placed on top of each other, I imagine they’ll still end up pushing one another. However, if none of the physics objects are touching one another, I imagine they’d stop moving.

Naomi

EDIT: By the way, if you have gravity affecting the physics objects, perhaps removing the physics body like lKinx suggested is the way to go. Also, removing the physics body first and then adding a different kind of physics body might do the trick…? [import]uid: 67217 topic_id: 18168 reply_id: 69452[/import]

You could try this:

[lua]–Remove the physics when needed
physics.removeBody(object)
–Add the physics when needed
physics.addBody(object, “dynamic”] --(Or “static”)[/lua] [import]uid: 103624 topic_id: 18168 reply_id: 69443[/import]

What Naomi said above will stop the object from moving, but if you have any gravity at all it will be overwritten. The code above will only work if you have physics.setGravity(0, 0).

At least that is my understanding. [import]uid: 103624 topic_id: 18168 reply_id: 69455[/import]

You guys are awesome!

I’m not using gravity, so I’ll try setting the velocity to zero. Great idea!

Has anyone experimented with .isBodyActive set to false? Perhaps this might work too? I’ll have to experiment.

Thank you!

-David
[import]uid: 96411 topic_id: 18168 reply_id: 69468[/import]

Hey, David, I haven’t used isBodyActive property. If you do experiment with it, will you post back the result? I’d be so interested in learning how it might work.

Cheers,
Naomi [import]uid: 67217 topic_id: 18168 reply_id: 69470[/import]