# Mintoris Forum

### Author Topic: 3D Balls animated  (Read 4072 times)

#### Operator

• Full Member
• Posts: 14
##### 3D Balls animated
« on: Aug 30, 2014, 01:10 PM »
3D Snippet…,
to possibly use the Mintoris sort() command in this snippet…

Code: [Select]
`REM 3D Animated BallsREM original by Dr. Chip: http://appball.com/basic/viewtopic.php?f=7&t=343&p=1146&hilit=3D+balls#p1146REM mod&port by Operator with painters algorithmREM MBasic 6.3.3 / Moto G / Android 4.4.4Graphics ONColor 0,0,0CLSscw = ScreenX()sch = ScreenY()csw = scw/2  'center_screenwidthcsh = sch/2  'center_screenheight'27 balls with 3 coordinates (x,y,z)DIM balls(27,3)lop: Gosub RotateGosub sort_zGosub drawGOTO lopRotate:'increment = faster rotationsa = a + 0.1b = b + 0.1c = c + 0.1i = 0FOR 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 yNEXT xi = 0Returnsort_z:'bubble sort z coordinateFOR 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 iNEXT nReturndraw:'draw all the balls…color 0,0,0CLSFOR 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)  COLOR 0,(balls(i,3)+2)*20,0      'circle, smaller if further away (z)  CIRCLE sx, sy, (balls(i,3) + 4) * 6,1NEXT i wait(10)Return`
« Last Edit: Aug 31, 2014, 12:31 AM by Operator »

#### Chuck

• Global Moderator
• Hero Member
• Posts: 1899
##### Re: 3D Balls animated
« Reply #1 on: Sep 16, 2014, 06:21 AM »
I don't think you can use the built in sort function, but try this:

Code: [Select]
`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,*)      balls(i,*) = balls(i+1,*)      balls(i+1,*) = temp()            Endif  NEXT iNEXT n`
Really cool snippet

-Chuck
« Last Edit: Sep 16, 2014, 06:28 AM by Chuck »

#### g3kko

• Full Member
• Posts: 46
##### Re: 3D Balls animated
« Reply #2 on: Sep 16, 2014, 12:52 PM »
Yea, cool snippet.

Rotate with touch…
Code: [Select]
`…Rotate:TouchEvent ac,in,x,y,100if ac<>-1thena=-y*0.005b=x*0.005endifi = 0….`

#### Chuck

• Global Moderator
• Hero Member
• Posts: 1899
##### Re: 3D Balls animated
« Reply #3 on: Sep 16, 2014, 06:50 PM »
I've adapted Jesse's modified bubble sort for this program. It's twice as fast as the standard bubble sort.

Sorting with Bubblesort

Sorting Algorithm Comparison

Code: [Select]
`  for i = 1 to 26    temp() = balls(i,*)    lowest = -1    for j = i + 1 to 27      if balls(j,3) < temp(3) then        temp() = balls(j,*)        lowest = j      endif    next j    if lowest > -1 then      balls(lowest,*) = balls(i,*)      balls(i,*) = temp()    endif  next i`
It smooths out the rotation nicely. There are even faster sorts out there.

-Chuck
« Last Edit: Sep 19, 2014, 01:02 AM by Chuck »

#### Operator

• Full Member
• Posts: 14
##### Re: 3D Balls animated
« Reply #4 on: Sep 21, 2014, 11:52 AM »
Thanks for boosting the speed with the
better sorting code. Now it runs about 62% faster (FPS) on my device.

I also tried to avoid sin/cos calculations using a look-up table (array) as in the plasma snippet, but this didn't help, it even made it again slower, only  about 53% faster (FPS)…

Does this mean that a sin/cos calculation in Mintoris is faster than an array look-up value command ?

#### Chuck

• Global Moderator
• Hero Member
• Posts: 1899
##### Re: 3D Balls animated
« Reply #5 on: Sep 21, 2014, 04:35 PM »
Does this mean that a sin/cos calculation in Mintoris is faster than an array look-up value command ?

I don't know. I'd have to see your array look-up code. I wouldn't be surprised if sin/cos used a really fast look-up table internally.

-Chuck

#### Operator

• Full Member
• Posts: 14
##### Re: 3D Balls animated
« Reply #6 on: Sep 21, 2014, 08:49 PM »
Here it is… code with
precalculated sin/cos values…
« Last Edit: Sep 22, 2014, 05:38 AM by Chuck »

#### Chuck

• Global Moderator
• Hero Member
• Posts: 1899
##### Re: 3D Balls animated
« Reply #7 on: Sep 21, 2014, 09:06 PM »
Yes, if your timings are right, sin/cos are faster than accessing an array element. It's hard to believe, but it's like the built in sort function. It is almost too fast to believe. There really isn't much overhead in accessing an array element. I wonder what a longer test, performed multiple times would show.

-Chuck

#### Operator

• Full Member
• Posts: 14
##### Re: 3D Balls animated
« Reply #8 on: Sep 23, 2014, 06:14 AM »
snippet to check this‥
Calculating sin/cos is
and stays faster than
look up.

#### Chuck

• Global Moderator
• Hero Member
• Posts: 1899
##### Re: 3D Balls animated
« Reply #9 on: Sep 24, 2014, 12:19 AM »
I spent all day doing timings, profiling code and making adjustments. I also tested sin/cos in java, 10000 iterations of sin/cos took 2ms. The time it takes to do an actual sin/cos in java is negligible. The whole difference in time you are seeing is the difference between accessing a regular variable verses accessing an array element. Remember that sin(X) and cos(X) still have to access the variable X. So what this really says is accessing a regular variable is about 30% faster than accessing an array element. With an array expression, you have to parse the index as well as accessing the array element.

Given all that I think this code is as fast as we can make it. I also made the rotation rate time based to smooth out the random Android interrupts. I set the program to Degree and added a degPerSec rotation variable. This was all a lot more fun than the performance profiling I did on Basic all day.

Code: [Select]
`Rotate:timeDiff = (Time() - lastTime) / 1000lastTime = Time()'increment = faster rotationsinc = degPerSec * timeDiffa = (a + inc) % 360b = (b + inc) % 360c = (c + inc) % 360as = Sin(a)ac = Cos(a)bs = Sin(b)bc = Cos(b)cs = Sin(c)cc = Cos(c)i = 0FOR x = -1 TO 1   FOR y = -1 TO  1     FOR z = -1 TO 1       i = i + 1      'rotate about the z-axis      x1 = x*ac - y*as         y1 = y*ac + x*as         'rotate about the y-axis      balls(i,1) = x1*bc - z*bs       z1 = z*bc + x1*bs      'rotate about the x-axis      balls(i,2) = y1*cc - z1*cs      balls(i,3) = z1*cc + y1*cs     NEXT z  NEXT yNEXT xi = 0Return`
I also moved the t1 time refresh to the bottom of the draw routine. That way the wait time is included in the frame time calculation. The wait time has to be included to get an accurate fps.

t = t + (Time() - t1)
FPS = int(frame*10000/t)/10
DrawText "FPS: "+Str\$(FPS),csw-80,csh+250,0,1
t1 = Time()

wait(10)
Return

-Chuck
« Last Edit: Sep 27, 2014, 07:19 PM by Chuck »

#### Operator

• Full Member
• Posts: 14
##### Re: 3D Balls animated
« Reply #10 on: Sep 27, 2014, 07:11 AM »
Thanks for this insight, correcting the timing and squeezing the most out of this snippet

#### Chuck

• Global Moderator
• Hero Member
• Posts: 1899
##### Re: 3D Balls animated
« Reply #11 on: Nov 28, 2016, 02:21 AM »
Try this program again with the 7.5.6 upgrade.

#### sraffens

• Global Moderator
• Sr. Member
• Posts: 220
##### Re: 3D Balls animated
« Reply #12 on: Nov 28, 2016, 04:58 AM »
I tried the 3D Balls - 03.bas and there 14.3 fps to about 24.x fps.
I changed some code and was able to get it to go to 30.x fps.
changing the GOTO loop to a do loop made 27.x fps.
change wait 10 to wait 1 made 30.x fps.
changing GOSUB to functions calls make no noticeable change at all.