Ok, I know this has be discussed to death time and again,
And I really have looked on the forums searching for a
solution, but to no avail.
I'm using sparkys collision Dll with Darkbasic Pro,
And I'm using sample code that shows how to impliment
collision between two objects(Kudos to member RUCCUS for
this code).
My problem is, When I move my sphere object around the level
with the arrow keys, Collision works between the level and
the sphere object.
Now, if I say, attach the camera to the sphere object, or
use "Lock object" and try to collide with the level, it
does not work(It goes right Through the level).
I want the camera to collide with the world object, any ideas what I'm doing wrong here?
I've attached my project with all files included
And here's my code.
You can move the camera with the AWSD keys and the sphere
with the arrow keys.
Rem Project: Collision2
Rem Created: 6/5/2009 3:02:07 PM
Rem ***** Main Source File *****
`Sliding Collision with Sparky's Collision dll
`By RUCCUS
`----------
`Email/MSN: Ask-RUCCUS@hotmail.com
`Main Program Settings
SYNC ON
SYNC RATE 0
AUTOCAM OFF
set camera range 1,100000
`Make the user's object.
make object sphere 1,200
`Make a box object, replace this with your map.
LOAD OBJECT "WORLD.DBO" ,2
POSITION OBJECT 2,3000,-2500,0
`Setup object 2, the map, to be in group 0, and to be set to collision
`type 0 - "Polygon".
SC_SetupObject 2,0,0
`Position/orient the camera.
POSITION CAMERA 0,-500,0
POINT CAMERA 0,0,0
`Start the main program loop.
DO
Rem Camera Movement Controls- AWSD FPS style.
x# = camera position x()
y# = camera position y()
z# = camera position z()
`forwards (w)
if keystate(17)=1
x# = newxvalue(x#,wrapvalue(camera angle y()),1)
z# = newzvalue(z#,wrapvalue(camera angle y()),1)
endif
`backwards (s)
if keystate(31)=1
x# = newxvalue(x#,wrapvalue(camera angle y()-180),1)
z# = newzvalue(z#,wrapvalue(camera angle y()-180),1)
endif
`left (a)
if keystate(30)=1
x# = newxvalue(x#,wrapvalue(camera angle y()-90),1)
z# = newzvalue(z#,wrapvalue(camera angle y()-90),1)
endif
`right (d)
if keystate(32)=1
x# = newxvalue(x#,wrapvalue(camera angle y()+90),1)
z# = newzvalue(z#,wrapvalue(camera angle y()+90),1)
endif
rem camera rotation
yrotate camera camera angle y() + mousemovex()*0.3
xrotate camera camera angle x() + mousemovey()*0.3
rem stops mouse from going upside down
if wrapvalue(camera angle x(0))>40 and wrapvalue(camera angle x(0))<180 then xrotate camera 0,40
if wrapvalue(camera angle x(0))>180 and wrapvalue(camera angle x(0))<280 then xrotate camera 0,280
rem camera position
position camera x#,y#,z#
`Store the user's old position on the xyz axis
OX# = OBJECT POSITION X(1)
OY# = OBJECT POSITION Y(1)
OZ# = OBJECT POSITION Z(1)
`Handle the player object movement/orientation
MOVE OBJECT 1,( UPKEY()-DOWNKEY() ) * .5
TURN OBJECT LEFT 1,( LEFTKEY()-RIGHTKEY() ) * .5
`Store the player object's new position on the xyz axis, after they've moved.
X# = OBJECT POSITION X(1)
Y# = OBJECT POSITION Y(1)
Z# = OBJECT POSITION Z(1)
`Call the sliding collision function, filling in the necassary variables
SlidingCollision(OX#,OY#,OZ#,X#,Y#,Z#,200,1,2)
SYNC
LOOP
`Sliding Collision Function
FUNCTION SlidingCollision(X1#,Y1#,Z1#,X2#,Y2#,Z2#,Radius#,Dyn,Obj)
`X1#-Z1# : The starting XYZ position of the intersection ray
`X2#-Z2# : The ending XYZ position of the intersection ray
`Radius# : The radius of the sliding collision sphere, usually you'd set this
` to the player's object's z sixe divided by 2. Our cube is 20 units thick,
` so we use 10 in the example.
`Dyn : The player object
`Obj : The map object that we want the player object to slide on
`Call the sc_SphereSlide command, using our variables.
`This command will create a mathematical intersection "Ray",
`this basically means that it'll make a 3D line starting from X1#,Y1#,Z1#,
`and ending at X2#,Y2#,Z2#. It'll then check if any of the polygons on the
`map object are inbetween these 2 points, if there is then it'll return a 1,
`if not, it'll return 0.
`What Sparky has done for us however is added in a few handy functions
`that are carried out inside of the SphereSlide command. Basically,
`once it detects an intersection, it'll get the angle the intersection
`occured at and using trigonometry (sin, cos and tan), it'll determine
`the logical position for an object to be if it were to "slide" along
`the polygon it hit. To make the system even more accurate however,
`Sparky's dll goes one step further by doing another intersection check on the
`new mathematical coordinates it just obtained, and checks that there wont be
`a collision there either. `It'll keep doing this until it reaches it's max
`iteration or until it finds a free, empty 3d location where the player's object
`can go. All this for free, sheesh!
C = sc_SphereSlide(Obj,X1#,Y1#,Z1#,X2#,Y2#,Z2#,Radius#,0)
`Now, sparky's dll will return the 3d position it thinks you should position your
`player object at AS WELL. These positions are returned using the sc_getCollisionSlideXYZ()
`commands. Basicaly the next code checks if an intersection occured in the first place
` (if C > 0), and if one did, then it retrieves the new XYZ location that the player should be
`placed at, and then positions the player object there accordingly.
IF C > 0
cx# = sc_getCollisionSlideX()
cy# = sc_getCollisionSlideY()
cz# = sc_getCollisionSlideZ()
POSITION OBJECT Dyn, cx#, cy#, cz#
ENDIF
ENDFUNCTION
`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