2
\$\begingroup\$

lately I have been trying to create a 2D platformer engine in C++ with Direct2D. The problem I am currently having is getting objects that are resting against each other to interact correctly after accelerations like gravity have been applied to them.

Right now I can detect collisions and respond to them correctly (I think) and when objects collide they remember what other objects they're resting against so objects can be pushed by other objects (note that there is no bounce in any collisions so when objects collide they are guaranteed to become resting until something else happens). Every time the simulation advances, the acceleration for objects is applied to their velocities (for example vx += ax * t, where t is time elapsed since last advancement).

After these accelerations are applied, I want to check if any objects that are resting against each other are moving at different speeds than their counterparts (as different objects can have different accelerations) and depending on that difference either unlink the two objects so they are no longer resting, or even out their velocities so they are moving at the same speed once again. I am having trouble creating an algorithm that can do this across many resting objects.

Here's a diagram to help explain my problem

http://i.imgur.com/cYYsWdE.png

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

First of all, I have some doubts about your diagram. Do the arrows represent the velocity vector? If so, when you say that objects are "resting", do you mean that they are resting in relation to each other even though the "group" is moving with same velocity?

Well, besides that, from your sample diagram (whose blocks I will call A, B, C and D from left to right) I understood the following:

  • The blocks are all connected at the begining, moving to right with the same speed;
  • An additional force towards right is applied to block B;
  • And this causes the link between block A and B to be broken, also making block A to revert its velocity vector moving away from the new "group" (made of blocks B, C and D now).

Since your question is about how to create an algorithm to handle those changes, my suggestion is to create a composition between connected blocks to really create a group (like if they were magnetic attached). Use a compositite design pattern, for example, to join blocks together, so you can iterate through them when a force is applied to one specific block in the group. If your group structure shall store information about relative position (it can be simply the index position in the list), then you can split the group and revert the velocity of the subgroup at left/right from the block in which the force was applied (naturally, according to the direction of the force applied).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .