Arcanor
13-04-2007 02:52:43
I'm trying to figure out the cleanest way to deal with having a player character move at constant speed regardless of the slope of the ground they are on. Right now I am using the following code in my custom force callback:
This works perfectly well as long as the character is on level ground, but since it keeps a constant Z velocity, if the character climbs a slope it moves much faster (since it's also climbing the Y axis simultaneously). Taken to the extreme, if the character walks on an 85 degree inclined slope, the character goes shooting off high into the sky because of all this velocity!
What I think I need to do is to change the velocity vector from being a global Z vector. Instead, I should use the same velocity amount, but with a direction parallel to the ground. My problem is I haven't figured out how to do this yet.
Specifically, I am looking for:
1. An efficient way of detecting if I'm in contact with a static object in the negative Y axis (i.e. the ground, or something else I might be standing on and could push off of). I've got a function isOnGround() which uses a BasicRaycast to calculate distance to ground and if it's within tolerance limits it returns true. This does work, but it seems inefficient to call this function every frame for every moving object. I'm hoping for a cheaper alternative.
2. Once I have established ground contact, I need to determine the slope of that ground in the YZ plane, so I can set my velocity at that same angle. I haven't figured this out yet at all.
Can anyone offer me any pointers? Thanks in advance.
if (obj->isWalking())
body->setVelocity(Ogre::Vector3(orientation * Ogre::Vector3::NEGATIVE_UNIT_Z * obj->getWalkSpeed()) + Ogre::Vector3(0, velocity.y, 0));
This works perfectly well as long as the character is on level ground, but since it keeps a constant Z velocity, if the character climbs a slope it moves much faster (since it's also climbing the Y axis simultaneously). Taken to the extreme, if the character walks on an 85 degree inclined slope, the character goes shooting off high into the sky because of all this velocity!
What I think I need to do is to change the velocity vector from being a global Z vector. Instead, I should use the same velocity amount, but with a direction parallel to the ground. My problem is I haven't figured out how to do this yet.
Specifically, I am looking for:
1. An efficient way of detecting if I'm in contact with a static object in the negative Y axis (i.e. the ground, or something else I might be standing on and could push off of). I've got a function isOnGround() which uses a BasicRaycast to calculate distance to ground and if it's within tolerance limits it returns true. This does work, but it seems inefficient to call this function every frame for every moving object. I'm hoping for a cheaper alternative.
2. Once I have established ground contact, I need to determine the slope of that ground in the YZ plane, so I can set my velocity at that same angle. I haven't figured this out yet at all.
Can anyone offer me any pointers? Thanks in advance.