Movement In Physics-Based Endless Runner

Following scenario:
I want to move enemy from right to left on the screen.
Enemy has physics body attached to it, so I can detect collision with hero or bullets.

How should I move the enemy?

  1. ​By setting its x/y coordinates in game loop (enterFrame event handler)?
  2. By setting its linear velocity?

What’s the preferred way?

I thought moving display objects being active in physics world by setting their x/y coordinates is wrong, because it causes some conflicts with the physics engine. Is this right?

If so, and the preferred option is using the 2., how do you move enemy that shouldn’t move on straight line, but on curved trajectory instead - e.g. sine function? In such case, I don’t know how I could use option 2. at all.

Just BUMPed this post, because I changed the content.

Let me get someone with more physics experience to give you a better answer, but if this is something like the turtles in Mario brothers where they are moving left to right to left, I would do it with a pair of transitions to move it from X = somewhere to X = somwheredifferent and on complete move it back and just have them call each other. 

The enterFrame is a reasonable method, but even with linear velocity, you need to have that in an enterFrame to keep it moving.

Hi @corona.shaman,

Normally, I would suggest applying a linear velocity, because if you’re already using the physics engine, why not use it for that too? However, because of your “curved path” movement, a Runtime listener to update the X and Y is probably most logical. This won’t cause issues with the physics collisions, unless you’re skipping the objects in big steps all over the place… in that case, you could potentially “jump” the object over something small that it’s supposed to collide with. That might not be desirable.

Hope this helps,

Brent

Thanks guys.

@Brent: The idea is to collect/avoid items coming from right to left to the hero.

Player stays on the same distance from left, the illusion of movement is achieved via scrolling background, parallax, etc

I think I make the hero, incoming objects and hero’s bullets as sensors, so they interact with each other but don’t physically influence their positions upon collision. E.g. apple hitting hero won’t throw him back a bit when colliding because of its density, etc.

Most important part right now is to make some interesting controls, so it is not just another clone of Jetpack joyride.

Just BUMPed this post, because I changed the content.

Let me get someone with more physics experience to give you a better answer, but if this is something like the turtles in Mario brothers where they are moving left to right to left, I would do it with a pair of transitions to move it from X = somewhere to X = somwheredifferent and on complete move it back and just have them call each other. 

The enterFrame is a reasonable method, but even with linear velocity, you need to have that in an enterFrame to keep it moving.

Hi @corona.shaman,

Normally, I would suggest applying a linear velocity, because if you’re already using the physics engine, why not use it for that too? However, because of your “curved path” movement, a Runtime listener to update the X and Y is probably most logical. This won’t cause issues with the physics collisions, unless you’re skipping the objects in big steps all over the place… in that case, you could potentially “jump” the object over something small that it’s supposed to collide with. That might not be desirable.

Hope this helps,

Brent

Thanks guys.

@Brent: The idea is to collect/avoid items coming from right to left to the hero.

Player stays on the same distance from left, the illusion of movement is achieved via scrolling background, parallax, etc

I think I make the hero, incoming objects and hero’s bullets as sensors, so they interact with each other but don’t physically influence their positions upon collision. E.g. apple hitting hero won’t throw him back a bit when colliding because of its density, etc.

Most important part right now is to make some interesting controls, so it is not just another clone of Jetpack joyride.