There is a character controller example that comes with Dark Dynamix. It includes sliding collision, stairs, jumping and interactions with dynamic objects.
Rem Project: Character Controller
Rem Created: Tuesday, February 28, 2012
Rem ***** Main Source File *****
//-- Walk = Arrow keys
//-- Jump = Space key
//-- Turn = Mouse
//-- Be sure to add "PhysX Constants.dba" to your solution.
Global groundID = 1
Global characterID = 2
Global boxID = 3
Global box2ID = 4
Global vectorID = 1
//-- Gravity for character
Global GRAVITY# = 0.16333 `9.8 / 60
//-- Character variables
Global move# = 0.0
Global strafe# = 0.0
Global xDisplacement# = 0.0
Global yDisplacement# = 0.0
Global zDisplacement# = 0.0
Global collisionFlag = 0
sync on
sync rate 60
autocam off
load object "../../Media/Babe/Babe.dbo", characterID
load image "../../Media/Babe/babe.dds", 1
texture object characterID, 1
position object characterID, 0, 20, -650
rotate object characterID, 0, 180, 0
scale object characterID, 2500, 2500, 2500
load object "../../Media/TestLevel/TestLevel.dbo", groundID
make object cube boxID, 40
make object cube box2ID, 40
position object boxID, 0, 50, -250
position object box2ID, 0, 50, -450
color object boxID, rgb(0, 255, 0)
color object box2ID, rgb(0, 0, 255)
DYN START
DYN SET GRAVITY 0, -9.8 * 10, 0
//-- Set to 1.0 to visualize debug info
DYN SET PARAMETER NX_VISUALIZATION_SCALE, 0.0
DYN SET PARAMETER NX_VISUALIZE_COLLISION_SHAPES, 1.0
DYN MAKE CONTROLLER CAPSULE characterID, object size y(characterID, 1) / 2, 45, 10
//-- Adjust height of controller, the debug renderer does not represent controllers
//-- very well so this is mostly trial and error.
DYN CONTROLLER CAPSULE SET HEIGHT characterID, object size y(characterID, 1) * 0.9
DYN LOAD TRIANGLE MESH "../../Media/TestLevel/TestLevel.TMesh", 1
DYN MAKE TRIANGLE MESH groundID, 1
//-- Both spheres are dynamic, but we will only handle
//-- interactions for one of them. Be aware that both
//-- spheres can interact with the controller but the
//-- controller will only interact with one sphere.
DYN MAKE BOX boxID, 0.001
DYN MAKE BOX box2ID, 0.001
//-- Set linear damping to simulate air friction
DYN SET LINEAR DAMPING boxID, 0.5
DYN SET LINEAR DAMPING box2ID, 0.5
//-- Collect character-shape interations
//-- When this is true we need to be sure to
//-- process the information.
DYN CONTROLLER COLLECT S HITS 1
//-- Create our vector we will use to collect
//-- information.
V = make vector3(vectorID)
DYN SIMULATE
do
DYN FETCH RESULTS
DYN UPDATE
DYN DEBUG RENDER
updateDisplacement(2.0)
updateInteractions(10000.0)
DYN SIMULATE
set camera to object orientation characterID
turn camera right 180
pitch camera down 15
position camera object position x(characterID), object position y(characterID) + 20, object position z(characterID)
move camera -100
sync
loop
DYN FETCH RESULTS
DYN STOP
END
FUNCTION updateDisplacement(speed#)
//-- Forward/Backward
move# = 0.0
if upkey()
move# = move# - speed#
endif
if downkey()
move# = move# + speed#
endif
//-- Side/Side
strafe# = 0.0
if leftkey()
strafe# = strafe# + speed#
endif
if rightkey()
strafe# = strafe# - speed#
endif
//-- Up/Down
onGround = collisionFlag && NXCC_COLLISION_DOWN
//-- Gravity
if onGround
yDisplacement# = 0.0
else
yDisplacement# = yDisplacement# - GRAVITY#
endif
//-- Jump
if onGround > 0 and spacekey()
yDisplacement# = yDisplacement# + 2.5
endif
//-- Animation
if onGround and (move# or strafe#)
set object frame characterID, object frame(characterID) + 1
if object frame(characterID) > total object frames(characterID)
set object frame characterID, 5
endif
endif
//-- Turn object
rotate object characterID, 0, object angle y(characterID) + mousemovex() * 0.2, 0
angle# = object angle y(characterID)
//-- move + strafe
xDisplacement# = move# * sin(angle#) + strafe# * sin(angle# + 90.0)
//-- move + strafe
zDisplacement# = move# * cos(angle#) + strafe# * cos(angle# + 90.0)
//-- This command returns information on whether the controller is touching anything
collisionFlag = DYN CONTROLLER MOVE(characterID, xDisplacement#, yDisplacement#, zDisplacement#)
ENDFUNCTION
FUNCTION updateInteractions(strength#)
yText = 0
while DYN CONTROLLER S HIT GET DATA()
objectA = DYN CONTROLLER S HIT GET CONTROLLER()
objectB = DYN CONTROLLER S HIT GET SHAPE()
if objectB = boxID
//-- Add force in direction of contact
DYN CONTROLLER S HIT GET DIRECTION vectorID
multiply vector3 vectorID, strength#
DYN ADD FORCE boxID, x vector3(vectorID), y vector3(vectorID), z vector3(vectorID)
center text screen width() / 2, yText, "PUSHING BOX"
yText = yText + 15
endif
if objectB = box2ID
center text screen width() / 2, yText, "TOO HEAVY"
yText = yText + 15
endif
endwhile
ENDFUNCTION
