Animating Physics bodies best strategies?

I’m working on a platform style game, using physics for motion and collision of the player and platforms. I want to have some objects like elevators or clouds etc. that slide up and down, left and right. At the moment I’m using Static objects, and using a transition.to() to move them. The motion is of course applied outside of the physics system. As is, everything seems to be working well enough.

I’m trying to come up with a system that gets the same effect using physics. I can make the objects the dynamic, and use setVelocity(), applyForce(), or applyLinearImpulse(), but. Using these you can’t know how far an object has moved, making it difficult to know when to reverse the motion and send the object the other direction. I have been looking at joints wondering if a piston joint might work. 

On closer inspection, it seems a Kinematic body would the correct choice for an object that ignores gravity and is moved via physics. 

Though there is a mention under Static bodies: Static bodies can be moved manually by the user. Implying that it’s acceptable to move static bodies outside of physics. 

You can use things like transition.to() to move static objects.  You can also use sensors and collision detection to determine directions to add force too.

Rob

You can use a static or dynamic body, and update its speed/position/direction in an enterframe event.
For example say you’d like for an enemy character to automatically move left and right on a platform, without falling down.
You can save a reference of that platform on the collision between the platform and the enemy, so that the enemy knows which platform he is on. Then, in the enemy:enterFrame function, you can have a check between enemy.contentBounds.xMin/xMax and platform.contentBounds.xMin/xMax. If the enemy xMin is < than the platform xMin, you change the direction of the enemy so that it goes right. If the enemy.xMax is > then the platform xMax, you change it again so that the enemy goes left.

Or you can also use dummy physics bodies at the edges of the platform so that they trigger a collision event in which you change the direction of the object that collided with them in case it is an enemy (: It really depends on how you build the game though, as both having too many bodies/collisions and enterFrame events can affect performances in their own ways.

I would avoid the use of transitions in general. They can be a bit of a mess to manage and they aren’t as flexible (say you need to pause your game for example). Plus they work based on time rather than framerate, which can be an issue in some situations if you’ve built it around framerate.

On closer inspection, it seems a Kinematic body would the correct choice for an object that ignores gravity and is moved via physics. 

Though there is a mention under Static bodies: Static bodies can be moved manually by the user. Implying that it’s acceptable to move static bodies outside of physics. 

You can use things like transition.to() to move static objects.  You can also use sensors and collision detection to determine directions to add force too.

Rob

You can use a static or dynamic body, and update its speed/position/direction in an enterframe event.
For example say you’d like for an enemy character to automatically move left and right on a platform, without falling down.
You can save a reference of that platform on the collision between the platform and the enemy, so that the enemy knows which platform he is on. Then, in the enemy:enterFrame function, you can have a check between enemy.contentBounds.xMin/xMax and platform.contentBounds.xMin/xMax. If the enemy xMin is < than the platform xMin, you change the direction of the enemy so that it goes right. If the enemy.xMax is > then the platform xMax, you change it again so that the enemy goes left.

Or you can also use dummy physics bodies at the edges of the platform so that they trigger a collision event in which you change the direction of the object that collided with them in case it is an enemy (: It really depends on how you build the game though, as both having too many bodies/collisions and enterFrame events can affect performances in their own ways.

I would avoid the use of transitions in general. They can be a bit of a mess to manage and they aren’t as flexible (say you need to pause your game for example). Plus they work based on time rather than framerate, which can be an issue in some situations if you’ve built it around framerate.