Sleeping + static body being moved

Apr 25, 2011 at 1:24 PM

I noticed that once things resting on a static body become asleep, they are not woken up when its Position is changed explicitly. What can I do about this?

Developer
Apr 25, 2011 at 7:42 PM
Edited Apr 25, 2011 at 7:44 PM

You should never change a Bodies position directly.

Instead, use something like this to move a Body

Vector2 destination = path.GetPosition(time);
Vector2 positionDelta = body.Position - destination;
Vector2 velocity = (Vector2) ((positionDelta / timeStep) * strength);
body.LinearVelocity = -velocity;


Where destination is where you want the Body to end up. timeStep is the timeStep to be used in the next frame (usually 1f /60f). strength is a coefficient from 0-1 with 1 being the strongest.
You can also do the same thing with Angular velocity.
Apr 26, 2011 at 6:03 AM

What if I don't want to apply forces and want to move the body to exactly the new position? This is a level editor.

Developer
Apr 26, 2011 at 10:16 PM

Setting position doesn't cause an object to become awake. If you want the object to be awake after your editor moves it, no big deal, just make the editor explicitly force the object awake. That's what I do with my editor, unless I have it in the mode where I am moving objects that I don't want to wake up until some game event activates it.

Developer
Apr 26, 2011 at 11:05 PM

By adding the perfect amount of velocity you can move an object to the perfect position while still keeping it in the simulation.

Also, why would you need the body to become awake in the editor?

Apr 27, 2011 at 2:05 PM

@EricCosky: thank you! I didn't realize waking just one body would wake the others in contact, but it makes sense. now.

mattbettcher@: but then whatever you're moving would cause unwanted collisions if you're changing its position by a large number (eg from one side of a map to the other)?. Why wake/simulate bodies in an editor? To set their initial parameters for the game. I'm placing some stuff on top of chunks of terrain and want it sitting at rest when the level starts. So I let them fall and come to rest in the editor's simulation before saving.