Sign in to follow this  
Followers 0
TheVastBernie

Collision detection - Reduce speed at steep hills

3 posts in this topic

Hi, I got triangle based collision detection implemetation working in my game, I now want to add realistic movement behavior. For that I first added gravity (9.81 * gravityNormal each second to my players velocity) and movement (I move him by setting the XZ values of his velocity according to his movement direction).
Gravity works as planned, he accelerates, falls, and hits the ground (though it's a bit slow). The movent works as well, he climbs over smaller bumps and hills.
The problem I have, is that he's able to climb really steep hills and buildings, provided they're at an slight angle. I know this is due to the fact that I reset the velocity at an collision. When the player hits the ground there can't be any Y movement anymore, so it gets set to 0. Then, with e.g. an X velocity of 3 the player is easily able to climb a hill (his sliding plane makes him slide up) beacuse the gravity didn't have time to build up enough vertical movement.
 
I thought I'm correct with the math behind my 'accurate' physics approach, but the hillclimbing thing got me thinking I might be wrong.
 
I tried to overcome this by ignoring approaching walls with too steep angles, but this meant that ground debris (usually visualized as simple boxes) was to steep (since player collides with side and the side of a box has a steep angle).
 
My collision code (without triangle test):
 
private void ComputeMovement(ref Vector3 position, ref Vector3 velocity, Vector3 radius, int recursive)
{
  if (recursive > 8 || velocity == Vector3.Zero)
    return;
  
  // Ellipsioid space conversion
  position = position / radius;
  velocity = velocity / radius;
  
  float t = 1;
  Vector3 collisionPoint = Vector3.Zero;
  Vector3 planeNormal = Vector3.Zero;
  
  // Compute trianle collision detection (t = factor at wich collision occurs)
  CalculateCollision(position, velocity, radius, out t, out collisionPoint, out planeNormal);
              
  Vector3 newPosition = position + t * velocity;
              
  if (t < 1)
  {
    // Create sliding plane
    Vector3 collisionNormal = Vector3.Normalize(position - collisionPoint);
    Vector3 destinationPosition = position + velocity;
    Plane slidingPlane = GeometryMath.PlaneFromPointNormal(collisionPoint, collisionNormal);
    Vector3 newVelocity = (destinationPosition - slidingPlane.SignedDistance(destinationPosition) * slidingPlane.Normal) - collisionPoint;
    
    // Check for steep hill
    float normalDotVelocity = Vector3.Dot(collisionNormal, velocity);
    if (Math.Abs(planeNormal.Y) < 0.707f && normalDotVelocity < 0)
      newVelocity.Y = 0;
    
    // Compute recursive function
    position = newPosition * radius;
    velocity = newVelocity * radius;
    ComputeMovement(ref position, ref velocity, radius, recursive + 1);
    return;
  }
  
  position = newPosition * radius;
  velocity = velocity * t * radius;
}

 

 

 
There really must be something wrong, can't be that I can only have one or the other.
Would really appreciate any help on this.
0

Share this post


Link to post
Share on other sites

Yup, "Math.Abs(planeNormal.Y) < 0.707f" controlls the steepness of the collision plane and "normalDotVelocity < 0" verifies that I'm only braking the players Y movement when going towards the wall, since I still want to be able to slide down, but not walk up.

0

Share this post


Link to post
Share on other sites

You need to implement stepping, check this article, basically what you do is trace your character bounding volume in the movement direction about knee high up from the ground and then down, if you did not hit anything too steep, you allow the move, if you did you don't.

 

This has the effect of simulating bipedal walking, so any debris that the character would be able to just climb, will be climbed, including stairs, curbs, etc, but a wall taller than about its waist will stop it.

 

This is exactly what Quake 3 does, and I imagine they kept that code for ID Tech 4.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0