Mini-Lesson Time
Ok, lets break this up into parts. We basically want to create a game whereby you drive a car from 1 waypoint to the next, and when you reach each waypoint, a new one is given to you.
A good starting point for making any program is to cut it into sections:
1. Create a box to represent a car model, give us the ability to move the box around in a 3D world, program our camera to stay behind the box, and create a matrix so we have some sort of an environment to move around in.
2. Program a routine to place boxes randomly around in our world, to represent buildings.
3. Detect collision with buildings and handle appropriately.
4. Program a waypoint system (we'll break this part up more when we get to it)
Keep in mind this isnt a tutorial, I wont be going in-depth into anything, just providing basic explanations. Ill stick to my usual style and give code first, explanation after.
Vehicle Handling Code:
`App Settings
SYNC ON:SYNC RATE 0:AUTOCAM OFF:HIDE MOUSE
`Make vehicle
MAKE OBJECT BOX 1,20,10,40
POSITION OBJECT 1,0,5,0
`Make matrix
MAKE MATRIX 1,1000,1000,10,10
POSITION MATRIX 1,-500,0,-500
`Store variable data
camHeight# = 40
camDist# = 100
`Main loop
DISABLE ESCAPEKEY
WHILE ESCAPEKEY() = 0
`Handle movement
moveSpeed# = (UPKEY()-DOWNKEY())*0.5
turnSpeed# = (RIGHTKEY()-LEFTKEY())*0.5
MOVE OBJECT 1,moveSpeed#
YROTATE OBJECT 1,WRAPVALUE(OBJECT ANGLE Y(1)+turnSpeed#)
`Store out player's position
x# = OBJECT POSITION X(1)
z# = OBJECT POSITION Z(1)
`Orient camera
POSITION CAMERA x#,camHeight#,z#
ROTATE CAMERA CAMERA ANGLE X(),OBJECT ANGLE Y(1),CAMERA ANGLE Z()
MOVE CAMERA -camDist#
`Refresh screen
SYNC
`Repeat
ENDWHILE
`End program
END
The first line sets up our application. Then we make our vehicle object and a matrix. Pretty simple so far.
camHeight# is the height of the camera, and camDist# is the distance behind the vehicle of the camera.
Kind of in a rush now so I wont be explaining much at all now...
Ok add random buildings (add this above the variable declaration for the camera)...
FOR x = 1 TO 50
MAKE OBJECT BOX x,40+RND(30),40+RND(30),40+RND(30)
POSITION OBJECT x,-500+RND(1000),OBJECT SIZE Y(x)/2,-500+RND(1000)
NEXT x
Which basically makes 49 buildings of random size and location.
Now we'll program the collision. Again this is basic hit-stop collision, for better collision detection methods and handling do some forum searches. Basically, we use the AUTOMATIC OBJECT COLLISION command. Very primitive compared to other collision routines, and I highly recommend looking up collision tutorials on these forums.
Add this code after the vehicle positioning line;
AUTOMATIC OBJECT COLLISION ON
And thats that, collision is now handled. Nifteh.
Finally, the waypoint system. Here's the entire game's code, waypoints and all;
`App Settings
SYNC ON:SYNC RATE 0:AUTOCAM OFF:HIDE MOUSE
`Make vehicle
MAKE OBJECT BOX 1,20,10,40
POSITION OBJECT 1,0,5,0
AUTOMATIC OBJECT COLLISION 1,20,1
`Make matrix
MAKE MATRIX 1,1000,1000,10,10
POSITION MATRIX 1,-500,0,-500
`Make buildings
FOR x = 2 TO 50
MAKE OBJECT BOX x,40+RND(30),40+RND(30),40+RND(30)
POSITION OBJECT x,-1000+RND(2000),OBJECT SIZE Y(x)/2,-1000+RND(2000)
NEXT x
`Store variable data
camHeight# = 40
camDist# = 100
wx# = -1000+RND(1000)
wz# = -1000+RND(1000)
`Make waypoint cylinder for visual and collision routines
MAKE OBJECT CYLINDER 51,600
SCALE OBJECT 51,20,100,20
COLOR OBJECT 51,RGB(000,255,000)
SET OBJECT TRANSPARENCY 51,1
GHOST OBJECT ON 51
FADE OBJECT 51,90
SET OBJECT FOG 51,0
`Make waypoint light for effects
MAKE LIGHT 1
COLOR LIGHT 1,RGB(000,255,000)
`Set ambient light higher...just cuz, it looks kewl... i guess.
SET AMBIENT LIGHT 50
FOG ON
`Main loop
DISABLE ESCAPEKEY
WHILE ESCAPEKEY() = 0
`Handle movement
moveSpeed# = (UPKEY()-DOWNKEY())*0.5
turnSpeed# = (RIGHTKEY()-LEFTKEY())*0.5
MOVE OBJECT 1,moveSpeed#
YROTATE OBJECT 1,WRAPVALUE(OBJECT ANGLE Y(1)+turnSpeed#)
`Store out player's position
x# = OBJECT POSITION X(1)
z# = OBJECT POSITION Z(1)
`Orient camera
POSITION CAMERA x#,camHeight#,z#
ROTATE CAMERA CAMERA ANGLE X(),OBJECT ANGLE Y(1),CAMERA ANGLE Z()
MOVE CAMERA -camDist#
`Waypoint Collision
IF OBJECT COLLISION(1,51) THEN wx# = -1000+RND(2000):wz# = -1000+RND(2000)
`Position waypoint
POSITION OBJECT 51,wx#,300,wz#
POSITION LIGHT 1,wx#,0,wz#
`Refresh screen
SYNC
`Repeat
ENDWHILE
`End program
END
The waypoint object is object 51. After setting some basic effects like fading it a bit, colouring it green, and constantly positioning a green light at it's position, we generate a random position for it. wx# is it's x position, and wz# is it's z position. The object collision command is used to detect when our player object 1 collides with the waypoint object 51, when that occurs, we generate a new x and z coordinate for the waypoint.
Tips:
- Replace the building creation code with a custome-designed map of a city, so you can ensure buildings wont overlap and can avoid programming a smarter map generator to do so for you.
- Change the collision routine to sliding collision
- Allow the car to speed up/slow down, and turn sharper/less sharp depending on it's speed.
- Store everything in variables for better reference, such as waypoint = 51, city = 2, vehicle = 1.
- Add some cool effects like clouds, a skysphere, better fog, some shaders, a pulsing/glowing waypoint, and sound effects.
- Finally, add a game over screen and a goal to the game. Whereby the user must get as many waypoints as they can before time finishes.
- Also, you'll want to do the waypoint positioning system differently. Store all possible positions for the waypoint in an array before the game runs, this way you can chose where you want the waypoint to go, then pick randomly from this array a position for the waypoint. When a position is used, use a type structure to let your program know it's been used, so you can later make your program use unused waypoints everytime.
- Add a start screen, a pause menu, and you're done. You've got yourself a game.
Keep in mind all of the tips I've mentioned have been covered numerous times on the forums, so do some searches before posting asking for help on them.
Goodluck, sorry I couldnt be more descriptive, I havent tested the code but it should all work unless I made a typo, double checked it and it seems in order,
- RUC'