[SOLVED]How would you move objects to achieve continuity?

Hello,
I have a scene where I got a lot of box2d bodies as obstacles to the player, which is a round circle. Like this:


The thing is that I want all these obstacles to move in the left direction so that the player would feel like he/she was actually moving(like other side-scroller games, such as flappy bird and I even think there actually is a smart name for this…).
So would you iterate over the bodies of the b2world and check each whether it is an obstacle by checking their userData and then set linear velocity on each one.
Or would you place them all in an array an then just iterate over that one and enable linear velocity to all OR maybe create a new layer consisting of only these static bodies(the obstacles) and then simply use this->setPositionX(this->getPositionX() - randomNumber*dt)
each frame? Or what would you do in order to achieve this the best and most efficient way?

Thanks, your time is much appreciated! Please.

move the player rather than the objects. and use cc.follow on the character

layer.runAction(cc.Follow.actionWithTarget(hero));

you could apply a consistent force upon the player if it is an endless runner.

1 Like

Thanks! But do you think that that is the most efficient solution? :confused:

I mean some of the advantages about making all the obstacles move instead of moving the actual player, are that you do not have to load all the objects at once by only having to create some of the obstacles at once(sounds confusing)… but what advantages are there of using the follow action? Cannot see any, but of course maybe somebody knows. Again, it would be much appreciated.

keep the bodies as static bodies and move the player. I do believe this is more efficient than moving the individual bodies.

1 Like

But when the player body jumps the camera still follows the object and centers it at the screen(I do not have any offset) and that is not really the effect I want. So unfortunenately, I guess that I will stick will moving the nodes individually… :frowning:

you can set boundaries on the follow action, set them for the top and bottom of your game world

in my game i use
var followAction = cc.follow(characterNode, cc.rect(0, 0, this.display.topRight().x, 4000));

in my case the character is moving vertically not horizontally, so you will need to adjust for your case,

the 4000 is the total height of my world

Thanks, it works well for me :slight_smile: But isn’t this in fact just the same as moving everything but the player/followedNode?

This is actually solved(I think but am still open for suggestions), I will start a new thread :slight_smile:

Another Method

Create the 4-10 obstacles and destroy them once they’ve entered and exited the screen. For flappy bird you could just update each obstacle’s position.x by the player’s “speed” (assuming it changes depending on how long they’ve stayed alive) using the standard scheduleUpdate. Collisions could be handled by simple rect/circle intersection logic.

This method is especially useful if the player never moves horizontally, for example.

If perf becomes an issue you can look into pooling these obstacles and making them inactive/invisible instead of destroying.

Like iterating over the b2world and find all obstacles/static bodies and then set the velocity of each other, right? And when iterating over the world and setting the linear velocity in order to move in the left direction, will they all start at the same time or, I know this question is probably just overthinking since that I guess we are only talking about milliseconds, if there is any difference?
Thanks for the answer!

Yeah, I was trying to explain how you could do it without physics and without creating all obstacles up front.
Yes, most simpler games shouldn’t have any performance issues on modern devices (note: I didn’t say won’t, just shouldn’t).

Overthinking is very common in programming and definitely in game development :smiley: which is why I try to answer a lot of these types of questions with a Devil’s Advocate approach or one with a counter-intuitive approach to get people thinking outside the box.

Great. One more: So if you created a horizontal scrolling game with box2d, would you use the follow action on the player or individually set the velocity all the obstacles by iterating over the the world(I do think that both options involves iteration in order to move multiple objects). Of course, I would also love to hear others’ opinion :slight_smile:

In the case of using physics you want as many bodies to be static instead of dynamic. Therefore I would not give velocity or move all the obstacles. I’d only move the player. You’ll probably still need to create and destroy Sprite+Bodies over time if the player continues for many minutes or longer.

I’d probably do a first draft by moving the player and having the camera follow either using the literal Camera (default or custom) or by just negating the world’s position by the same translation as the player (in reverse) to make the player look as if they’re not moving horizontally, but instead it appears the world is moving horizontally from right to left (assuming Flappy Bird mechanics).

Hope that helps. Others can answer with any different thoughts.

1 Like

If your wish would be to choose the velocity-way, wouldn’t you then just use kinematic bodies instead of static?

Looks like you’ve walked down this path before?
Proper way of moving physics object in path Box2d?.

So, maybe make 2+ prototypes with boxes as player and walls and test out each method yourself?

  • Player[Dynamic] + Walls[Static] + Const Impulse.x, Impulse.y on touch for player (linear, no damp/friction)
  • Player[Kinematic] + Walls[Dynamic] + Move player w/Velocity
  • Kinematic + Only use Box2d Collision Detection
  • No Physics
  • other

I’d say dalste gave you good advice.

You should prob use setCategoryBitmask instead of userdata to determine whether to act on collision.

Okay thanks for all these great suggestions, I cannot ask for more. Btw. that thread you linked was not about a scroller like this but where the whole level was showing on the screen but yeah I get it. Note: Yes, I do think that his answers were very helpful too

Okay actually I have managed to come up with one more question haha… So why would you prefer using methods like the follow action instead of just moving everything except the player in one direction, both methods looks like the same?

Update: I just realized that you actually have answered this question before but I still just cannot see why to use the follow action instead of moving all the other children than the player, since that it gives the same output(visually), correct? Maybe there also is a difference in the performance?

Why move 1000 things instead of just one? You’d have to investigate to determine if it was more/less performant. Usually moving the camera only requires one transform update versus 1000 transform updates and would likely be more performant, but that also depends on a lot of things (namely how engine/camera is implemented), however, and it sounds like you want to learn a best practice, but honestly whatever works for you while still being performant is a best practice for you.

2c

1 Like