1
\$\begingroup\$

I'm working on a 2D puzzle game, and one of the features is the ability to push crates so you can climb on top of them to get to a destination.

I've tried many things to get this to work but none of them succeed.

My main problem is that I don't know where to put my code. For example, I don't want to handle the movement of the crate in the player class because that wouldn't be neat, the same way some people say something along the lines of "If you're making chess, the rules of the pawn and knight shouldn't be in the rules of the queen".

If anyone has an answer or could provide a pseudo-code explanation it would be appreciated.

\$\endgroup\$
  • \$\begingroup\$ I'm also new at this. I would create a simple Crate class whose initial attributes are position, image, etc. Then in the class's update method I would check for collisions with the player using some kind of global function which checks for, and resolves, collisions. If there is a collision and the player is on the ground to the left of the crate and has positive x velocity, I'd move the crate right at some constant speed. If the player collides with a row of, say, two boxes and you want them both to move at, say, a slower speed, you'll have to do a bit more work. \$\endgroup\$ – GoldenGremlin Sep 16 '17 at 12:01
1
\$\begingroup\$

The short answer is a physics system. I job usually is to handle collisions in a believable way, among other tasks. Many game engines have this build in.

Long answer is that you have to add conditions to your movable objects, so that when objects collide with them a force is applied to them ( or at least they move with the them). This can be simple or complex depending on the rest of your project. This would all happen in the update() part of the standard game loop.

\$\endgroup\$
1
\$\begingroup\$

Your physics code is responsible for detecting collisions. Once detected, you need to respond to the collision, based on the "types" of A and B.

This is called "double dispatch" and Scott Meyers discusses it in More Effective C++.

Personally I use a more C-style approach to collision response, and use a simple bit-field to denote the type of object.

I look up a collision handler for (A,B) and if its not available, see if there is one for (B,A). Once found, use that one (*). If there is no special handler set for this type combo, just call a generic response function that pushes the colliding objects apart with contact joint.

This way, you can define a handler for (barrel, player) and another one for (barrel, barrel) for instance. A handler for (player, powerup) would typically remove the powerup without affecting the physics of the player, play a sound, and reward the player.

I simply keep an array of these handlers, and search it every time two objects collide. The response function will reside outside both Barrel:: and Player:: objects. The downside is that it does need to know about both Barrel and Player, and thus will typically include the headers for both.

(*) Alternatively, you could always sort A and B, so you know whether to look for A,B or B,A.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.