Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark Physics & Dark A.I. & Dark Dynamix / [Dark Dynamix] Custom/manual character controller

Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 3rd Jul 2012 18:25 Edited at: 3rd Jul 2012 18:47
Thought I'd make a new thread to detail my findings on a manual character controller (CC), as I think I've cracked it.

A quick explanation for anyone who reads this and hasn't seen mine and Matty's discussions: character controllers are great for quickly getting a character to run around the level, but as soon as you want your CCs to be pushed around by other objects (moving platforms, lifts, boulders, moving walls etc) they're useless, as they don't offer this functionality. They can't be affected by the movement of dynamic actors in the scene.

I tried quite a long time to add in custom code to get them to do what I wanted, but in the end, and after some research, the general consensus seems to be to not use CCs. They're fine for quick testing and simple games, but when you need to start doing more, drop them. Don't try to force them.

My Solution

The general principle is a dynamic capsule, which cannot rotate in anyway. It hovers above the ground (to allow it to climb stairs and not get caught on them), and it's velocity is manually set, rather than applying forces, so you have total control over the speed. You could use forces. I'm sure that'd work too, although I would expect the speeds would be hard to predict.

Firstly, you need to setup your own capsule to act as the CC. Here's mine, with some custom functions but it should give you an idea.

The height of the capsule and the vertical shift of the box is key. This will define where the capsule sits within your player model. You don't want the bottom of the capsule to be at the base of the player's feet, as we're going to hover the capsule above the ground to allow the player to climb stairs. So basically, this area is where you make adjustments for the steepness of stairs you want to be able to climb.

Here's the offset object function, in case it's useful to you:

Now your custom controller is setup, you move it around the level using this command:
Quote: "dynSetLinearVelocity(dbObj,fltXSpd,fltYSpd,fltZSpd);"

How you calculate your X,Y,Z speeds is up to you and there are loads of different ways of doing this.

The last step is to hover the controller above the ground so that it can move smoothly and climb stairs, and it also allows you to add jumping. The basic principle is to cast a ray down from the character capsule and find out where the floor is. Then reposition the capsule so it's hovering above the floor if it's too close. Here's my code stub for that ...

With your own movement code for x,y,z speeds, that should have you moving about.

The last thing is character rotation. Since the yRotation of the capsule is locked, you will have to manually set the yRotation of your character model every loop. Dynamix will overwrite this rotation and set it to the rotation of the capsule every loop, so you'll have to keep track of the angles in variables and set the model yourself.

So to summarize:
1. Configure a capsule for your character, such that, with the origin of the character model at it's feet and on the floor, the capsule is floating above ground height. Lock out all rotations and gravity.
2. Move the capsule around by setting its velocity.
3. Make sure it hovers above the ground by casting a ray downwards and repositioning it when it gets too close to the floor.

And that's it. I appreciate this isn't a working example but hopefully these pointers will give you an idea of how to implement your own controller and do away with the limiting standard PhysX CC.

Matty H
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 3rd Jul 2012 19:22
Great resource Fallout.

Never got much time today but will look at this tomorrow and perhaps turn it into an example for the next update.

Does it work well with your moving walls?

Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 3rd Jul 2012 19:40
So far, seems to be the perfect solution. There are a few negatives ...

1. There seems to be a bit more bumping up and down staircases. With a normal CC, I would imagine that is some smoothing going on when you hop up stairs. With this mechanism, it's a sudden replacement when the downwards raycast detects it's too close to the floor and bumps upwards. Should easily be solvable with some smoothing though.

2. There's a gap under the capsule where it's hovering, so really small objects at ground height won't be interacted with

Aside from that, it seems pretty good. It is a normal dynamic actor, so the walls push it perfectly. A side effect is that when I bounce a grenade off the wall into my face, my character is knocked back a bit too.

I much prefer this because now all those little physics interactions are handled automatically. Anything that hits the player will push him around now, without having to manually code it.

Login to post a reply

Server time is: 2019-12-10 16:56:02
Your offset time is: 2019-12-10 16:56:02