BBC BASIC for Windows
Programming >> Graphics and Games >> Rotating ice crystals
http://bb4w.conforums.com/index.cgi?board=graphics&action=display&num=1477475525

Rotating ice crystals
Post by David Williams on Oct 26th, 2016, 09:52am

Code:
      *ESC OFF
      *FLOAT 64
      MODE 8 : OFF
      ORIGIN @vdu%!208, @vdu%!212

      ON ERROR OSCLI "REFRESH ON":ON:CLS:PRINT'" ";:REPORT:PRINT" at line ";ERL:VDU7:END

      MinRadials% = 3
      MaxRadials% = 15
      QuadsPerRadial% = 10
      MaxPts% = 4 * QuadsPerRadial% * MaxRadials%
      DIM p(MaxPts%-1,2), q(MaxPts%-1,2), m(2,2)
      DIM col{(QuadsPerRadial%-1) r%, g%, b%}

      rho% = 700 : REM added to all rotated Z co-ordinates

      REM Time-dependent scale function parameters:
      MaxScale% = 1000
      t1% = 50  : REM object reaches full scale at time = t1%
      t2% = 750 : REM object remains at full scale while t1% <= time < t2%
      t3% = 800 : REM scale diminished to zero when t2% <= time < t3%
      c = MaxScale% - (MaxScale%/(t3%-t2%))*t2%

      newObj% = TRUE : REM Create a new object when this flag is TRUE

      *REFRESH OFF

      R% = RND(-TIME)

      time0% = TIME

      REPEAT
  
        CLS
  
        IF newObj% THEN
          numPts% = FNBuildNewObject
          newObj% = FALSE
        ENDIF
  
        T% = TIME
  
        dt% = T% - time0%
  
        REM A time-dependent compound function to control the scale of the object:
        IF dt% < t1% THEN scale = (MaxScale%/t1%)*dt%
        IF dt% >= t1% AND dt% < t2% THEN scale = MaxScale%
        IF dt% >= t2% AND dt% < t3% THEN scale = MaxScale% - (dt%-t2%)*MaxScale%/(t3%-t2%)
        IF dt% >= t3% THEN
          time0% = T%
          scale = 0
          newObj% = TRUE
        ENDIF
  
        REM Define time-dependent rotation angles:
        A = T%/122
        B = T%/176
        C = T%/100
        D = 2*PI*SIN(T%/520)
        E = 3*PI*COS(T%/720)
        sA = SIN(A)
        cA = COS(A)
        sB = SIN(B)
        cB = COS(B)
        sC = SIN(C)
        cC = COS(C)
  
        REM Rotate all of the object's points:
        m() = (cB*cC), -cB*sC, sB,  cA*sC+sA*sB*cC, cA*cC-sA*sB*sC, -sA*cB, sA*sC-cA*sB*cC, sA*cC+cA*sB*sC, cA*cB
        q() = p().m()
  
        REM Render all the polygons (quads) in the current object:
        colIndex% = 0
        n% = 0
        FOR I% = 0 TO numPts%-1 STEP 4
          IF n% = 0 THEN
            COLOUR 7, col{(colIndex%)}.r%, col{(colIndex%)}.g%, col{(colIndex%)}.b%
          ENDIF
          z0 = scale / (rho%+q(I%,2))
          z1 = scale / (rho%+q(I%+1,2))
          z2 = scale / (rho%+q(I%+2,2))
          z3 = scale / (rho%+q(I%+3,2))
          MOVE z0*q(I%,0), z0*q(I%,1)
          MOVE z1*q(I%+1,0), z1*q(I%+1,1)
          PLOT 85, z2*q(I%+2,0), z2*q(I%+2,1)
          PLOT 85, z3*q(I%+3,0), z3*q(I%+3,1)
          n% += 1
          IF n% = NumRadials% THEN
            colIndex% += 1
            n% = 0
          ENDIF
        NEXT
  
        *REFRESH
        WAIT 1
  
      UNTIL FALSE
      END

      DEF FNBuildNewObject
      LOCAL I%, J%, K%
      LOCAL r1, r2, r3, a, da, x1, y1, x2, y2, x3, y3, x4, y4
      NumRadials% = MinRadials% + RND(MaxRadials% - MinRadials% )
      FOR I% = 0 TO QuadsPerRadial%-1
        col{( I% )}.r% = 150 + RND(105)
        col{( I% )}.g% = col{(I%)}.r% + 1 - RND(10)
        col{( I% )}.b% = 220 + RND(35)
      NEXT I%
      FOR J% = 1 TO QuadsPerRadial%
        r1 = 256 * RND(1)
        r2 = r1 + 30 + 50*RND(1)
        r3 = r2 + 40 + 100*RND(1)
        da = 3 + 12*RND(1)
        FOR I% = 0 TO NumRadials%-1
          a = I% * (360/NumRadials%)
          x1 = r1*SINRAD(a)
          y1 = r1*COSRAD(a)
          x2 = r2*SINRAD(a-da)
          y2 = r2*COSRAD(a-da)
          x3 = r2*SINRAD(a+da)
          y3 = r2*COSRAD(a+da)
          x4 = r3*SINRAD(a)
          y4 = r3*COSRAD(a)
          p(K%,0) = x1
          p(K%,1) = y1
          p(K%+1,0) = x2
          p(K%+1,1) = y2
          p(K%+2,0) = x3
          p(K%+2,1) = y3
          p(K%+3,0) = x4
          p(K%+3,1) = y4
          K% += 4
        NEXT I%
      NEXT J%
      = K%
 

Re: Rotating ice crystals
Post by michael on Oct 26th, 2016, 2:14pm

You have amazing programming skill.. Hey.... how about a driving through a world sim? (maybe a racetrack?)
Re: Rotating ice crystals
Post by David Williams on Oct 26th, 2016, 5:38pm

Here's some pretty old 3D stuff (from several years ago), all made with BB4W. The trouble is, I seem to have lost the source code for most of them. sad Most BB4W users will have already seen these, I think.


3D Tour Demo (This takes you on a tour of a small 3D world; after 3 or 4 minutes, you take off and fly around in the sky):

https://drive.google.com/open?id=0B3j5sIQi9SskWGJVUjBPXzBCRWc


3D Maze (a.k.a. 'Viewer_2') - use the Arrow keys, and hold down the 'Ctrl' key to move faster:

https://drive.google.com/open?id=0B3j5sIQi9SskMjRBWlZsZjN5UDg


3D Maze Demo II (Use the arrow keys to explore the maze)

https://drive.google.com/open?id=0B3j5sIQi9SskdWNJNVhnVDk0T0k


Crystaloids Demo (This was meant to become a full game - it's just a demo; use the arrow keys and press 'Z' to fire):

https://drive.google.com/open?id=0B3j5sIQi9SskSU1tLTR3dnRERFE


Xmas Demo II (Includes a rotating 3D snowman):

https://drive.google.com/open?id=0B3j5sIQi9SskV2dUbFBuVTNrNjQ


Just a colourful, fuzzy pseudo-3D tunnel:

https://drive.google.com/open?id=0B3j5sIQi9SskbW91aWtpRlM5N1U



David.
--


Re: Rotating ice crystals
Post by David Williams on Nov 7th, 2016, 7:13pm

Here is a modified version of the 'Ice Crystals' program (EXE only - sorry), in which a translucent 'ice crystal' is rendered over a wintry background image.

OneDrive: https://1drv.ms/u/s!Asv5H0sZTvQ-jQJNUPJDbyXkn6Eq

Google Drive: https://drive.google.com/open?id=0B3j5sIQi9SskQ1ctbENBTms1R1E

Because it's an EXE file from an 'untrusted' source (i.e. me), Windows might prevent you from running it. You may have to tell Windows that you trust the file. I did scan it for viruses/malware using F-Secure AV, with no problems detected.


David.
--
Re: Rotating ice crystals
Post by KenDown on Nov 11th, 2016, 04:59am

That's what Elite should have been - descend to the planet and "mine" it for resources by collecting the crystals before going into hyperspace and heading for the next planet!

A pity you've lost the source code for that 3D World.