3D bouncing cube with retro background

Post Reply
DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

3D bouncing cube with retro background

Post by DrChip »

Just some fun as I waiting for an appointment. :)

Code: Select all

REM 3d cube with retro background
REM iPhone 6+ / 10.3 b4 
REM I havent added texture to the cube
REM yet. I got lazy... 
REM you can change the object to the amiga ball. :)
REM enjoy...

GRAPHICS
GOSUB setup

LOOP:
REFRESH OFF
GRAPHICS CLEAR 0,0,0
GOSUB background
sizez=15*SIN(mm*10)
GOSUB rotate
GOSUB construct
REFRESH 
GOTO LOOP


'Draw The Object
construct:
FOR a=1 TO faces
    GOSUB drawface
NEXT a
RETURN

'Draw A Face Of The Object
drawface:
vx1= tx(f1(a))-tx(f2(a))
vy1= ty(f1(a))-ty(f2(a))
vx2= tx(f3(a))-tx(f2(a))
vy2= ty(f3(a))-ty(f2(a))
n=  vx1*vy2-vx2*vy1
IF n<0 THEN
    n=-(n/3000)
    FILL COLOR (r(a)+n)/255,(g(a)+n)/255,(b(a)+n)/255
    FILL TRI tx(f1(a)),ty(f1(a)) , tx(f2(a)),ty(f2(a)) , tx(f3(a)),ty(f3(a))
    FILL TRI tx(f1(a)),ty(f1(a)) , tx(f4(a)),ty(f4(a)) , tx(f3(a)),ty(f3(a))
    
    
    IF clss(a)=1 THEN
        DRAW COLOR 25/255,25/255,25/255
        DRAW LINE tx(f1(a)),ty(f1(a)) TO tx(f2(a)),ty(f2(a))
        DRAW LINE tx(f2(a)),ty(f2(a)) TO tx(f3(a)),ty(f3(a))
        DRAW LINE tx(f3(a)),ty(f3(a)) TO tx(f4(a)),ty(f4(a))
        DRAW LINE tx(f4(a)),ty(f4(a)) TO tx(f1(a)),ty(f1(a))
    END IF
END IF
RETURN


rotate:

'Rotate And Scale Each Point. Store Result 
FOR a=1 TO points
    x1=x(a)
    y1=y(a)
    z1=z(a)
    
    ' X,Y,Z rotations! 
    xx=x1
    yy=y1*cs(xr)+z1*sn(xr)
    zz=z1*cs(xr)-y1*sn(xr)
    y1=yy
    x1=xx*cs(yr)-zz*sn(yr)
    z1=xx*sn(yr)+zz*cs(yr)
    zz=z1
    xx=x1*cs(zr)-y1*sn(zr)
    yy=x1*sn(zr)+y1*cs(zr)
    
    'Apply Perspective! 
    
    dv=(zz/40)+1
    xx=sizez*(xx/dv)+sw/2
    yy=sizez*(yy/dv)+sh/2
    tx(a)=xx
    ty(a)=yy
    tz(a)=zz
NEXT a
xr=xr+1
yr=yr+2
zr=zr+3
IF xr>720 THEN
    xr=xr-720
END IF
IF yr>720 THEN
    yr=yr-720
END IF
IF zr>720 THEN
    zr=zr-720
END IF
RETURN


background:
mm=mm+.005
b1=1000*SIN(mm)
g1=1000*SIN(mm/2)
r1=1000*SIN(mm/3)

FOR a=1 TO SCREEN_HEIGHT() STEP res
    FILL COLOR (r1+a)/1000,(g1+a)/1000,(b1+a)/1000
    FILL RECT 0,a TO SCREEN_WIDTH(),a+res
NEXT a

RETURN

setup:
res=20 '12 'detail

DIM rs(1442)
FOR a=1 TO 1440
    rs(a)=226+400*SIN((a/2)*PI/180)
NEXT a

'Define the necessary variables;

sizez=.1 '                       how big do you want it?
points=8 '          The amount of points in the object
faces=6 '            The Amount of faces in the object
DIM x(points+1) '            Original X co-ordinate store
DIM y(points+1) '            Original Y co-ordinate store
DIM z(points+1) '            Original Z co-ordinate store
DIM tx(points+1) '       Transformed  X co-ordinate store
DIM ty(points+1) '        Transformed Y co-ordinate store
DIM tz(points+1) '        Transformed Z co-ordinate store
DIM f1(faces+1) '         Connections definition
DIM f2(faces+1) '         Connections definition
DIM f3(faces+1) '         Connections definition
DIM f4(faces+1) '         Connections definition
DIM r(faces+1) '                          Red Component
DIM g(faces+1) '                          Green Component
DIM b(faces+1) '                          Blue Component
DIM clss(faces+1) '                     Cell Shade Face?

xr=1
yr=2
zr=3

'Define Sine Tables for faster matrix calculations;

DIM cs(721)
DIM sn(721)

sw = SCREEN_WIDTH()
sh = SCREEN_HEIGHT()
PI = 3.1415

FOR ang=1 TO 720
    cs(ang)=COS(ang*(PI/360))
    sn(ang)=SIN(ang*(PI/360))
NEXT ang

' Read in the object's points

FOR a=1 TO points
    READ x(a),y(a),z(a)
NEXT a

'Read In Connections and face parameters

FOR a=1 TO faces
    READ f1(a)
    READ f2(a)
    READ f3(a)
    READ f4(a)
    READ r(a),g(a),b(a),clss(a)
NEXT a

'   The Object Description As Data!
'   The Data Below Describes A Cube.

'Points definition

'Below are the points of the object defined as x,y,z;

DATA -10,10,10,10,10,10,10,-10,10,-10,-10,10
DATA -10,10,-10,10,10,-10,10,-10,-10,-10,-10,-10

'Connection definition;
'Below are the faces of the object defined as vertice
'numbers, specified in clockwise order. These are followed
'by r,g,b values for the face and finally cell shaded
'parameter (0)=off (1)=on.

DATA 1,2,3,4,30,0,0,1
DATA 5,8,7,6,30,0,0,1
DATA 6,2,1,5,0,30,0,1
DATA 8,4,3,7,0,30,0,1
DATA 2,6,7,3,0,0,30,1
DATA 8,5,1,4,0,0,30,1

RETURN


Post Reply