SECTION 4: ~~~~~~~~IMPLEMENTING THE COLLISION~~~~~~~~
Again, there are many ways to do this but we will use the INTERSECT OBJECT command. So let's break that one down...
The INTERSECT OBJECT command is what creates our ray for the collision. It will create an invisible line between the two positions we specify. In this case, we will be using our first limb(the wall collision sphere) and of course the player box. Since these values are constantly changing, we can use DBPro commands to locate those positions for us. Those commands would be the LIMB POSITION X(), LIMB POSITION Y(), and LIMB POSITION Z(). This will give us the values of each coordinate of our limb. And we use the OBJECT POSITION X(), OBJECT POSITION Y(), and OBJECT POSITION Z() to get the current position of our player object. Now, no matter where we are or our limb is, we will always have the current positions.
The INTERSECT OBJECT command itself has 7 values to add to it. The first value is the object we want to collide with. In this case it is object 2 (our temple). The next values are broken down to XYZ coordinates. The first set is going to be the LIMB POSITION.
(LIMB POSITION has 2 values. the object number and the limb number. For now we will focus on limb 1- the wall collision sphere)
The second set of XYZ values will be the player box. So let's take a look at it in action...
IF INTERSECT OBJECT (2, LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
MOVE OBJECT 1,-10
ENDIF
We can use the intersect object to check for collision with anything if we change that first value to 0. But for now we will keep it at 2. Then we want the limb's XYZ positions. Since this is not a constant number we just use the LIMB POSITION X(Object #, Limb #) and then the same for the Y and Z coordinates.
Now we will place in the second set of coordinates for our RAY. This time we use the Object Position X(object #) and the same for Y and Z.
To check if there is a collision we use the " > 0 " operation. This just says that if it is greater than 0 then there is a collision. If it is less then or equal to 0 then there is no collision.
Next we add what we want to do. In this case, if there is a collision we want to back the player up to a point where there was no collision. So all we do is subtract from the players position twice the amount we moved forward. This will place us before the collision occured and let the whole process start over. So now we will put this all together into our program.
This will go inside the first KEYSTATE IF block.
IF KEYSTATE(17)=1
Move OBJECT 1,5
IF INTERSECT OBJECT (2, LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
MOVE OBJECT 1,-10
ENDIF
ENDIF
`The italicized part of the code you should already have in your project so all you need to add is the unitalicized text.
`Now that we have our wall collision checks in for when we are walking around, let's take a look at how to check for the floor.
`We will use the same concept as before.
`The only difference is that we are now using our second limb (the ground collision sphere: limb # 2)
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
`Now, if there was a collision, we need to adjust the Y axis.
`So all we do is use the POSITION OBJECT command and add five to the Y position. This will move us up by 5 on the Y axis.
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) + 5, OBJECT POSITION Z(1)
ENDIF
`This next part of the collision check will be used if there is no ground collision.
`What this does is make sure you are on the ground. Otherwise, we would be "flying" hehe
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))<=0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) - 5, OBJECT POSITION Z(1)
ENDIF
`Now we will do the backwards collision for if we are walking backwards.
`This is almost exactly the same but we use a positive number for this rather than a negative.
`This will move us forward if there is a collision.
IF KEYSTATE(31)=1
MOVE OBJECT 1,-5
IF INTERSECT OBJECT (2, LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
MOVE OBJECT 1,10
ENDIF
`This part of the code also checks for the ground collision but for moving backwards.
`This code is exactly the same as the previous one with the execption that we changed the values for the Y axis to account for moving backwards
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) + 5, OBJECT POSITION Z(1)
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))<=0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) - 5, OBJECT POSITION Z(1)
ENDIF
ENDIF
`The only problem is that now it only checks for collision if we are actually moving.
`If we were to walk off the edge of a cliff we wouldn't fall if we weren't moving either forward or backward.
`So to fix that we add one more collision command just after we position the camera.
`This will check if there is a collision or not using the same INTERSECT OBJECT command that we used earlier.
`this basically doublechecks your collision so that when you walk off a cliff you are going to fall even when you are not walking forward or backwards.
`double-checks ground collision. This part of the code will go down below the position camera command.
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) + 5, OBJECT POSITION Z(1)
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))<=0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) - 5, OBJECT POSITION Z(1)
ENDIF
That is all there is to the collision. Looks like a lot but it's not too bad once you know what it does. If you need a further explanation feel free to email me and I'll do what I can So now your code will look like this.
REM initial program settings
SYNC ON
SYNC RATE 0
HIDE MOUSE
BACKDROP OFF
MAKE CAMERA 1
SET CAMERA RANGE 1,1,10000
SET AMBIENT LIGHT 50
REM Make The Player Box
MAKE OBJECT BOX 1,10,10,10
REM Initial wall collision sphere
MAKE OBJECT SPHERE 100,10
MAKE MESH FROM OBJECT 1,100
DELETE OBJECT 100
ADD LIMB 1,1,1
OFFSET LIMB 1,1,0,0,10
HIDE LIMB 1,1
REM Initial Ground collision sphere
ADD LIMB 1,2,1
OFFSET LIMB 1,2,0,-200,0
DELETE MESH 1
HIDE LIMB 1,2
REM Temple
LOAD OBJECT "temple1.x",2
POSITION OBJECT 2,0,-200,200
YROTATE OBJECT 2, 180
DO
REM Keystates. W,S,A,D for controls
IF KEYSTATE(17)=1
MOVE OBJECT 1,5
IF INTERSECT OBJECT (2, LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
MOVE OBJECT 1,-10
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) + 5, OBJECT POSITION Z(1)
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))<=0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) -5, OBJECT POSITION Z(1)
ENDIF
ENDIF
IF KEYSTATE(31)=1
MOVE OBJECT 1,-5
IF INTERSECT OBJECT (2, LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
MOVE OBJECT 1,5
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) + 5
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))<=0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) - 5, OBJECT POSITION Z(1)
ENDIF
ENDIF
IF KEYSTATE(30)=1
TURN OBJECT LEFT 1,5
TURN CAMERA LEFT 1,5
ENDIF
IF KEYSTATE(32)=1
TURN OBJECT RIGHT 1,5
TURN CAMERA RIGHT 1,5
ENDIF
REM reposition camera to line up with player object
POSITION CAMERA 1, OBJECT POSITION X(1), OBJECT POSITION Y(1), OBJECT POSITION Z(1)
`double-checks ground collision (when using a lower number for the walking speed
`you can simulate the actual steps look
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))>0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) + 5, OBJECT POSITION Z(1)
ENDIF
IF INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), OBJECT POSITION X(1),OBJECT POSITION Y(1), OBJECT POSITION Z(1))<=0
POSITION OBJECT 1, OBJECT POSITION X(1), OBJECT POSITION Y(1) - 5, OBJECT POSITION Z(1)
ENDIF
SYNC
LOOP
LIFE: "That thing that happens to us when we are too young to die"
~V.J.C. 2003