Propert way to create Side Scroller type physics

Mar 27, 2009 at 6:02 PM
I was just curious if there were any samples about on how to implemented side scroller type physics. My initial attempt was to simply create a body with no friction and apply a horizontal linear velocity to him to make him move. This worked ok but gravity kept causing him to slide back down the hill. I was curious how you would go about making a player character and moving him?
Mar 27, 2009 at 7:57 PM
Edited Mar 27, 2009 at 7:58 PM
Well let me try to help

1. For friction
Create a body and a geom for the character,
make him like you normaly would.
But then change his friction coefficient.
This resides in the body or geom class i cant remember
basically make his body and geometry then say

body.frictioncoefficient = 0.1f;
(or geom.frictionCoefficient = 0.1f;)

then create a body and geom for the floor
then change its friction coefficent
floorbody.frictionCoefficient = 0.2f;
(or whatever, just not 0)


play aroud with the numbers, the simulator takes the numbers from the colliding objects and gets the average number between them.
So when the player moves on the floor he will have a Friction of 0.15f.

For movement do this

if(keyBoardState.isKeydown(keys.Right))
{
body.ApplyForce(new vector2(200, 0));
}

if(keyboardState.isKeyDown(keys.Left))
{
body.applyforce(new vector2(-200, 0));
}

this will push your character in the x direction but not the y, his top force will be whatever x you set
This works very well and i use this method.
Hopefully this helped, obviously the code above is sloppy but Farseer does alot of the work for you in cleaing it up(dont copy and paste it).


If you want i can just create a small sample, of course its quite simple so there wont be much need for it.
Mar 27, 2009 at 9:05 PM
Thanks for the reply. I know have used Farseer to make a few different scenes so I am familiar with the framework. The problem I am running into is the fact that if I make the friction low enough that he can easily slide up hills then the gravity pushes him and makes him slide down the hill when no user input is active. Also if I turn down the gravity low enough that it does not push him down hills then when he is running down a hill (applying force down the hill) then the gravity is not pushing him down fast enough and he kind of "bounces" as he moves down the hill. I always get in a big circle of tweaking the values etc... and always end up in the same spot ;(
Mar 27, 2009 at 10:27 PM
Alright i understand your problem now
Ive yet to have to deal with that yet though,
your hill must be pretty steep though
what you could do is

if(playerGeom.Collides(Hill.Geom))
{
playerBody.IgnoreGravity = true;
}
else
IgnoreGravity = false;

I dont see any problem with disabling the gravity when the player is already grounded.
When he jumps he would jump normal, and when he walks it would be unchanged considering Horizontal movement doesnt take gravity into account anyways.

Sorry i cant give you the super Fix-All answer you must be looking for, im sure genbox or rogueCommander know what to do.
Mar 27, 2009 at 11:29 PM
@TLegion: Thanks for holding me in such high esteem, but im just another person trying his best to answer peoples questions.
What you might do is (in pseudo code):
void OnCollision(Geom geom1, Geom geom2, ContactList list)
{
    if (geom1.tag is a playerTag and geom2.tag is a terrain tag)
    {
        if (geom1.body.linearVelocity.x is very close to zero)
        {
            geom1.friction = 0.2 (or whatever value works best to stop sliding)
        }
    }
    if (geom2.tag is a playerTag and geom1.tag is a terrain tag)
    {
        if (geom2.body.linearVelocity.x is very close to zero)
        {
            geom2.friction = 0.2 (or whatever value works best to stop sliding)
        }
    }
}
void OnSeparation(Geom geom1, Geom geom2, ContactList list)
{
    if (geom1.tag is a playerTag and geom2.tag is a terrain tag)
    {
        if (geom1.body.linearVelocity.x is not very close to zero)
        {
            geom1.friction = 0.1 (or whatever value works best for movement)
        }
    }
    if (geom2.tag is a playerTag and geom1.tag is a terrain tag)
    {
        if (geom2.body.linearVelocity.x is very close to zero)
        {
            geom2.friction = 0.1 (or whatever value works best for movement)
        }
    }
}
then assign these to the OnCollision and OnSeparation events of your terrain and player geoms
Mar 29, 2009 at 1:39 PM
Another thing that might help is to push the player as if they were facing up or down the slope. In other words apply the movement force along the vector that is perpendicular to the normal of the surface. That way you're not pushing into the hill when going up and also not stepping into thin air when going down. Although i can imagine that walking off rounded edges might not behave as desired.

// Get from the last collision contacts with terrain objects (warning: the direction of the normal seems to be dependant on order of the two geoms being created at the moment)
Vector2 lastTerrainNormal;
// Get perpendicular vector
Vector2 playerRight = new Vector2(lastTerrainNormal.Y, -lastTerrainNormal.X)

// Apply to lateral force. Plus and minus maybe incorrect
if(keyBoardState.isKeydown(keys.Right))
{
body.ApplyForce(playerRight * 200);
}

if(keyboardState.isKeyDown(keys.Left))
{
body.applyforce(playerRight * -200);
}

Apr 1, 2009 at 8:04 AM
Hi there Shaku

Theres another way to do this thats worked really well with me, and thats to make your character as a box(or whatever shape) with a ball underneath attatched with a pin joint that it rolls on (like a wheelchair :).

To move, apply toque to the ball, and to stop moving, create an AngleJoint between the box and ball. This stops it from rolling if you stop on a hill.

To keep the player upright use a Fixed Angle Joint attatched to the box. You can also use this to adjust the players angle to the ground if you want.

Another advantage to this is that your player reacts more realistically when walking on things, for exaple when running over little blocks they will be thrown back a little as if kicked up from his feet. And in a hampster wheel, the wheel will turn as you are running.
Oct 2, 2009 at 8:21 PM

Hi RobertDodd,  i tried to do like you sad, but character moves in a very broken manner and almost flies sometimes

Then i changed acceleration method from appling torque to just increasing AngularVelocity and it helped a lot, but still whole system is shaking and jumping all the time

I would really appreciate your help.

 

sorry for bad english

Oct 3, 2009 at 7:28 PM

i'm making a game that requires quite a bit of control over the characters, so i've developed a handful of cool tricks to make the movement more stable. i can upload the current build for you to check out if you want me to. just ask.

as for those handful of tricks... (btw my characters use the ball method mentioned above)

- player magnetism.
there is a small geometry under my character for detecting the ground. when it's colliding with something, i apply downward force on the character. it's a great way to increase traction and reduce bouncing.

- magic traction.
if you use the ball method, there is no way to control the player in midair. it makes it extremely hard to jump accurately. so i add a predetermined amount of force to the ball simultaneously to the rotation of the ball. while barely noticeable on the ground, when jumping it allows for some much needed control.

- angular velocity.
technically, it would be more correct to use torque, but it tends to be rather sluggish. setting the angular velocity to a predetermined "runspeed" multiplied by the x value of the thumbstick makes for good acceleration and a constant runspeed.

-braking.
again, this isn't technically correct, but i find it works better. for the brake, i check two conditions.
A) the character is touching the ground
B) the x value of the left thumbstick is 0 (or within a deadzone)
if both of these are true, i divide the linearvelocity.x by a constant. i think right now it's around 1.2. this results in relatively quick stops and very little rolling (unless the slope is steep enough to not touch the sensor).

i hope this helps you a bit. if you have any questions ask, and if you wanna see my source i'll gladly upload it for you.

Oct 5, 2009 at 12:13 PM

Hey SlimeQ,

I would be very interested in getting a copy of your project too, because your tricks look very interesting :)

As for me I'm just using a plain rectangle and adjusting the velocity directly instead of applying forces, to be able to control in air moves and avoid too much bounciness, but I'll give your tricks a try!

Oct 5, 2009 at 12:18 PM
Edited Oct 7, 2009 at 10:10 PM

I would be very interested in getting a copy of your project too.
It sounds like you have figured out some good stuff.

Oct 6, 2009 at 1:25 AM

http://rapidshare.com/files/289196395/Hyperinlation.rar

this probably isn't too common, but here's the current build of my game. i thought about making a specific sample build, but i decided it's not worth it. just don't steal this, k?

it requires an xbox controller and the controls are as follows; left stick moves, clicking left stick jumps, right stick aims, right trigger shoots, right stick aims, face buttons switch equipment. currently, if you break off somebody's head the game crashes. it's cuz the entrail system isn't quite perfected. so don't do that.

Oct 7, 2009 at 6:53 AM

Hey danthekilla, I'd suggest you now remove your email address from the post. That's a great way to attract spambots!

Oct 7, 2009 at 9:06 AM
SlimeQ wrote:

http://rapidshare.com/files/289196395/Hyperinlation.rar

this probably isn't too common, but here's the current build of my game. i thought about making a specific sample build, but i decided it's not worth it. just don't steal this, k?

it requires an xbox controller and the controls are as follows; left stick moves, clicking left stick jumps, right stick aims, right trigger shoots, right stick aims, face buttons switch equipment. currently, if you break off somebody's head the game crashes. it's cuz the entrail system isn't quite perfected. so don't do that.

Thanks a lot! :)

Oct 7, 2009 at 10:11 PM
Cowdozer wrote:

Hey danthekilla, I'd suggest you now remove your email address from the post. That's a great way to attract spambots!

Done and Done.

Thanks for reminding me.