UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on July 08, 2004, 09:50:40 AM

Title: Scrolling Cubes
Post by: kevin on July 08, 2004, 09:50:40 AM
Scrolling Cubes
Example By SuperMonkey & Uwdesign 8th,July,2004  (Updated Dec/2012)

This PlayBASIC demo renders a rotating cube to an image than rendering that as a tile across the display.




[pbcode]
;---------------------------------------------------------------------
;  Example By SuperMonkey & Uwdesign 8th,July,2004  (Updated Dec/2012)
;---------------------------------------------------------------------

Rem create some space for the vertex and face data
Dim points#(8,3)
Dim faces(6,5)
Dim matrix#(3,3)
matrix#(1,1)=1
matrix#(2,2)=1
matrix#(3,3)=1

Rem read in the vertex data
For p = 1 To 8
  points#(p,1) =  ReadData()
  points#(p,2) =  ReadData()
  points#(p,3) =  ReadData()
Next p

Rem read in the face data
For f = 1 To 6
  faces(f,1) = ReadData()
  faces(f,2) = ReadData()
  faces(f,3) = ReadData()
  faces(f,4) = ReadData()
  faces(f,5) = ReadData()
Next f

Rem vertex data

Data -1,-1,-1
Data 1,-1,-1
Data 1,1,-1
Data -1,1,-1

Data -1,-1,1
Data 1,-1,1
Data 1,1,1
Data -1,1,1


Rem face data
;right/left
Data 2,6,7,3, RGB(180,150,230)
Data 5,1,4,8, RGB(80,100,120)
 
Data 1,2,3,4, RGB(50,70,90)
Data 6,5,8,7, RGB(100,140,180)

; top /bot
Data 5,6,2,1, RGB(0,100,50)
Data 4,3,7,8, RGB(50,160,100)





Rem some space to put the rotated points
Dim rotated#(8,3)

Rem variables which control the size of the cube
x_scale# = 100.0
y_scale# = 100.0
z_scale# = 100.0
Rem set the initial rotation angles to 0
tilt#=0.0
turn#=0.0
roll#=0.0


 
  CreateShape 1,6,6  
  SetShapeEdge 1,1,1,2
  SetShapeEdge 1,2,2,3
  SetShapeEdge 1,3,3,4
  SetShapeEdge 1,4,4,1

CreateImage 1,50,50


  Repeat
 Cls 0


  Rem prepare the rotation matrix
  A#=Cos(tilt#) : B#=Sin(tilt#)
  C#=Cos(turn#) : D#=Sin(turn#)
  E#=Cos(roll#) : F#=Sin(roll#)
  AD#=A#*D#
  BD#=B#*D#

; Calc Rotattion Martrix  
   m11#=C#*E#
  m21#=-1*C#*F#
  m31#=D#
  m12#=BD#*E#+A#*F#
  m22#=-1*BD#*F#+A#*E#
  m32#=-1*B#*C#

  m13#=-1*AD#*E#+B#*F#
  m23#=AD#*F#+B#*E#
  m33#=A#*C#

  Rem rotate all the points using the matrix
  For p=1 To 8

   pointx#=points#(p,1)
   pointy#=points#(p,2)
   pointz#=points#(p,3)

     rotated#(p,1) = (m11# * pointx# + m12# * pointy# + m13# * pointz#) * x_scale#
     rotated#(p,2) = (m21# * pointx# + m22# * pointy# + m23# * pointz#) * y_scale#
     rotated#(p,3) = (m31# * pointx# + m32# * pointy# + m33# * pointz#)
   
     Rem now do the perspective calculation
     z# = rotated#(p,3) + 7.0
     rotated#(p,1) = 25.0 + (rotated#(p,1) ) / z#
     rotated#(p,2) = 25.0 + (rotated#(p,2) )/ z#
 Next p


  RenderToImage 1

  Cls 0
  For f=1 To 6
   Rem p1 -> p4 are the points on the face
   p1# = faces(f,1)
   p2# = faces(f,2)
   p3# = faces(f,3)
   p4# = faces(f,4)
 Col=faces(f,5)
 vx1#=rotated#(p1#,1)
 vy1#=rotated#(p1#,2)

 vx2#=rotated#(p2#,1)
 vy2#=rotated#(p2#,2)
 
 vx3#=rotated#(p3#,1)
 vy3#=rotated#(p3#,2)

  ; Cross product
   If (((vx2#-vx1#)*(vy3#-vy1#))-((vx3#-vx1#)*(vy2#-vy1#)))>0

    Ink col
 ; set the shapes vertex for this quad..
    SetShapeVertex 1,1,vx1#,vy1#
    SetShapeVertex 1,2,vx2#,vy2#
    SetShapeVertex 1,3,vx3#,vy3#
    SetShapeVertex 1,4,rotated#(p4#,1),rotated#(p4#,2)
 ; draw it
    DrawShape 1,0,0,2
 EndIf
 Next f

  RenderToScreen
  TileImage 1,x,x,1


  x=Mod(x+1,50)

Rem animate the tilt, turn and roll values
 tilt# = tilt#+0.1
 turn# = turn#+0.2  
 roll# = roll#+0.3

  Sync
  Until (Inkey$() = "q")
END

[/pbcode]


Title: Scrolling Cubes
Post by: Draco9898 on July 08, 2004, 02:02:53 PM
Yikes, math is evile... :lol:
Title: Scrolling Cubes
Post by: Supermonkey on July 08, 2004, 02:56:29 PM
Cool some of my code is being used. Any chance you can talk me through how to get camera rotation to work?

cheers

Jonny
Title: Scrolling Cubes
Post by: kevin on July 08, 2004, 04:41:27 PM
it doesn't use a camera... all i did was use  PlayBASIC's shape command (a polygon filler)  to connect the edges of your cube (the quad of verts) and draw those verts as a filled the polygon...

it draws one cube to a small image, rather than the screen.. Then tiles this cube across the screen making the effect..  It's a old school demo effect.. Just though it'd be a different way to show off  Shapes...
Title: Scrolling Cubes
Post by: Draco9898 on July 08, 2004, 05:58:03 PM
Can you 'shade' shapes? That could be quite useful! or fill these shapes with images?  :lol:

You know the title screen for final fantasy 5 and the text gets rendered inside in? that's what I'm thinking  :D
Title: Scrolling Cubes
Post by: kevin on July 08, 2004, 06:07:39 PM
you can create a negative of a shape that could be used to mask an image.  So it's fairly handy..  

Shading is option too, not for a while though..
Title: Scrolling Cubes
Post by: Supermonkey on July 08, 2004, 06:48:31 PM
Quoteit doesn't use a camera... all i did was use PB's shape command

Sorry didn't mean to confuse you, I understood how the code worked I just wondered if you could explain to me how I could impliment camera rotation (if its not too much bother). Maybe I should make a new topic?

cheers

Jonny
Title: Scrolling Cubes
Post by: kevin on July 08, 2004, 07:01:53 PM
oh, i see what you mean .. now :)

that's another subject, make a new thread..  I can give you basics,  it'd take forever to explain it great detail..