TGC Codebase Backup



Boxes1 by JerBil

10th Jun 2004 19:16
Summary

Object movement and collision. No media required.



Description

This example shows one way to move objects in response to collisions. Try different values for the speed and direction variables for fun.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: Boxes1
Rem Created: 6/9/2004 8:24:13 PM

Rem ***** Main Source File *****

`Set Display Mode (Screen Size & Depth)
set display mode 800,600,32

`Turn On 3D Backdrop
backdrop on

`Color 3D Backdrop Black
color backdrop 0,RGB(0,0,0)

`Set available (ambient) light to low value for good contrast
set ambient light 3

`make three cubes, one unit in size each
make object cube 1,1
make object cube 2,1
make object cube 3,1

`make one flat box, 10 units wide, 2/10 unit high, and 10 units deep
make object box 4,10,0.2,10

`color first cube red & third cube green
color object 1,RGB(255,0,0)
color object 3,RGB(0,255,0)

`color flat box green/blue-ish
color object 4,RGB(10,100,100)

`declare variables for the X position of each cube
cube1x#=-6 `goes left of center
cube2x#=0  `goes in center
cube3x#=6  `goes right of center


`declare variable dr# for value to subtract from cube 1 position
`setting it negative - will move cube left
dr#=-0.01

`declare variable sp# for speed to turn (spin) cubes
sp#=0.2

`turn on sync -once on, we must sync (refresh the screen) ourselves-
sync on

`place all three cubes in 3D space
position object 1,cube1x#,0,0
position object 2,cube2x#,0,0
position object 3,cube3x#,0,0

`place flat box in 3D space
position object 4,0,-0.8,0

`place camera zero (default camera) in 3D space (8 is above scene, -15 is before scene)
position camera 0,0,8,-15

`point camera zero at 0,0,0 in 3D space
point camera 0,0,0,0

`turn on global collision to detect any 3D objects colliding with each other
set global collision on

`start a DO LOOP -our working code goes here-
do

`if there is a collision with cube one, change dr# & sp# (sp# is used to spin cubes)
if object collision(1,0)   `the zero in (1,0) will detect ANY object colliding with cube one
  dr#=0.01 `dr# now positive
  sp#=1.5  `sp# faster
endif

`if there is a collision with cube three, change dr# & sp#
if object collision (3,0)  `the zero in (3,0) will detect ANY object colliding with cube three
 dr#=-0.01 ` dr# now negative
 sp#=1.5   ` sp# faster
endif

`add dr# to cube2x# (direction to cube 2's x position)
 cube2x#=cube2x#+dr#
`now position cube 2 with new cube2x# value
 position object 2,cube2x#,0,0

`while we're at it, turn (spin) all the cubes using sp# (speed)
 turn object left 2,sp#  `spin cube 2 left
 turn object right 1,sp# `spin cube 1 right
 turn object left 3,sp#  `spin cube 3 left

`if we have set sp# to 1.5, DECrement it a little each loop
`this will have the effect of gradually slowing spinning cubes
 if sp#>0.2
  dec sp#,0.002
 endif

`update the screen (sync)
sync

`keep LOOPing
loop