2D Platformer Collision - Moving Platforms, etc

Started by
5 comments, last by K_J_M 14 years, 9 months ago
Hi there! I'm planning on creating a 2D Platformer using C++ with SDL. I was planning on implementing simple movement through 2D vectors (X + speedX, Y + speedY... kind of thing) and collision through simple rect-on-rect checking. I was wondering how things like moving platforms would be handled, where the player would stand on a land object and would move with the object when it moves. Also, do you have any general tips for me regarding collision and physics for 2D Platformers? Thanks guys!
Advertisement
The platforms can be implemented as kinematic objects, e.g., objects that you move arbitrarily but that dynamic (physically simulated) objects can collide with. Kinematic objects have material/surface properties, so if a dynamic object touches it, there will be friction. So, your player and NPC's can be dynamic objects. If they land on a moving, kinematic platform, friction will cause them to move with the platform. They can freely move around the world, interacting with kinematic platforms and other level elements (waterwheel, pendulum, other things you might put in there) just as they interact with other dynamic objects.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Just to expand: for all the platforms, etc. assume that no matter what happens in the world, they will always move according to some prescribed motion. If it's a platform, maybe it goes up and down according to some function you wrote.

Place the bodies in your physics simulation for those platforms, but set their mass and moment of inertia to infinity (or set their inverse mass, etc. to 0). You will have to manually recalculate and set their positions and orientations from your formulas each frame, since no forces in the world will be able to move them.

Now imagine a normal object in your world hits the platform. Since the platform has infinite mass, it's basically like the object hit a static wall. The object will react accordingly assuming you have your contact forces set up right (most contact force solvers can easily handle cases where one of the objects has infinite mass and MOI).

What happens if two platforms collide? Bad things. So when you are iterating over the bodies in your world, make sure if two kinematically controlled objects (fancy phrase that means your platforms) collide, you early out without trying to solve the collision. Platforms will pass right through each other. Still, try to avoid this situation since if a character is on a platform as it tries to move through another platform, you'll get some very large reaction forces (things will tend to explode).

If your equations that handle the motion of the kinematically controlled objects are differentiable (that is, you can easily calculate the velocity of the platform at any given frame), you'll better be able to handle the case of a character standing on a platform. If you assume the platform has 0 velocity at each time step, it will still work, but you might get an odd up and down vibration on the character as the platform moves up and down and the character alternates between intersecting and not.
[size=2]Darwinbots - [size=2]Artificial life simulation
Thanks guys, I am beginning to understand the theory there.

My main issue is that I'm clueless when it comes to implementation.

Can someone point me in the correct direction for how to write and implement a basic physics system (complete with Mass, MOI, Gravity, Friction calculations).

Example code would be fantastic. I'm sure I can do the math myself (although references for that would be great too), but I really need to understand how it is implemented.
Here is a site that explains every small part of a platformer game to things like moving platforms. The implementation is flash based, but you should still be able to pick out what you need. I recommend it.

http://www.tonypa.pri.ee/tbw/start.html
Holy crap, you can read!
Assuming you have a basic level of physics type knowledge, check out Erin Catto's GDC presentations. Then peek at Box2D lite. It's about the most stripped down, but functional, physics engine I know of. You can even steal Box2D and modify it for your personal use. in this case you would basically add "kinematically controlled objects" with infinite mass and moment of inertia (usually represented by 0 inverse mass and MOI) to the sim, and just make sure to remember to update their positions and velocities every frame before you simulate the world.

Alternatively, if your physics is really rusty, you might be better off just faking the physics ala something like the original Mario. In which case you'd just detect if the character is standing on a platform and set their position every frame by hand to follow the platform.
[size=2]Darwinbots - [size=2]Artificial life simulation
I've implemented moving platforms in my game quite succesfully.

Assuming you know the velocities of the moving platform, simply add them to your players position.


Taking a 2d example.

My moving platform moves with velocites

moving_platform_velocity_x = -5
moving_platform_velocity_y = -2

in an up left ish direction in my game world

of course, the moving platforms position, whatever that may be, is updated by adding it's velocity vector to it.

If your player is in contact with the platform. Then simply add the moving platform velocity vector to your players position.

player_pos_x = player_pos_x + moving_platform_velocity_x
player_pos_y = player_pos_y + moving_platform_velocity_y

No matter if your player is already moving, if he's in contact with the platform, his position will be updated accordingly.

Allows the player to move on the platform whist it's moving.

Very simply.

KJM

This topic is closed to new replies.

Advertisement