• entries
743
1924
• views
578310

# CharacterController II, this time its personal...

Followers 0

858 views

Scrap the last entry with that stupid hack for preventing slope climbing. Turns out there is a proper geometric solution that was easier than I could have ever expected.

When the GJK returns a separation vector, you can dot this (normalized) with the world up vector to produce a value that you can use as the slope clamp. For example, less than 0.8f sets a slope limit of about 20 degrees. Could probably work out the exact relationship if I wanted to make the max slope variable based on an angle, but not for now.

Once we know we are being separated out from a slope that is too steep, first we need to get the vector that points down the face of the slope:Vec3 transformVelocity(const Vec3 &v, const Vec3 &n){ if(fabs(dotVectors(n, Vec3(0, 1, 0)) - 1) < 0.0001f) { return v; } Vec3 r = normalizeVector(reflect(v, n)); Vec3 p = perpendicular(r, n); return normalizeVector(p) * vectorLength(v);}Vec3 getDownVector(const Vec3 &n){ return transformVelocity(Vec3(0, -1, 0), normalizeVector(n));}
Now we have the unit vector pointing down the slope, and we have the separation vector representing the amount we have been pushed back out of the slope, and we know from our dot test above that the slope is too steep to climb.

So all we need to do is project the separation vector onto the down vector, then add the result to the velocity before it is applied to the position: Vec3 sep = physics.getSeparationVector(body, pos, vel); float d = dotVectors(normalizeVector(sep), Vec3(0, 1, 0)); if(d < 0.8f) { Vec3 p = getDownVector(sep); vel += p * vectorLength(sep); } vel += sep;
And the player now stops exactly at the point of contact with the slope, but gravity still pulls you down if you aren't on the floor.

I've set it up so the arrow keys rotate the world around both X and Z axis now so can easily test this against a huge range of different slopes and different configurations and so far seems fine. And doesn't feel like a hack either.

So the whole CC update step has two raycasts and one convex test now. The overall flow looks like this:

• Get the requested velocity
• Find the current floor (raycast)
• If touching the floor, and the floor angle is not a steep slope, transform the velocity by the slope normal and lock the player to the floor
• If not touching a floor, add gravity to the velocity
• Get a separation vector if any (GJK)
• Dot this with (0, 1, 0) and if that is below a threshold, use code above to project the separation vector onto the slope down vector
• Add separation vector to velocity
• Do a floor check at the new position (raycast) and lock to floor if within a threshold
• If the move distance is over a low threshold update the body position

You know, I have totally failed to find a good article on hand-written character controllers and think maybe I should write one for GDNet once I have found all the inevitable problems with my approach. There are loads of resources that work out in theory, but fall to bits when used in a real game environment. The only open source CC I know of is Bullet's which has a host of problems and is not really usable.

3
Followers 0

This is good knowledge to have! I haven't taken the time to write my own character controller (besides a simple AABB collider and stepper), but even then it's really buggy. I haven't tried yet because I just don't quite know how. I wouldn't mind getting a read of a paper form you ;) I enjoy reading about your work and this character control!

2

Interesting, how does it feel in game?  I'd love to see a demo / video of it in action.

0

## Create an account

Register a new account