now i use the collision dll
http://forum.thegamecreators.com/?m=forum_view&b=5&t=74762&p=0 but my object is stuck in the ground :S any help?
Rem Project: FPS
Rem Created: Friday, May 14, 2010
Rem ***** Main Source File *****
`Created by
`Date Started:
`Setup Client Options
SYNC ON:SYNC RATE 0:HIDE MOUSE
autocam off
randomize timer()
global numSpheres as integer : numSpheres = 5
global radius# as double float : radius# = 7.0
global littleRadius# as double float : littleRadius# = 2.0
makeExtraSpheres(numSpheres)
global rtimer as integer
global stimer as integer
global vtimer as integer
rem player movement vector
global vx# as double float
global vy# as double float : vy#=0
global vz# as double float
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
global syncmode as integer : syncmode = 60
global view as integer : view = 1
global hide as integer : hide = 0
` Our character
MAKE OBJECT SPHERE 11,50:COLOR OBJECT 11,RGB(000,255,000)`POSITION OBJECT 11,0,1000,-400
` An enemy
MAKE OBJECT SPHERE 2,50:COLOR OBJECT 2,RGB(255,000,000):POSITION OBJECT 2,130,0,0
` A large wall
`MAKE OBJECT BOX 3 ,500,400,10:POSITION OBJECT 3,0,0,150
` An ammo crate
MAKE OBJECT BOX 4,15,20,50:COLOR OBJECT 4,RGB(000,255,255):POSITION OBJECT 4,-40,0,0
` Some ground to walk on
`MAKE OBJECT PLAIN 5,1000,1000:XROTATE OBJECT 5,-90
`color object 5, RGB(255,0,0)
for n=1 to 4
If object exist (n) =1
sc_setupObject n,1,1
endif
next n
for i=6 to 15
If object exist (i) =1
sc_setupObject i,1,1
endif
next i
sc_setupObject 11,0,1
`gun
load object "rpk-3ds.3ds", 10
position object 10,0,0,-200
`map
load object "map2.x", 5
position object 5,0,0,0
sc_setupcomplexObject 5,1,2
`sound
load sound "shot.wav", 1
load sound "emptygun.wav", 2
load sound "reload.wav", 3
load sound "reload.wav", 4
color backdrop rgb(10,10,10)
`Make sure all the objects are on ground level
POSITION OBJECT 4, OBJECT POSITION X(4), OBJECT SIZE Y(4)/2,OBJECT POSITION Z(4)
POSITION OBJECT 11, OBJECT POSITION X(11), OBJECT SIZE Y(11)/2,OBJECT POSITION Z(11)
POSITION OBJECT 2, OBJECT POSITION X(2), OBJECT SIZE Y(2)/2,OBJECT POSITION Z(2)
`Create all media needed
`Add limb for intersection checks
MAKE OBJECT SPHERE 9999,10
MAKE MESH FROM OBJECT 1,9999
DELETE OBJECT 9999
`Bullet Intersection Limb
ADD LIMB 11,1,1
OFFSET LIMB 11,1,0,0,500
HIDE LIMB 11,1
remstart
`Front Intersection Limb
ADD LIMB 11,2,1
OFFSET LIMB 11,2,0,0,OBJECT SIZE Z(11)/2
HIDE LIMB 11,2
`Back Intersection Limb
ADD LIMB 11,3,1
OFFSET LIMB 11,3,0,0,-OBJECT SIZE Z(11)/2
HIDE LIMB 11,3
`Right Intersection Limb
ADD LIMB 11,4,1
OFFSET LIMB 11,4,OBJECT SIZE X(11)/2,0,0
HIDE LIMB 11,4
`Left Intersection Limb
ADD LIMB 11,5,1
OFFSET LIMB 11,5, -OBJECT SIZE X(11)/2,0,0
HIDE LIMB 11,5
`bottom intersection check
ADD LIMB 11,6,1
OFFSET LIMB 11,6, -OBJECT SIZE y(11)/2,0,0
HIDE LIMB 11,6
`top blabla
ADD LIMB 11,7,1
OFFSET LIMB 11,7, OBJECT SIZE y(11)/2,0,0
HIDE LIMB 11,7
remend
`<!! LIMB PART !!>
`Make an object for our mesh to be created from
MAKE OBJECT TRIANGLE 99,0,0,0,0,0,0,0,0,0
`Make a mesh from that object so our limb knows what to look like
MAKE MESH FROM OBJECT 5,99
`Delete the object since we only needed it for the mesh creation
DELETE OBJECT 99
`Add the limb to the user's object.
`The syntax is: ADD LIMB Object Number, Limb Number, Mesh Number
`The limb number must be the next limb in the limb count. So
`if the object has 5 limbs already, to add another limb it's
`number must be 6. Dynamic object's like .x's and .3ds may have
`many limbs, so we need to use the PERFORM CHECKLIST FOR OBJECT
`LIMBS and `CHECKLIST QUANTITY to figure out what our limb
`number should be.
PERFORM CHECKLIST FOR OBJECT LIMBS 11
Limb#=CHECKLIST QUANTITY()
ADD LIMB 11,Limb#,5
`Now we need to offset the limb to be beside the user
OFFSET LIMB 11,Limb#,OBJECT SIZE X(11)/3,-15,220
`Now we'll hide the limb for even less lag
HIDE LIMB 11,Limb#
`Finally, we're going to use the GLUE OBJECT TO LIMB command
`to make sure the gun stays at the limb position and takes on
`the limb's rotation at all times
rotate object 10,180,0,180
GLUE OBJECT TO LIMB 10, 11, Limb#
`Define the main variables needed
EnemyHP=100` Enemy’s Health
Ammo=30` Starting Ammo
MaxAmmo=30` Maximum amount of Ammo
Clips=3` Starting Clips
MaxClips=3` Maximum amount of Clips
Jumping=0
ScreenWidth#=SCREEN WIDTH()
ScreenHeight#=SCREEN HEIGHT()
`Start main DO/LOOP
DO
handleExtraSpheres(numSpheres)
`Screen FPS
FPS=SCREEN FPS()
TEXT 0,0,"FPS: "+STR$(FPS)
MovePlayer()
`Movement Controls
remstart
IF KEYSTATE(17)=1
XAngle#=OBJECT ANGLE X(11)
XROTATE OBJECT 11,0
MOVE OBJECT 11,.5
XROTATE OBJECT 11,XAngle#
ENDIF
IF KEYSTATE(31)=1
XAngle#=OBJECT ANGLE X(11)
XROTATE OBJECT 11,0
MOVE OBJECT 11,-.5
XROTATE OBJECT 11,XAngle#
ENDIF
IF KEYSTATE(30)=1 THEN MOVE OBJECT LEFT 11,.3
IF KEYSTATE(32)=1 THEN MOVE OBJECT RIGHT 11,.3
remend
`Define variables for lag reduction
X#=OBJECT POSITION X(11)
Y#=OBJECT POSITION Y(11)
Z#=OBJECT POSITION Z(11)
`Camera Positioning
POSITION CAMERA X#,Y#+3,Z#
`Space key Controls
remstart
IF SPACEKEY()=1 AND Jumping=0 THEN Jump=1
`JUMP Variable Definition
IF Jump=1
Jump_Speed#=.5
Jumping=1
Jump=0
ENDIF
`Jumping
IF Jumping=1
DEC Jump_Speed#,.00175
POSITION OBJECT 11,X#,Y#+Jump_Speed#, Z#
ENDIF
remend
`Mouse Camera Movement
Camy#=Camy#+MOUSEMOVEX()*.1
Camx#=Camx#+MOUSEMOVEY()*.1
IF Camx#>90 AND Camx#<135 THEN Camx#=90
IF Camx#>270 AND Camx#<225 THEN Camx#=90
YROTATE CAMERA Camy#
XROTATE CAMERA Camx#
YROTATE OBJECT 11,Camy#
XROTATE OBJECT 11,Camx#
`Mouseclick Shooting
if shoot = 1 then inc Timeshoot#,.02
if Timeshoot#>=1
shoot=0
Timeshoot#=0
endif
If Ammo = 1 then play sound 2
IF KEYSTATE(19)=0 AND MOUSECLICK()=1 and shoot=0 and reload = 0
IF Ammo>0
DEC Ammo,1
shoot = 1
play sound 1
endif
IF OBJECT EXIST(2)=1
IF Ammo>0 AND INTERSECT OBJECT (2, LIMB POSITION X(11,1), LIMB POSITION Y(11,1), LIMB POSITION Z(11,1), X#,Y#,Z#)>0 THEN DEC EnemyHP,4
ENDIF
ENDIF
`Check if the enemy should die, if so delete it
IF EnemyHP=0 AND OBJECT EXIST(2)=1 THEN DELETE OBJECT 2
`Reloading (press R)
IF MOUSECLICK()=0 AND KEYSTATE(19)=1 AND Ammo<maxammo And clips > 0
Reload=1
loop sound 3
endif
if reload =0
stop sound 3
endif
`Reloading Part 2 (delay)
IF Reload=1 THEN INC Time#,.001
IF Time#>=1 THEN Ammo=MaxAmmo:DEC Clips,1:Time#=0:Reload=0
`Ammo Crate code (obtain ammo from ground)
If object exist(4)=1
IF clips<Maxclips AND INTERSECT OBJECT (4, X#,Y#-50,Z#, X#,Y#,Z#)>0 and reload = 0
CENTER TEXT ScreenWidth#/2,ScreenHeight#/2,"Press F to pick up"
IF keystate(33)=1
play sound 4
INC Clips,(MaxClips-Clips)
`INC Ammo,(MaxAmmo-Ammo)
DELETE OBJECT 4
ENDIF
Endif
ENDIF
`Display text above enemy and crate object
IF OBJECT EXIST(2)=1 THEN CENTER TEXT OBJECT SCREEN X(2),OBJECT SCREEN Y(2)-70,"Enemy Health: "+str$(EnemyHP)
IF OBJECT EXIST(4)=1 THEN CENTER TEXT OBJECT SCREEN X(4),OBJECT SCREEN Y(4)-20,"Ammo Crate"
`Display our Ammo and Clips on-screen
TEXT 0,ScreenHeight#-50,"Ammo: "+str$(Ammo)
TEXT 0,ScreenHeight#-40,"Clips: "+str$(Clips)
`Show that we’re reloading
IF Reload=1 THEN CENTER TEXT ScreenWidth#/2,ScreenHeight#/2,"Reloading..."
`Add a target reticule to the screen
CIRCLE ScreenWidth#/2,ScreenHeight#/2,5
`Check for collision with objects, and handle appropriately
remstart
`Ground Intersection Check
IF INTERSECT OBJECT (5,X#,(Y#-OBJECT SIZE Y(11)/2),Z#,X#,Y#,Z#)>0
Jumping=0
ELSE
IF Jumping=0
Jumping=1: Jump_Speed=-1
ENDIF
ENDIF
`Front Intersection Check
IF INTERSECT OBJECT (5,LIMB POSITION X(11,2),LIMB POSITION Y(11,2),LIMB POSITION Z(11,2),X#,Y#,Z#)
`If intersection infront, move the user backwards
MOVE OBJECT 11,-1
ENDIF
`Back Intersection Check
IF INTERSECT OBJECT (5,LIMB POSITION X(11,3),LIMB POSITION Y(11,3),LIMB POSITION Z(11,3),X#,Y#,Z#)
`If intersection behind, move user forwards
MOVE OBJECT 11,1
ENDIF
`Right Intersection Check
IF INTERSECT OBJECT (5,LIMB POSITION X(11,4),LIMB POSITION Y(11,4),LIMB POSITION Z(11,4),X#,Y#,Z#)
`If intersection to the right, move the user left
MOVE OBJECT LEFT 11,1
ENDIF
`Left Intersection Check
IF INTERSECT OBJECT (5,LIMB POSITION X(11,5),LIMB POSITION Y(11,5),LIMB POSITION Z(11,5),X#,Y#,Z#)
`If intersection to the left, move user right
MOVE OBJECT RIGHT 11,1
endif
IF INTERSECT OBJECT (5,LIMB POSITION X(11,6),LIMB POSITION Y(11,6),LIMB POSITION Z(11,6),X#,Y#,Z#)
MOVE OBJECT up 11,3
endif
IF INTERSECT OBJECT (5,LIMB POSITION X(11,7),LIMB POSITION Y(11,7),LIMB POSITION Z(11,7),X#,Y#,Z#)
MOVE OBJECT down 11,3
endif
remend
`Refresh the screen(SYNC) and call the LOOP function
SYNC
LOOP
function movePlayer()
oldxx# = object position x(11)
oldyx# = object position y(11)
oldzx# = object position z(11)
rem apply gravity, and user changes to movement
angy# = object angle y(11)
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#
if keystate(32)=1 then vx# = vx# + cos(angy#) : vz# = vz# - sin(angy#)
if keystate(30)=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
xx# = oldxx#+vx#
yx# = oldyx#+vy#
zx# = oldzx#+vz#
collide = sc_SphereCastGroup(1,oldxx#,oldyx#,oldzx#,oldxx#,oldyx#+vy#,oldzx#,radius#,0)
if collide>0
rem how flat is this ground
ny# = sc_getCollisionNormalY()
if abs(ny#)>slope#
rem FLAT, stick
oldyx# = sc_getStaticCollisionY()
else
rem STEEP, slide
xx# = xx# - oldxx# : zx# = zx# - oldzx#
oldxx# = sc_getCollisionSlideX()
oldyx# = sc_getCollisionSlideY()
oldzx# = sc_getCollisionSlideZ()
xx# = xx# + oldxx# : zx# = zx# + oldzx#
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
oldyx# = oldyx# + vy#
ground = 0
endif
`if ground = 1 and jumptimer>0 then dec jumptimer
if collide>0
rem if hit, reposition player, halt movement vector
xx# = sc_getCollisionSlideX()
oldyx# = sc_getCollisionSlideY()
zx# = 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 11,xx#,oldyx#,zx#
sc_updateObject 11
text 80,0, str$(oldyx#)
text 200,0, str$(xx#)
text 400,0, str$(vy#)
endfunction
function makeExtraSpheres(num)
for i=20 to 20+num
make object sphere i,littleRadius#*2.0
position object i,rnd(50)-25,rnd(50)+100,rnd(50)-25
color object i,rgb(rnd(255),rnd(255),rnd(255))
sc_setupObject i,3,1
sc_allowobjectscaling i
next i
rem extra spheres are group 3
endfunction
function handleExtraSpheres(num)
for i=20 to 20+num
rem move all the little spheres towards the player and slide them
oldxx# = object position x(i)
oldyx# = object position y(i)
oldzx# = object position z(i)
point object i,object position x(11),object position y(11),object position z(11)
`move object i,0.5
xx# = object position x(i)
yx# = object position y(i)
zx# = object position z(i)
rem little spheres collide with all (0)
collide = sc_sphereSlide(0,oldx#,oldy#,oldz#,x#,y#,z#,littleRadius#,i)
if collide>0
position object i,sc_getCollisionSlideX(),sc_getCollisionSlideY(),sc_getCollisionSlideZ()
endif
sc_updateObject i
next i
if hide = 1
for i=20 to 20+num
hide object i
next i
hide =2
endif
endfunction