Implementing gravity in the game, the character keeps bouncing up and down slightly when he hits the ground, upon debugging the character is essentially bouncing on and off the ground. Not quite sure how to iron out the issue any help here would be greatly appreciated.

The constant value

```
private float gravity = 0.09f;
```

adding to the velocity if the character is on the ground

```
if (!isOnGround)
velocity.Y += gravity;
```

adding the characters overall velocity to the overall position.

```
position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
```

Part of the collision method which determines if the character is on the ground or not.

```
for (int y = topTile; y <= bottomTile; ++y)
{
for (int x = leftTile; x <= rightTile; ++x)
{
// If this tile is collidable,
TileCollision collision = Level.GetCollision(x, y, tileMap);
if (collision != TileCollision.Passable)
{
// Determine collision depth (with direction) and magnitude.
Rectangle tileBounds = Level.GetBounds(x, y);
Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (depth != Vector2.Zero)
{
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);
// Resolve the collision along the shallow axis.
if (absDepthY <= absDepthX || collision == TileCollision.Platform)
{
// If we crossed the top of a tile, we are on the ground.
if (previousBottom <= tileBounds.Top)
isOnGround = true;
```

I am afraid I do not fully understand your collision code yet: if there is an off-by-one-pixel error somewhere, it will be hard to spot. But your physics code needs a few fixes already:

- velocity updates should depend on the timestep, even if gravity is a constant
- vertical velocity should be set to zero if on the ground
- adding the “overall velocity” is incorrect; it’s the
**average**velocity for the duration of the last frame that needs to be added. This article should explain why.

Leading to the following code:

```
Vector2 oldvelocity = velocity;
if (!isOnGround)
// This will require tweaking the gravity value
velocity.Y += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
else
velocity.Y = 0.0;
position += 0.5 * (oldvelocity + velocity)
* (float)gameTime.ElapsedGameTime.TotalSeconds;
```

December 14, 2011 02:15 AM

- Serverfault Help
- Superuser Help
- Ubuntu Help
- Webapps Help
- Webmasters Help
- Programmers Help
- Dba Help
- Drupal Help
- Wordpress Help
- Magento Help
- Joomla Help
- Android Help
- Apple Help
- Game Help
- Gaming Help
- Blender Help
- Ux Help
- Cooking Help
- Photo Help
- Stats Help
- Math Help
- Diy Help
- Gis Help
- Tex Help
- Meta Help
- Electronics Help
- Stackoverflow Help
- Bitcoin Help
- Ethereum Help