TGC Codebase Backup



3d Anaglyphic Bouncing Boxes (you need red/cyan glasses) by SwirlingBrain

30th Dec 2003 4:22
Summary

3d Bouncing Boxes. You need Red/Cyan anaglyph glasses to see the effect of the boxes coming at you.



Description

This application produces 3d anaglyph red/cyan objects that bounce around the screen. You must have red/cyan glasses to view the effect. (red left eye, cyan right eye). You can change some of the variables to view more boxes.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem * Title  : Active Application
Rem * Author : Jim Brown
Rem * Date   : December 2003
`+--------------------------------------------------+
`| Jim Brown jimbrown@swirlingbrain.com             |
`| 3d anaglyph bouncing boxes (darkbasic)           |
`|                                                  |
`| To view, you need anaglyph glasses with          |
`| red on left and cyan on right.                   |
`| (blue on right is ok too but you loose some of   |
`| the green colors)                                |
`|                                                  |
`| Each box is made up of two cubes ghosted on top  |
`| of each other:  A red and a blue to see for each |
`| eye.  Green is mixed in too to get more viewable |
`| colors.                                          |
`+--------------------------------------------------+

rem set sync rate
sync on : sync rate 20
hide mouse

rem number of cubes to see
numobjects=4

rem red/blue only flag
redblueonly=0

rem size of cubes
size=100/numobjects

rem red/blue separation factor
sep#=0.055

rem z offset of world
zoffset=200

rem x,y,z point of a cube
dim x(numobjects)
dim y(numobjects)
dim z(numobjects)

rem x,y,z direction forward or back of each axis
dim xdir(numobjects)
dim ydir(numobjects)
dim zdir(numobjects)

rem speed of each axis
dim xspd(numobjects)
dim yspd(numobjects)
dim zspd(numobjects)

rem spin
dim spinspd(numobjects)
dim spin(numobjects)

for i=0 to numobjects-1

  rem start each object in a random direction
  xdir(i)=rnd(1)
  ydir(i)=rnd(1)
  zdir(i)=rnd(1)

  rem start each object at a random position
  x(i)=rnd(300)-150
  y(i)=rnd(300)-150
  z(i)=rnd(300)-150

  rem start each object at a random speed
  xspd(i)=rnd(5)+2
  yspd(i)=rnd(5)+2
  zspd(i)=rnd(5)+2

  rem start spin
  spinspd(i)=rnd(5)
  spin(i)=rnd(360)

next i


rem create the objects
for i=0 to numobjects-1

  rem make objects
  make object cube i*2+1, size
  make object cube i*2+2, size

  rem color objects
  color object i*2+1, rgb(255, 0, 0)
  color object i*2+2, rgb(0, 0, 255)

  rem make object transparent
  ghost object on i*2+1
  ghost object on i*2+2

next i

rem set background to black
color backdrop rgb(0,0,0)
sync

do

if escapekey() then break

rem move each object a little bit
for i=0 to numobjects-1

  rem calculate a new position for object
  if xdir(i)=1 then x(i)=x(i)+xspd(i) else x(i)=x(i)-xspd(i)
  if ydir(i)=1 then y(i)=y(i)+yspd(i) else y(i)=y(i)-yspd(i)
  if zdir(i)=1 then z(i)=z(i)+zspd(i) else z(i)=z(i)-zspd(i)

  rem bounds check (bounce if out of bounds)
  bounce=0
  if x(i)>100 then x(i)=100 : xdir(i)=0 : xspd(i)=rnd(5)+2 : bounce=1
  if x(i)<-100 then x(i)=-100 : xdir(i)=1 : xspd(i)=rnd(5)+2 : bounce=1
  if y(i)>75 then y(i)=75 : ydir(i)=0 : yspd(i)=rnd(5)+2 : bounce=1
  if y(i)<-50 then y(i)=-50 : ydir(i)=1 : yspd(i)=rnd(5)+2 : bounce=1
  if z(i)>250 then z(i)=250 : zdir(i)=0 : zspd(i)=rnd(5)+2 : bounce=1
  if z(i)<-125 then z(i)=-125 : zdir(i)=1 : zspd(i)=rnd(5)+2 : bounce=1

  rem if bounce change color
  if bounce

    rem figure color levels for red and cyan lenses
    rem basically you need to have half or more red and
    rem half or more of either blue or green (mixed = cyan)
    rem blue seems to be more important than green
    rem if you have only red and blue glasses you should remove green
    r=210+rnd(255-210)
    g=rnd(255)
    b=rnd(255-128)+128
    if (b+g)<200 then b=200-g

    rem color objects
    if redblueonly
      color object i*2+1, rgb(r, 0, 0)
      color object i*2+2, rgb(0, 0, b)
    else
      color object i*2+1, rgb(r, 0, 0)
      color object i*2+2, rgb(0, g, b)
    endif

    rem if bounce change spin speed
    spinspd(i)=rnd(5)

  endif

  rem set object to new calculated position
  position object i*2+1, (0-z(i))*sep#+x(i),y(i),z(i)+zoffset
  position object i*2+2, x(i),y(i),z(i)+zoffset

  rem spin the object a little bit
  spin(i)=wrapvalue(spin(i)+spinspd(i))

  rem make object spin every direction
  rotate object i*2+1, spin(i), spin(i), spin(i)
  rotate object i*2+2, spin(i), spin(i), spin(i)

next i

rem update screen
sync

loop