The best way to make player for a platformer game?

Official forum for the Chipmunk2D Physics Library.
Post Reply
sickman
Posts: 19
Joined: Mon Oct 06, 2008 7:50 am
Contact:

The best way to make player for a platformer game?

Post by sickman »

If I make the player to be just a rigid body, and move with applying impulses, he will rotate.
And I don't want that to happen. But if I just set player's body's angle to 0 every main loop time,
the body gets acting weird because velocity and position get badly out of sync.
The player should also be able to stand on other physics objects. So how to do this thing?
Player's body is just a block, nothing more complex shapes.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: The best way to make player for a platformer game?

Post by slembcke »

First of all, you can give your player's rigid body an infinite moment of inertia. This makes it infinitely hard to cause the object to rotate, but it will otherwise act normally. That solves the easier of the two problems.

I have a property available on all collision shapes called surface velocity. It was meant to help do things like conveyor belts or player character controls. Basically, it's an additional velocity that is added during the friction calculations so that you can make the surface of the shape be moving at a different rate as the shape itself.

A conveyor belt is the simplest example. A line segment without a surface velocity acts like the ground, friction makes objects touching it stop (causes the relative velocity to go to zero). If you give the segment a surface velocity, it uses friction to move the object along it so that it matches the surface velocity. The limitation is that the surface velocity is in absolute coordinates and only operates through friction. This means that you cannot give a wall a surface velocity moving to the right and have it bump objects to the right that touch it.

A friend of mine made a prototype of a platformer game using Chipmunk, and we came up with some new ideas recently to model the player character. The player was made of two box shapes, a big one that was used as the main collision shape, and a smaller one that only stuck out of the bottom of the main shape. The main shape was treated as normal. The bottom shape had a surface velocity applied to it to cause the player to move powered by friction. This gives you a lot of functionality for free. The player will slide to a stop using the correct amount of friction. The player can stand on moving platforms, and they will just work because friction is relative. Lastly, because the smaller box can only touch things below the player, you can easily use a collision pair function to determine when the player is standing on the ground.

As a last note, it's perfectly valid to use friction values larger than 1.0.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sickman
Posts: 19
Joined: Mon Oct 06, 2008 7:50 am
Contact:

Re: The best way to make player for a platformer game?

Post by sickman »

Can I see the source code of the platformer game?

There aren't much tutorials about chipmunk so it would be great help.
ippa
Posts: 14
Joined: Mon Aug 25, 2008 4:53 am
Contact:

Re: The best way to make player for a platformer game?

Post by ippa »

mm some code would kick ass =). Im experimenting with a platformer myself with gosu/chipmunk in ruby.. so I would love to see some more rubycode using chipmunk out there (I just made an educated guess that your/your friends prototype was coded in ruby).
http://rubylicio.us/ - tasty links for the ruby connoisseur.
User avatar
AndyKorth
Site Admin
Posts: 36
Joined: Wed Aug 15, 2007 3:56 pm

Re: The best way to make player for a platformer game?

Post by AndyKorth »

I'm the said friend. However, I wrote the game using the ruby bindings, and there is very little code that would be useful. Scott touched on all of the major points, and the rest of the code is for the terrible graphics ;)

But to summarize.. infinite moment of inertia to prevent the player from rotating.

Use surface velocities to make the player move.

My player has a shape attached to the bottom of it, and when it collides with the walls, I know the player is not airborne. When he jumps, I set a flag marking him as airborne. I set a friction of mu = 2 on the bottom shape, and then set a friction of zero on the other shape- that way she slides when you jump against walls.

That's really all the hints I've got. The main player is a forklift, so a lot of the code was setting up the fork part of the forklift and getting the sprites line up. As it stands it's very confusing code and would make a terrible example.
singpolyma
Posts: 16
Joined: Sun Nov 09, 2008 3:33 pm
Contact:

Re: The best way to make player for a platformer game?

Post by singpolyma »

What was used for jumping? surface_v doesn't really work there, it seems....
ippa
Posts: 14
Joined: Mon Aug 25, 2008 4:53 am
Contact:

Re: The best way to make player for a platformer game?

Post by ippa »

singpolyma wrote:What was used for jumping? surface_v doesn't really work there, it seems....
an upwards force with body.apply_force() ? :) just guessing.

@AndyKorth: Thanks for the extra details!
http://rubylicio.us/ - tasty links for the ruby connoisseur.
sickman
Posts: 19
Joined: Mon Oct 06, 2008 7:50 am
Contact:

Re: The best way to make player for a platformer game?

Post by sickman »

ippa wrote:
singpolyma wrote:What was used for jumping? surface_v doesn't really work there, it seems....
an upwards force with body.apply_force() ? :) just guessing.

@AndyKorth: Thanks for the extra details!
Some time ago I was trying to make a body jump with apply_force, but
someone told me apply_force will make the body moving forever, never slowing down.

Instead, apply_impulse will make the body moving, but it slows down.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: The best way to make player for a platformer game?

Post by slembcke »

You want to use impulses to mimic forces that are very large and happen quickly such as jumping or gun recoil. Impulses are direct changes to the velocity of the body, equivalent to adding a value to the velocity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
bluescrn
Posts: 5
Joined: Mon Aug 18, 2008 2:38 am
Contact:

Re: The best way to make player for a platformer game?

Post by bluescrn »

After a lot of experimentation, I've got some fairly good platform character movement working... but the end result became a little bit complex...

I'm using 2 circles for collision, a sort of 8-shape with the bottom circle being slightly smaller. (This is mostly so the top circle can have the friction value I want for pushing objects, and the bottom 'feet' circle can have a different value)

- The body is set up so it never rotates, using an infinite moment of inertia (cpBodyNew(mass, INFINITE))

- Friction is set to a low value when a movement key is pressed, but gradually ramped up to a fairly high value when the key is released. This stops the player sliding down slopes too much when stationary, within him slowing down too fast when you try to move him

- Left/Right movement is done by applying an impulse - this impulse is scaled down towards zero as the player approaches his maximum speed in that direction, so you can't accelerate to unlimited speeds!

- I added some extra friction/air resistance by scaling down the velocity. Different values when airborne and on the ground

I use collision pair functions to detect when you're on the ground, to allow/disallow jumping. Beware of arkward cases (player wedged in a' \8/' type position where there's nothing directly below him). I also allow the player to jump if he was touching the ground within the last 0.1 seconds - otherwise the jump control can be a bit unpredictable after slight bounces when sliding down slopes or after falling

It's become quite obvious that there's no reaction force when my player moves or jumps whilst on a movable object - that's one of the next things I need to work on...
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests