Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having issues with my character in terms of gravity and the character bouncing up and down. You can find more information about it in the link below.

Character bouncing off ground due to gravity

I thought the answer below fixed it unfortunately it didn't after doing some more digging through the original platformer example on the app hub.

Platformer Starter Kit

The bouncing issue was resloved in the following way in the original.

The velocity would be added to the position

position += velocity;

Then the position would values would be even out.

position = new Vector2((float)Math.Round(position.X), (float)Math.Round(position.Y));

The x movement is below

accerlation = 0.046875f

velocity.X = velocity.X + accerlation; 

and gravity is being added like so

private float gravity = 0.21875f;

velocity.Y = velocity.Y + gravity;

The issue is that the values are so small that once they've been added they are taken off again resulting in the character not moving. What is the best approach to solving this issue ?

share|improve this question
    
The values would need to be over 1 making the characters too fast and the gravity force too strong, also the game is being run on a console so I don't need to account for different GPU speeds –  dbomb101 Dec 31 '11 at 16:44
    
I just want the values are relative, so for example if I set the acceleration to say 50.0f I would like to know what they would translate to pixels per frame the character should be moving –  dbomb101 Dec 31 '11 at 18:00
    
Ah fair enough, thanks –  dbomb101 Dec 31 '11 at 18:54

1 Answer 1

up vote 0 down vote accepted

The reason why your character is not moving is because your acceleration values are just too small. I recommend taking this course of action:

Step 1) A few improvements to the physics code

First make your physics code based time-based instead of frame-based. This simply means that where you used to have something move X units per frame, now it will be moving X units per second instead. Just multiply the elapsed time when calculating the velocity and position!

You could optionally also use velocity verlet over euler integreation as presented on this article for an extra bit of stability.

I'll leave it out for this example though:

// This value will be used as your variable time step
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

// These values will have to be tweaked for your game
float acceleration = 1f;
float gravity = 1f;
Vector2 maxVelocity = new Vector2(50f, 50f);

// Apply accelerations
velocity.X += acceleration * dt; 
velocity.Y += gravity * dt;

// Limit the maximum velocity so you don't accidentally go back in time
velocity = Vector2.Clamp(velocity, -maxVelocity, maxVelocity);

// Apply velocity
position += velocity * dt;

Step 2) Tweak the acceleration/gravity values until you get something that feels right

The next step is finding the best values for your gravity, acceleration and maximum velocity variables.

You could start with reasonable values near what you expect them to be by definition (remember that you're dealing with units per second now, not per frame).

Then temporarily add an option to your game to tweak the values during gameplay. This will let you search for the best values while trying it out at the same time. Once you find the best values, hardcode them or put them into a configuration file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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