Hey all, I've implemented Sparky's collision demo in my own code and adapting it to suit my needs. Things are going okay and I understand most of it.
However, I'm not able to raise the movement speed of my character controller... currently the character is walking way too slow.
Here's the code:
oldx# = object position x(player_obj)
oldy# = object position y(player_obj)
oldz# = object position z(player_obj)
// apply gravity, and user changes to movement
angy# = object angle y(player_obj)
vx# = 0
vz# = 0
// if player is jumping or falling then apply 'normal' gravity
// if not attempt to keep the player stuck to the floor
if vy#=0 and jumptimer=0 then vy# = vy# + 10*gravity# else vy# = vy# + gravity#
if keystate(32)=1 : vx# = vx# + cos(angy#) : vz# = vz# - sin(angy#) : endif
if keystate(30)=1 : vx# = vx# - cos(angy#) : vz# = vz# + sin(angy#) : endif
if keystate(31)=1 : vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#) : endif
if keystate(17)=1 : vx# = vx# + sin(angy#) : vz# = vz# + cos(angy#) : endif
// only jump if on ground, and a certain time after last jump
if ground=1
// Original jump code
`if spacekey()=1 and jumptimer=0 then vy# = vy# + 3.0 : jumptimer = 20
// Temp: Jump higher
if spacekey()=1 and jumptimer=0 then vy# = vy# + 6.0 : jumptimer = 20
endif
// this would be the player's final position without collision
x# = oldx#+vx#
y# = oldy#+vy#
z# = oldz#+vz#
// first handle gravity - seperated from horizontal movement
// to achieve a more realistic effect
// Method: simple - cast a sphere vertically down, if it hits the level then
// position the object there (sticky collision) then move
// on to horizontal movement
// more complex - if we were to only use the simple method the player would be
// allowed to climb almost vertical slopes. Alternative is to
// get the normalY direction to work out how flat the gorund
// below the player is, 0-slope# is flatter, slope#-1 is steeper.
// if it's flat, use sticky collision, if it's steep slide the
// player down the slope. Changing slope# determines how steep the
// player can climb. NOTE: this also effects stairs.
collide = sc_SphereCastGroup(1,oldx#,oldy#,oldz#,oldx#,oldy#+vy#,oldz#,radius#,0)
if collide>0
// how flat is this ground
ny# = sc_getCollisionNormalY()
if abs(ny#) > slope#
// FLAT, stick
oldy# = sc_getStaticCollisionY()
else
// STEEP, slide
x# = x# - oldx# : z# = z# - oldz#
oldx# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
oldz# = sc_getCollisionSlideZ()
x# = x# + oldx# : z# = z# + oldz#
endif
// ny#<0 means the player has hit a ceiling rather than a floor
if ny#>slope#
// only on ground if standing on flat ground
ground = 1
vy# = 0
else
ground = 0
// if player has hit a flat ceiling then stop vy# movement
if ny#<-slope# then vy# = gravity#
endif
else
// nothing below player, not on ground, add vertical speed to player
oldy# = oldy# + vy#
ground = 0
endif
// jumptimer will decrease only when player is back on ground
// creates a pause between two successive jumps
if ground = 1 and jumptimer>0 then dec jumptimer
// handle horizontal movement as sliding
// player only collides with group 1 (level) objs and moves freely through others
collide = sc_SphereSlideGroup(1,oldx#,oldy#,oldz#,x#,oldy#,z#,radius#,0)
if collide>0
// if hit, reposition player, halt movement vector
x# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
z# = sc_getCollisionSlideZ()
vx# = 0
vz# = 0
// possible code for giving the player a jumping help up stairs...
// might be useful if slope# is set very high but stairs are still required
``dy# = oldy#-sc_getStaticCollisionY()
``if dy#<slope# and dy#>0 and ground=1 then vy# = 0.5
endif
// position the player
position object player_obj,x#,oldy#,z#
Any help is much appreciated! Thanks!
Regards Sph!nx