Balls
Posted: Sat Jan 24, 2015 1:57 am
REM 3D Animated Balls
REM original by Dr. Chip: http://appball.com/basic/viewtopic.php? ... alls#p1146
REM mod&port by Operator with painters algorithm
REM MBasic 6.3.3 / Moto G / Android 4.4.4
rem ported to Smart Basic by DrChip
rem SB / iPhone 6 Plus / iOS 8.2 B4
Graphics
draw Color 0,0,0
scw = Screen_Width()
sch = Screen_Height()
csw = scw/2 'center_screenwidth
csh = sch/2 'center_screenheight
'27 balls with 3 coordinates (x,y,z)
DIM balls(30,5)
loop:
refresh off
Gosub Rotate
Gosub sort_z
Gosub draw
refresh on
GOTO loop
Rotate:
'increment = faster rotations
a = a + 0.01
b = b + 0.01
c = c + 0.01
i = 0
FOR x = -1 TO 1
FOR y = -1 TO 1
FOR z = -1 TO 1
i = i + 1
'rotate about the z-axis
x1 = x*COS(a) - y*SIN(a)
y1 = y*COS(a) + x*SIN(a)
'rotate about the y-axis
x2 = x1*COS(b) - z *SIN(b)
z1 = z *COS(b) + x1*SIN(b)
'rotate about the x-axis
y2 = y1*COS(c) - z1*SIN(c)
z2 = z1*COS(c) + y1*SIN(c)
'load array with rot. coordinates
balls(i,1) = x2
balls(i,2) = y2
balls(i,3) = z2
NEXT z
NEXT y
NEXT x
i = 0
Return
sort_z:
'bubble sort z coordinate
FOR n = 27 TO 1 STEP -1
FOR i = 1 TO n-1
IF balls(i,3) > balls(i+1,3) THEN
temp = balls(i,3)
balls(i,3) = balls(i+1,3)
balls(i+1,3) = temp
temp = balls(i,2)
balls(i,2) = balls(i+1,2)
balls(i+1,2) = temp
temp = balls(i,1)
balls(i,1) = balls(i+1,1)
balls(i+1,1) = temp
Endif
NEXT i
NEXT n
Return
draw:
'draw all the balls...
graphics clear 0,0,0
FOR i = 1 TO 27
'perspective trans. mod 500 value for depth
sx = 500*balls(i,1)/(balls(i,3)-8)+csw
sy = 500*balls(i,2)/(balls(i,3)-8)+csh
'green, darker if further away (z)
fill COLOR (balls(i,3)+2)*20/150,0,0
'circle, smaller if further away (z)
fill CIRCLE sx, sy size (balls(i,3) + 4) * 6
NEXT i
Return
REM original by Dr. Chip: http://appball.com/basic/viewtopic.php? ... alls#p1146
REM mod&port by Operator with painters algorithm
REM MBasic 6.3.3 / Moto G / Android 4.4.4
rem ported to Smart Basic by DrChip
rem SB / iPhone 6 Plus / iOS 8.2 B4
Graphics
draw Color 0,0,0
scw = Screen_Width()
sch = Screen_Height()
csw = scw/2 'center_screenwidth
csh = sch/2 'center_screenheight
'27 balls with 3 coordinates (x,y,z)
DIM balls(30,5)
loop:
refresh off
Gosub Rotate
Gosub sort_z
Gosub draw
refresh on
GOTO loop
Rotate:
'increment = faster rotations
a = a + 0.01
b = b + 0.01
c = c + 0.01
i = 0
FOR x = -1 TO 1
FOR y = -1 TO 1
FOR z = -1 TO 1
i = i + 1
'rotate about the z-axis
x1 = x*COS(a) - y*SIN(a)
y1 = y*COS(a) + x*SIN(a)
'rotate about the y-axis
x2 = x1*COS(b) - z *SIN(b)
z1 = z *COS(b) + x1*SIN(b)
'rotate about the x-axis
y2 = y1*COS(c) - z1*SIN(c)
z2 = z1*COS(c) + y1*SIN(c)
'load array with rot. coordinates
balls(i,1) = x2
balls(i,2) = y2
balls(i,3) = z2
NEXT z
NEXT y
NEXT x
i = 0
Return
sort_z:
'bubble sort z coordinate
FOR n = 27 TO 1 STEP -1
FOR i = 1 TO n-1
IF balls(i,3) > balls(i+1,3) THEN
temp = balls(i,3)
balls(i,3) = balls(i+1,3)
balls(i+1,3) = temp
temp = balls(i,2)
balls(i,2) = balls(i+1,2)
balls(i+1,2) = temp
temp = balls(i,1)
balls(i,1) = balls(i+1,1)
balls(i+1,1) = temp
Endif
NEXT i
NEXT n
Return
draw:
'draw all the balls...
graphics clear 0,0,0
FOR i = 1 TO 27
'perspective trans. mod 500 value for depth
sx = 500*balls(i,1)/(balls(i,3)-8)+csw
sy = 500*balls(i,2)/(balls(i,3)-8)+csh
'green, darker if further away (z)
fill COLOR (balls(i,3)+2)*20/150,0,0
'circle, smaller if further away (z)
fill CIRCLE sx, sy size (balls(i,3) + 4) * 6
NEXT i
Return