Hi, I have searched the forums and couldn't come up with an answer for this. Basically I have a sphere I am using as the character that the camera follows. The level has a few boxes I am using to test collision. One of the boxes spins and I am trying to figure out how I would get the sphere to spin when it is on top of the box, and also to be pushed away from the box when it collides. I have tried several different methods of doing this but with no luck. Most of the code was taken from one of sharkeys demos. I would be thankful for any help.
sync on : sync rate 60
global playerTexture as integer : playerTexture=1
global groundTexture as integer : groundTexture=2
global objectTexture as integer : objectTexture=3
global distance# as double float : distance#=85.0
global height# as double float : height#=100.0
global smooth# as double float : smooth#=2.0
makeTextures() : makeLight() : makeLevel()
backdrop on : color backdrop 0
global radius# as double float : radius# = 7.0
rem player movement vector
global vx# as double float : global vy# as double float : global vz# as double float
makePlayer()
global gravity# as double float : gravity# = -0.1
global slope# as double float : slope# = 0.5
global ground as integer : ground = 1
global jumptimer as integer : jumptimer = 0
a#=0
do
MovePlayer()
text 0,0,"Use W/D/Q/E to move and A/D to turn" : text 0,30,"Use SPACE to jump and -/+ to zoom in and out" : text 0,80,"FPS: "+str$(screen fps())
if ground=1 : text 0,120,"Touching Ground: Yes" : else : text 0,120,"Touching Ground: No" : endif
positionx = object position x(2) : positiony = object position y(2) : positionz = object position z(2)
text 0,160,"X: "+str$(positionx) : text 0,180,"Y: "+str$(positiony) : text 0,200,"Z: "+str$(positionz)
keypressed=scancode() : text 0,220, "Key Code: "+str$(keypressed)
sync
loop
function makeTextures()
rem player texture
ink rgb(255,0,0),0 : box 0,0,16,16 : get image playerTexture,0,0,16,16
rem ground texture
ink rgb(0,0,255),0 : box 0,0,16,16 : ink rgb(0,255,0),0 : circle 8,8,1 : get image groundTexture,0,0,16,16
rem texture for level objects
ink rgb(0,255,0),0 : box 0,0,16,16 : ink rgb(255,0,0),0 : circle 8,8,1 : get image objectTexture,0,0,16,16
rem clear the screen to white for lettering
ink rgb(255,255,255),0
endfunction
function makeLight()
set normalization on
lightmode=1 : hide light 0 : set ambient light 20
make light 1 : set point light 1,0,0,0 : set light range 1,2000
set shadow position lightmode,0,0,0 : position light 1,0,300,0
endfunction
function makeLevel()
shader=1
rem ground
make object box 1,1000,5,1000 : position object 1,0,0,0 : texture object 1,groundTexture : scale object texture 1,-30,-30
sc_setupComplexObject 1,1,2
rem box with ramps
make object box 4, 20,19,20 : position object 4,100,10,45
rem ramps
make object box 5,20,0.5,40 : make object box 6,20,0.5,40
position object 5,100,10,72 : position object 6,100,10,18
xrotate object 5,30 : xrotate object 6,-30
rem steep slope
make object box 7,20,0.5,40 : position object 7,50,25,18 : xrotate object 7,-60
rem spinning box
make object box 8,20,20,20 : position object 8,150,15,45
rem texture, set collision, and shadows on objects 4-8
for tx=4 to 8
texture object tx,objectTexture
sc_setupComplexObject tx,1,2
set shadow shading on tx
next tx
rem all level objects are group 1
endfunction
function makePlayer()
make object sphere 2,radius#*2.0
set shadow shading on 2
texture object 2,playerTexture
position object 2,100,9,-20
sc_setupObject 2,0,1
rem player has no group
endfunction
function movePlayer()
rem rotate object with A/D keys
if keystate(30)=1
yrotate object 2,object angle y(2)-3.0
endif
if keystate(32)=1
yrotate object 2,object angle y(2)+3.0
endif
oldx# = object position x(2)
oldy# = object position y(2)
oldz# = object position z(2)
rem apply gravity, and user changes to movement
angy# = object angle y(2)
vx# = 0
vz# = 0
rem if player is jumping or falling then apply 'normal' gravity
rem 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#
rem Q (16) key moves player left, E (18) moves right, W (17) moves forward, and S (31) for backwards
if keystate(18)=1 then vx# = vx# + cos(angy#) : vz# = vz# - sin(angy#)
if keystate(16)=1 then vx# = vx# - cos(angy#) : vz# = vz# + sin(angy#)
if keystate(31)=1 then vx# = vx# - sin(angy#) : vz# = vz# - cos(angy#)
if keystate(17)=1 then vx# = vx# + sin(angy#) : vz# = vz# + cos(angy#)
rem only jump if on ground, and a certain time after last jump
if ground=1
if spacekey()=1 and jumptimer=0 then vy# = vy# + 3.0 : jumptimer = 20
endif
rem this would be the player's final position without collision
x# = oldx#+vx#
y# = oldy#+vy#
z# = oldz#+vz#
rem first handle gravity - seperated from horizontal movement
rem to achieve a more realistic effect
rem Method: simple - cast a sphere vertically down, if it hits the level then
rem position the object there (sticky collision) then move
rem on to horizontal movement
rem more complex - if we were to only use the simple method the player would be
rem allowed to climb almost vertical slopes. Alternative is to
rem get the normalY direction to work out how flat the gorund
rem below the player is, 0-slope# is flatter, slope#-1 is steeper.
rem if it's flat, use sticky collision, if it's steep slide the
rem player down the slope. Changing slope# determines how steep the
rem player can climb. NOTE: this also effects stairs.
collide = sc_SphereCastGroup(1,oldx#,oldy#,oldz#,oldx#,oldy#+vy#,oldz#,radius#,0)
if collide>0
rem how flat is this ground
ny# = sc_getCollisionNormalY()
if abs(ny#)>slope#
rem FLAT, stick
oldy# = sc_getStaticCollisionY()
else
rem STEEP, slide
x# = x# - oldx# : z# = z# - oldz#
oldx# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
oldz# = sc_getCollisionSlideZ()
x# = x# + oldx# : z# = z# + oldz#
endif
rem ny#<0 means the player has hit a ceiling rather than a floor
if ny#>slope#
rem only on ground if standing on flat ground
ground = 1
vy# = 0
else
ground = 0
rem if player has hit a flat ceiling then stop vy# movement
if ny#<-slope# then vy# = gravity#
endif
else
rem nothing below player, not on ground, add vertical speed to player
oldy# = oldy# + vy#
ground = 0
endif
rem jumptimer will decrease only when player is back on ground
rem creates a pause between two successive jumps
if ground = 1 and jumptimer>0 then dec jumptimer
rem handle horizontal movement as sliding
rem player only collides with group 1 (level) objects and moves freely through others
collide = sc_SphereSlideGroup(1,oldx#,oldy#,oldz#,x#,oldy#,z#,radius#,0)
if collide>0
rem if hit, reposition player, halt movement vector
x# = sc_getCollisionSlideX()
oldy# = sc_getCollisionSlideY()
z# = sc_getCollisionSlideZ()
vx# = 0
vz# = 0
rem possible code for giving the player a jumping help up stairs...
rem 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
rem position the player
position object 2,x#,oldy#,z#
rem zoom in
if keystate(13)=1 and distance#>40.0
dec distance#,2.0
endif
rem zoom out
if keystate(12)=1 and distance#<150.0
inc distance#,2.0
endif
set camera to follow x#,oldy#,z#,WrapValue(Object Angle Y(2)),distance#,height#,smooth#,0
point camera x#,oldy#+40,z#
yrotate object 8,object angle y(8)+1
sc_updateObject 2
endfunction
if memblock exist(1) then delete memblock 1