Balls

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

Balls

Post by DrChip »

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
Attachments
image.jpg
image.jpg (88.39 KiB) Viewed 2033 times

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Balls

Post by Mr. Kibernetik »

My mod: breathing, colors, speed optimization, adapted for any screen

Code: Select all

/*
   3D Animated Balls
   original by Dr. Chip
   mod&port by Operator with painters algorithm
   MBasic 6.3.3 / Moto G / Android 4.4.4
   ported to Smart Basic by DrChip
   breathing,colors and speed optimization by Mr.K
   SB / iPhone 6 Plus / iOS 8.2 B4
*/

Graphics 

scw = Screen_Width()
sch = Screen_Height()
csw = scw/2 'center_screenwidth
csh = sch/2 'center_screenheight
scmin = min(scw,sch)
balls.scale = scmin / 80

'27 balls
option base 1
DIM balls.x(27), balls.y(27), balls.z(27), balls.c(27,3)

'set random color
for i=1 to 27 ! for j=1 to 3
balls.c(i,j)=rnd(1)
next j ! next i

refresh off
loop:
breath=(sin(time()*2)+1)/3+1
Gosub Rotate
Gosub draw
refresh
GOTO loop


Rotate:
a = time()/2
sa = sin(a)
ca = cos(a)

i = 0
FOR x = -1 TO 1
FOR y = -1 TO 1
FOR z = -1 TO 1

'rotate about the z-axis
x1 = x*ca - y*sa
y1 = y*ca + x*sa 

'rotate about the y-axis
x2 = x1*ca - z*sa 
z1 = z*ca + x1*sa

'rotate about the x-axis
y2 = y1*ca - z1*sa
z2 = z1*ca + y1*sa 

'load array with rot. coordinates
i += 1
balls.x(i) = x2*breath
balls.y(i) = y2*breath
balls.z(i) = z2*breath
NEXT z
NEXT y
NEXT x
Return


draw:
sort balls.z as si

'draw all the balls...
graphics clear 0,0,0

FOR j = 1 TO 27
i = si(j)
'perspective trans. mod 500 value for depth
sx = scmin*balls.x(i)/(balls.z(i)-8)+csw
sy = scmin*balls.y(i)/(balls.z(i)-8)+csh

'darker if further away (z)
dc=(balls.z(i)/breath+2)/3
fill COLOR balls.c(i,1)*dc,balls.c(i,2)*dc,balls.c(i,3)*dc

'circle, smaller if further away (z)
fill CIRCLE sx, sy size (balls.z(i) + 4) * balls.scale
NEXT j

Return

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

Re: Balls

Post by DrChip »

Very cool effects added!

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: Balls

Post by Dav »

Nice! It's amazing what can be done in smart Basic using a small amount of code.

- Dav

Post Reply