Character Controller

Post Reply
tjloughl
Posts: 23
Joined: Wed Oct 03, 2007 4:03 am

Character Controller

Post by tjloughl »

Hello,
I have a character controller that uses a collision sphere for the base. When I press WASD, it applies forces in the correct direction, thus making the sphere move in that direction. But the problem is that when I run along a non-flat terrain, the character ALWAYS tries to run up the terrain. It tries to climb the hills by orienting the character so that W (forward) is always up the hill. Any ideas why this would occur?
Thanks
TJ
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Character Controller

Post by Erwin Coumans »

Can you give some more info: how do you keep the character/sphere up and prevent it from rolling?

Thanks,
Erwin
tjloughl
Posts: 23
Joined: Wed Oct 03, 2007 4:03 am

Re: Character Controller

Post by tjloughl »

Yea, I meant to say that also.
I put a constraint on the character that restrains the up from rotating (I believe I saw you suggest this on a forum post)

Code: Select all

 
btVector3 anglowerLimit = btVector3(0,0,1);
btVector3 angupperLimit = btVector3(0,0,0);
btVector3 linlowerLimit = btVector3(1,1,1);
btVector3 linupperLimit = btVector3(0,0,0);
btTransform transB;
transB.setIdentity();
					
btRigidBody* staticBody =  LocalCreateRigidBody(entityNode, 0, trans, 0);
btGeneric6DofConstraint *upConstraint =new              
     btGeneric6DofConstraint(*body,*staticBody,transB,transB,false);
					
upConstraint->setAngularLowerLimit(anglowerLimit);
upConstraint->setAngularUpperLimit(angupperLimit);
upConstraint->setLinearLowerLimit(linlowerLimit);
upConstraint->setLinearUpperLimit(linupperLimit);
m_btDynamicsWorld->addConstraint(upConstraint);
ps-no preventing of rolling. I assume the character would slide down a hill if it was steep. I'll probably put some friction on to keep from sliding too much (take it off when moving or something, i dunno yet)
DannyChapman
Posts: 84
Joined: Sun Jan 07, 2007 4:29 pm
Location: Oxford, England
Contact:

Re: Character Controller

Post by DannyChapman »

When you're on a sideways slope the contact point is to one side of the sphere centre. Perhaps when you then move forward, since the ground friction is applied to one side of the sphere centre, it results in a torque around the vertical axis, so your character rotates.

Often character controllers are better implemented as quite "unphysical" - e.g. a cylinder some distance above the ground "supported" by a raycast (or small sphere cast). This lets you handle steps as you move around, and you can smooth the vertical motion. Horizontal motion is done by setting the velocities directly (faking "inertia" in the controller/game code) prior to updating the physics. Then you might need to handle interactions with physical objects in a special way too.

The idea of this is to have your character movement very controller and predictable, so long as it doesn't interfere in a significant way with the environment. Having a character subject to real physical friction on slopes is not nice - better to decide in the game code that certain slopes are simply not traversable than mess around with tuning friction values.

However, it depends what you're trying to do...
tjloughl
Posts: 23
Joined: Wed Oct 03, 2007 4:03 am

Re: Character Controller

Post by tjloughl »

Hey thanks man, your hints were great. I got my character controller working pretty well. I locked ALL angular axes in my constraint and kept all linear free. Then, rather than applying forces, I apply velocities (applying forces doens't work well for hill climbing). Finally, I set the character's rotation by connecting its forward vector to the camera's forward vector, its up always to the world's up vector. This is very smooth and works great. Thanks again

I do have some other general questions though.
1) How would this work for characters with animations? Does the character collision volume always stay the same, even despite the animations, or do they change with the animations? What I am thinking about is a character diving over a ledge or something. How would this work with all rotation locked? (maybe unlocking temporarily?)

2) What is the best way to do melee weapons, such as a punch? I was thinking to just put a small collision geometry at the location of the fist and when it comes into contact with something it does damage to it.

3) I guess this brings me to the next question. How would I go about creating bounding geometries for a character that would register exactly where it was hit so that I know if it was a headshot, body shot, etc?

Thanks for your help!
yassim
Posts: 12
Joined: Tue Jun 26, 2007 2:33 am

Re: Character Controller

Post by yassim »

Random suggestions (but known to work):

1) designate a bone (probably the root of the anim skel) as a drive bone. When you get it animated, you move the character by this bone so it travels across the scene. When you get it in game, disconnect this bone from the skel, but keep it animating. Read the frame delta from the bone and apply it as your velocity. You may need to scale by a small magic value to over come friction, but the rest should be comthing from your anim.

2) Collision boxes or spheres should work. You may want to animate their movment relative to the character, as well as defining time windows when the attack is active. Its also cool to use one attack box even though your character could have many attacking limbs (say 2 swords).

3) Using different RBs for different targets would work. The user pointer in the RB can then backtrack to your game level structs to account for damage.

Note: for both damage type (sending and receving) they will probably want to be larger than you expect (say 10 to 20 % larger than your character body/limb, or you may find its quite hard to "hit" anything.


Hope this helps.
Post Reply