I'm trying to model character movement a bit better using "shapecasts." What I can't figure out is how to avoid climbing steep slopes.
Here's the algorithm I use to shapecast:
The simplest way I can make it work is to shapecast up, forward, then back down to try to touch the ground. This way, the character can both up and down slopes and stairs, and not get stuck on little pieces of rubble on the ground or rough surfaces. However, it fails when the ceiling slopes downward, and I can't figure out how to get it so the player treats steep slopes like walls, while still being able to climb them. Any ideas?
Here's the algorithm I use to shapecast:
Code:
forward = normalized direction to move in
distance = distance to move in that direction
startingPosition = GetBodyPosition()
step = width of the thinnest shape in the body / 2
distanceSoFar = 0
while distanceSoFar < distance:
// Move the body forward
distanceSoFar = max( distance, distanceSoFar + step )
SetBodyXForm( startingPosition + forward * distanceSoFar )
// Figure out the maximum penetration in the forward direction, updating contacts
maxPenetration = 0
for each contact:
contact.update()
penetrationScale = dot( point.normal, forward )
if penetrationScale <= epsilon:
continue
for each contact point:
maxPenetration = max( -point.separation * penetrationScale, maxPenetration )
// If there is any penetration, we're done
// move back a bit so it's no longer penetrating
if maxPenetration > 0:
if maxPenetration > epsilon:
SetBodyXForm( startingPosition + forward * ( distanceSoFar - penetration ) )
break
// Return the distance traveled
return distanceSoFar
distance = distance to move in that direction
startingPosition = GetBodyPosition()
step = width of the thinnest shape in the body / 2
distanceSoFar = 0
while distanceSoFar < distance:
// Move the body forward
distanceSoFar = max( distance, distanceSoFar + step )
SetBodyXForm( startingPosition + forward * distanceSoFar )
// Figure out the maximum penetration in the forward direction, updating contacts
maxPenetration = 0
for each contact:
contact.update()
penetrationScale = dot( point.normal, forward )
if penetrationScale <= epsilon:
continue
for each contact point:
maxPenetration = max( -point.separation * penetrationScale, maxPenetration )
// If there is any penetration, we're done
// move back a bit so it's no longer penetrating
if maxPenetration > 0:
if maxPenetration > epsilon:
SetBodyXForm( startingPosition + forward * ( distanceSoFar - penetration ) )
break
// Return the distance traveled
return distanceSoFar
The simplest way I can make it work is to shapecast up, forward, then back down to try to touch the ground. This way, the character can both up and down slopes and stairs, and not get stuck on little pieces of rubble on the ground or rough surfaces. However, it fails when the ceiling slopes downward, and I can't figure out how to get it so the player treats steep slopes like walls, while still being able to climb them. Any ideas?