TGC Codebase Backup



Simple Box Movement: V2 by RUCCUS

27th Feb 2005 20:20
Summary

The second installment of my Simple Box Movement. This one cover's the usage of angles to turn the box in the direction you wish to aim at.



Description

Turn the box with the left and right arrow keys.
Move forward and backward with the up and down arrow keys.

Goodluck!



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Basic Movement Controls Tutorial
REM By RUCCUS of The Game Creators

REM No credit needed, use freely and change at own risk :p

REM Standard Setup
SYNC ON    : SYNC RATE 300
HIDE MOUSE
REM Time to create a simple box, this will be our character
REM We use the make object box command to make a box in db
REMSTART The first number tells us the number of the box
for later reference. The second defines the width, the third
defines the height, and the fourth defines the length.REMEND
MAKE OBJECT BOX 1,10,10,10
REMSTART Now we position the object, first we tell it what object
to position, we want to position object 1 so we use a 1. Then
we show it where to position the object, using the x, y, and z
coordinates.REMEND
POSITION OBJECT 1,0,0,-10
REMSTART Finally, let's colour the object, just to spiff it up
a bit. Once again tell the compiler which object to colour,
in our case, object 1. Then tell it what amount of colours we
will use of each primary colour, red, blue and green. Since we
are making the box all green, we wont have any red or blue, and
full green (255).REMEND
COLOR OBJECT 1, RGB(000,255,000)
REMSTART Lets create one other box just so you can see you're
moving around. It will be object 2, and will be red.REMEND
MAKE OBJECT BOX 2,30,20,30
POSITION OBJECT 2,0,0,15
COLOR OBJECT 2, RGB(255,000,000)
REMSTART As one last touch we should position the camera at a
higher angle so we can see the object a lot better.
We do this by using the position camera command.REMEND
POSITION CAMERA 0,50,-100
REMSTART Now we start a loop using the DO command. This loop will
constantly be occuring to check for anything new happening in
the game. REMEND
DO
REM COLLSION!!!
REMSTART The following code will give the game a little bit of a
more realistic feel. It'll stop the box from running through the
other box.
First, we need to store the object 1's coordinates for later use. REMEND
OLDX# = OBJECT POSITION X(1)
OLDY# = OBJECT POSITION Y(1)
OLDZ# = OBJECT POSITION Z(1)
REM CONTROLS
REMSTART Now lets define the controls to move our friendly box
around. We'll start with moving forward and backward.REMEND

REM Moving Forwards.
REMSTART IF the UPKEY is pressed (equals 1) the move object 1
foewards by 1 unit. The object will always move forwards as long
as the upkey equals 1, or is being pressed in other words.REMEND
IF UPKEY() = 1 THEN MOVE OBJECT 1,1
REM Moving Backwards
REMSTART The same goes for moving backwards, except we'll
use a negative so the box moves backwards.REMEND
IF DOWNKEY() = 1 THEN MOVE OBJECT 1,-1
REM Now for turning left and right
REMSTART Basically, if the leftkey is pressed, rotate the
object 1 on it's y axis, at an angle of -.5 degrees per second
that the leftkey is held. The same goes for the rightkey,
except rotating at a positive amount. Try changing the
YROTATE to XROTATE or ZROTATE, but be sure to change the ANGLE Y
to the corresponding axis aswell!REMEND
IF LEFTKEY()=1 THEN YROTATE OBJECT 1,OBJECT ANGLE Y(1)-1
IF RIGHTKEY()=1 THEN YROTATE OBJECT 1,OBJECT ANGLE Y(1)+1
REM The last part of the collision
REMSTART This code will check for when object 1 hits any other
object. If it does it will position the object 1 at it's last
coordinates that were'nt touching a different object.REMEND
IF OBJECT COLLISION (1,0)
POSITION OBJECT 1,OLDX#,OLDY#,OLDZ#
ENDIF
REM Finishing up
REMSTART The SYNC command will tell the program to refresh the
screen, so we can see the object move. Then the loop command
ends the loop.REMEND
SYNC
LOOP
REM Challenges
REMSTART
-Try experimenting with different colours
-Try changing the angles of rotation, you could get you're
box to start flying....:)
-Try changing the amount the object turns and moves to
increase/decrease the speed and turning capability
GOODLUCK!

                                                      - RUCCUS