Page 1 of 1

Physics for a character running

Posted: Wed Jan 02, 2008 6:25 am
by Youx
Hi everyone

I was wondering if anyone had a good idea about a good way to simulate (really boldly) a human running, like a character sprite for a game, in chipmunk.

What I am doing right now is :
world
* gravity = 400 downward

ground
static body
* mass = inf
* inertia = inf
with a segment shape
* friction (u) = 1.0

man
a body
* mass = 1.0
* inertia = 1000
with a square shape (-50,-50 -> 50,50)
* friction (u) = 0.2

When a key is pressed to run, I apply a force of 100 to the right
When a key is pressed to jump, I apply an impulse of 200 upward

So far, it works (after a lot of tries), but not exactly like I want :
- Acceleration and deceleration are quite low (you have to keep the run button on for a long time to get it "running", and to have it "stop" once the button is released)
- I don't think it reaches a "peak" speed like it should (if I keep pressing on the run button, it gets faster and faster)

I am quite new to this, and not really good at physics (been a long time), but I am really interested if someone has any suggestion to achieve this (quick acceleration to reach a peak speed, quick deceleration to stop)

Thanks

Re: Physics for a character running

Posted: Wed Jan 02, 2008 4:13 pm
by dc443
You probably don't want to just apply a constant force because that simply means the object's velocity will get faster and faster. You could apply a drag force in the opposite direction which is proportional to the speed, or speed squared or something like that, and it will then reach a maximum velocity.

Your friction of 0.2 on the guy seems a bit small if you want the friction to cause him to stop quickly.

Re: Physics for a character running

Posted: Thu Jan 03, 2008 4:58 am
by lucas
Unless you want ragdoll, make the character a square when running, and change to a shape on collisions.

Re: Physics for a character running

Posted: Thu Jan 03, 2008 7:19 pm
by Youx
Ok, thanks for the advices, it was really useful!!
I changed a few things :
- I increased the friction (to 0.95), and increased the force applied (1000 instead of 100), so now it accelerates and stops much quicklier.
- I also substracted 2 times the speed when moving, so now it reaches a nice peak speed.

But even though all this works fine now, I am running into a completely different problem now :
When I make the character jump (while running), when it reaches the floor, the square starts "rolling", probably because of friction or something : ->jumpy
I thought of replacing the square by a circle, but there is not enough friction on a circle to make it stop, so I get an "ice-skating" character.
I am going to try flattening the square into a rectangle, maybe lowering its center of gravity like this will help? Or maybe I should try to increase the mass of the square?

Re: Physics for a character running

Posted: Thu Jan 03, 2008 7:34 pm
by Android_X
If you want the rigid body to never rotate by itself just set its inertia to INFINITY.

Re: Physics for a character running

Posted: Fri Jan 04, 2008 1:57 pm
by slembcke
Seriously, you should look at surface velocities. Don't use basic forces to move your character. It will make walking on platforms and other interesting bit in your game trivial.

Re: Physics for a character running

Posted: Fri Jan 04, 2008 9:25 pm
by Youx
Ok thanks, I'll take a look at that :)