I'm glad you liked the functions
. There are some bugs in them... Anyway, I will explain my functions to you.
****************************************************************
COLLSetupObject(MaxOBJ)
-Sets up the collision system. Call this at the top of your program.
MaxOBJ- The highest object number you will ever use for any collision-using objects in your project.
COLLUpdateOld(Obj,Gravity#)
-Updates your object's old position. Do this BEFORE you move your object.
Obj- The object number of the object you are updating.
Gravity#- How powerful the gravity is.
COLLUpdateNew(Obj)
-Updates your object's new position. Do this AFTER you move your object but BEFORE you call the COLLHandleCollision function.
Obj- The object number of the object you are updating.
COLLHandleCollision(Obj,Group,Radius,Offset,Terrain Collision?)
-Actually positions your object based on the old and new positions gathered by COLLUpdateOld and COLLUpdateNew.
Obj- The object you're handling collision for.
Group- The group number you want to check for collision against.
Radius- The radius of your object's collision sphere.
Offset- The Y offset of your object's collision sphere.
Terrain Collision?- Never got this working... just set it to 0.
COLLJump(Obj,Height,Key)
-Allows an object to jump a certain height only when a certain key is pressed and they are on the ground. Just call it each loop and you should be ok.
Obj- The object that will be jumping.
Height- The height of that object's jump.
Key- The scancode number of the key that will trigger jumping. Each key on your keyboard has its own number. The spacekey's, for example, is 57.
COLLCompJump(Obj,Height)
-Does the exact same thing as the COLLJump function but does not take a key input. When you call this, the object will jump if it can.
Obj- The object that will be jumping.
Height- The height of the object's jump.
****************************************************************
Here's how your code should look. (This won't run, it's just to show what should happen.)
Sync On
COLLSetup(Maximum Object) `10000 should do I guess...
Load Player Object
Setup your player object for collision using the sc_setupobject command.
Load World Object
Setup your world for collision with the sc_setupcomplexobject or sc_setupobject command (I recommend sc_SetupComplexOBject).
Do
COLLUpdateOld(Player Object Number,Gravity Amount [0.2 is good])
Move your player using move object or whatever
COLLJump(Player Object, Jump Height, Key) <--Only if the player jumps.
COLLUpdateNew(Player Object Number)
COLLHandleCollision(Player Object Number, Whatever group you put your world in, Player Radius, Player Offset (use 0 for now...),Terrain Collision (WRITE 0.))
Sync
Loop
And make sure to have this code at the end of your program:
END
rem ********************************************
rem
rem COLLISION FUNCTIONS
rem
rem ********************************************
function COLLSetup(MaxObj)
`MaxOBJ=The Highest Object Number that will need collision.
Dim COLL#(MaxOBJ,8)
exitfunction
delete memblock 1
endfunction
function COLLUpdateOld(OBJ,GravityAmount as float)
COLL#(OBJ,4) = object position x(OBJ)
COLL#(OBJ,5) = Object position y(OBJ)
COLL#(OBJ,6) = object position z(OBJ)
COLL#(OBJ,7)=COLL#(Obj,7)-GravityAmount
endfunction
function COLLJump(OBJ,HEIGHT as float,Key as integer)
if KEY>0
if Keystate(Key) and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
else
if controlkey() and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
ENDIF
endfunction
function COLLCompJump(OBJ,HEIGHT as float)
if COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
endfunction
Function COLLUpdateNew(OBJ)
sc_UpdateObject OBJ
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,2) = Object position y(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
sc_UpdateObject OBJ
endfunction
Function COLLHandleCollision(OBJ,GROUP,RADIUS as float,Offset#,TC as boolean)
COLL#(obj,8)=0
COLL#(obj,2)=COLL#(obj,2)+COLL#(obj,7)
oy#=COLL#(obj,2)
Coll=sc_sphereSlideGroup(Group,COLL#(obj,4),COLL#(obj,5)+Offset#,COLL#(obj,6),COLL#(obj,1),COLL#(obj,2)+Offset#,COLL#(obj,3),Radius,Obj)
if Coll>0
COLL#(obj,1)=sc_getcollisionslidex()
if sc_getcollisionnormalx()=0 and sc_getcollisionnormalz()=0`COLL#(obj,2)+Offset#<>sc_getcollisionslidey() AND sc_getcollisionslidey()>sc_getstaticcollisiony()
COLL#(obj,7)=0
COLL#(obj,8)=1
endif
COLL#(obj,2)=sc_getcollisionslidey()-Offset#
COLL#(obj,3)=sc_getcollisionslidez()
Endif
Coll2=sc_SpherecastGroup(Group,COLL#(obj,4),COLL#(obj,5)+Offset#,COLL#(obj,6),COLL#(obj,4),oy#+Offset#,COLL#(obj,6),Radius,Obj)
if Coll2>0
COLL#(obj,7)=0
COLL#(obj,8)=1
endif
Position Object Obj,COLL#(obj,1),COLL#(obj,2),COLL#(obj,3)
endfunction
I hope this helps! If you'd like an example with a .x model just ask. Good luck
If you don't know how to call a function, you can read this tutorial I wrote
:
http://forum.thegamecreators.com/?m=forum_view&t=87472&b=7
EDIT: I changed your old code to use the functions, although they aren't too great with terrain
:
Rem Project: collision
Rem Created: 07/07/2008 08:35:45
Rem ***** Main Source File *****
SYNC ON
SYNC RATE 60
autocam off
set ambient light 70
COLLSetup(100)
`Make a simple matrix, purely visual, not needed.
MAKE MATRIX 1,1000,1000,10,10
POSITION MATRIX 1,-500,0,-500
`Make the user's object.
MAKE OBJECT CUBE 1,20
POSITION OBJECT 1,0,5,0
sc_setupobject 1,2,1
`Make a box object, replace this with your map.
load image "test.jpg",10
load object "test.x",2
texture object 2, 10
POSITION OBJECT 2,0,0,0
scale object 2,1000,1000,1000
`Setup object 2, the map, to be in group 1 and define it as a complex object.
SC_SetupComplexObject 2,1,2
`Start the main program loop.
DO
COLLUpdateOld(1,0.2)
`Handle the player movement/orientation
MOVE OBJECT 1,( UPKEY()-DOWNKEY() ) * 5
yrotate object 1,Object Angle Y(1)-( LEFTKEY()-RIGHTKEY() ) * 5
COLLJump(1,4,57)
COLLUpdateNew(1)
COLLHandleCollision(1,1,Object Size Y(1,1)/2.0,0,0)
Set Camera To Follow Object Position X(1),Object Position Y(1),Object Position z(1),Object Angle Y(1),75,50,3.5,0
Point Camera Object Position X(1),Object Position Y(1),Object Position z(1)
rem Player dies
if Object Position Y(1)<-500
Position Object 1,0,5,0
Endif
SYNC
LOOP
`This last bit is a requirement of Sparky's dll. Basically, DBP's commands are grouped into plugins themselves.
`Theres a plugin for Camera commands, a plugin for Object commands, a plugin for Network commands, etc.
`However DBP, at the time of writing, includes only the necassary plugins for the program to work. So if you dont use
`and 3D commands, it wont include any 3D plugins. Sparky's dll requires the memblock commands, we haven't used any
`memblock commands yet so DBP wont include the Memblock plugin, which means Sparky's dll will crash. To fix this, we
`just add a DELETE MEMBLOCK 1 command after the function. DBP will never read this code, and so it will never get
`executed, but it will make DBP include the Memblock plugin.
`If you've used a memblock command elsewhere in your project, then you wont need this.
DELETE MEMBLOCK 1
END
rem ********************************************
rem
rem COLLISION FUNCTIONS
rem
rem ********************************************
function COLLSetup(MaxObj)
`MaxOBJ=The Highest Object Number that will need collision.
Dim COLL#(MaxOBJ,8)
exitfunction
delete memblock 1
endfunction
function COLLUpdateOld(OBJ,GravityAmount as float)
COLL#(OBJ,4) = object position x(OBJ)
COLL#(OBJ,5) = Object position y(OBJ)
COLL#(OBJ,6) = object position z(OBJ)
COLL#(OBJ,7)=COLL#(Obj,7)-GravityAmount
endfunction
function COLLJump(OBJ,HEIGHT as float,Key as integer)
if KEY>0
if Keystate(Key) and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
else
if controlkey() and COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
ENDIF
endfunction
function COLLCompJump(OBJ,HEIGHT as float)
if COLL#(OBJ,8)=1 then COLL#(OBJ,7)=HEIGHT
endfunction
Function COLLUpdateNew(OBJ)
sc_UpdateObject OBJ
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,2) = Object position y(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
COLL#(OBJ,1) = object position x(OBJ)
COLL#(OBJ,3) = object position z(OBJ)
sc_UpdateObject OBJ
endfunction
Function COLLHandleCollision(OBJ,GROUP,RADIUS as float,Offset#,TC as boolean)
COLL#(obj,8)=0
COLL#(obj,2)=COLL#(obj,2)+COLL#(obj,7)
oy#=COLL#(obj,2)
Coll=sc_sphereSlideGroup(Group,COLL#(obj,4),COLL#(obj,5)+Offset#,COLL#(obj,6),COLL#(obj,1),COLL#(obj,2)+Offset#,COLL#(obj,3),Radius,Obj)
if Coll>0
COLL#(obj,1)=sc_getcollisionslidex()
if sc_getcollisionnormalx()=0 and sc_getcollisionnormalz()=0`COLL#(obj,2)+Offset#<>sc_getcollisionslidey() AND sc_getcollisionslidey()>sc_getstaticcollisiony()
COLL#(obj,7)=0
COLL#(obj,8)=1
endif
COLL#(obj,2)=sc_getcollisionslidey()-Offset#
COLL#(obj,3)=sc_getcollisionslidez()
Endif
Coll2=sc_SpherecastGroup(Group,COLL#(obj,4),COLL#(obj,5)+Offset#,COLL#(obj,6),COLL#(obj,4),oy#+Offset#,COLL#(obj,6),Radius,Obj)
if Coll2>0
COLL#(obj,7)=0
COLL#(obj,8)=1
endif
Position Object Obj,COLL#(obj,1),COLL#(obj,2),COLL#(obj,3)
endfunction