News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Mayhem

Started by RaverDave, October 15, 2006, 12:01:30 PM

Previous topic - Next topic

RaverDave

Ok, this is totally unexpected results, I tried to convert some code from one language to another for some bouncing balls,anyhow,things went wrong of course, and what resulted is kinda interesting code, so just incase anybody needs this kinda interesting code..here it is! :D:D

[/; PROJECT : mazetest
; AUTHOR  : Raverdave
; CREATED : 13/10/2006
; EDITED  : 15/10/2006
; ---------------------------------------------------------------------
OpenScreen 640,480,32,2
SetFPS 60
ScreenVsync On
Mouse Off
Global MyMap = GetFreeMap()
Global mapx=0
Global mapy=0
Global currlev=1
Global maxlev=99
Global TileWidth=32
Global TileHeight=16
Global Number_of_tiles = 9
Global MyMap = GetFreeMap()
Global screenwidth=GetScreenWidth()
Global screenheight=GetScreenHeight()
global NUMBEROFBALLS = Rnd(6)+1
global NUMBEROFGRAV = Rnd(4)+1
Global timer1=0
Global timer2=0
Global numberofballs=5
Dim dx(NUMBEROFBALLS,40) As Float()
Dim dy(NUMBEROFBALLS,40) As Float()

Type balltype
   x# As Float
   y# As Float
   vx# As Float
   vy# As Float
   ax# As Float
   ay# As Float
   c1 As Integer : c2 As Integer : c3 As Integer
EndType


Type gravtype
   x# As Float
   y# As Float
   size# As Float
EndType

Dim ball(NUMBEROFBALLS) As balltype
Dim grav(NUMBEROFGRAV) As gravtype


`Init the balls
For j=1 To NUMBEROFBALLS
   ball(j).x# = Rnd(SCREENWIDTH-40)+20
   ball(j).y# = Rnd(SCREENHEIGHT-40)+20
   ball(j).vx# = 0.0
   ball(j).vy# = 0.0
   ball(j).ax# = 0.0
   ball(j).ay# = 0.0
   ball(j).c1 = 128*Rnd(1)+127
   ball(j).c2 = 128*Rnd(1)+127
   ball(j).c3 = 128*Rnd(1)+127
   For i=1 To 40
         dx(j,i) = ball(j).x#
         dy(j,i) = ball(j).y#
   Next i
Next j

`init the planets (gravs)
For j=1 To NUMBEROFGRAV
   grav(j).x# = Rnd(SCREENWIDTH-100) + 70.0
   grav(j).y# = Rnd(SCREENHEIGHT-100) + 70.0
   grav(j).size# = Rnd(30) + 10.0
Next j

global GRAVITY# = 0.1
global FRICTION# = 0.95
CreateMap MyMap,99
maketiles()
;setuplev()


Do
Cls 0
mayhem()

;DrawMap MyMap,currlev,mapx,mapy
Text 300,100,FPS()
;gosub Get_Max_e

Sync
Loop

function mayhem()
For j=1 To NUMBEROFBALLS

      `Reset the acceleration vars
      ball(j).ax# = 0.0
      ball(j).ay# = 0.0

      `Shufle the trail array
      For i=39 To 1 Step -1
        dx(j,i+1) = dx(j,i)
        dy(j,i+1) = dy(j,i)
      Next i

      `Gravity
      For i=1 To NUMBEROFGRAV
         `Calculate the distance and gravity effect of each grav
         dist# = Sqrt((ball(j).x#-grav(i).x#)^2 + (ball(j).y#-grav(i).y#)^2)
         accel# = (GRAVITY# * grav(i).size#) / dist#
         `Update the acceleration and velocity
         ball(j).ax# = ball(j).ax# + accel#*((grav(i).x#-ball(j).x#)/dist#)
         ball(j).ay# = ball(j).ay# + accel#*((grav(i).y#-ball(j).y#)/dist#)
         ball(j).vx# = ball(j).vx# + ball(j).ax#
         ball(j).vy# = ball(j).vy# + ball(j).ay#
      Next i

      ball(j).vx# = ball(j).vx# * FRICTION#
      ball(j).vy# = ball(j).vy# * FRICTION#


      `Update the velocities and positions
      ball(j).x# = ball(j).x# + ball(j).vx#
      ball(j).y# = ball(j).y# + ball(j).vy#

      `Check screen limits and bounce if neccesary
      If ball(j).x#<10.0
         ball(j).x#=10.0
         ball(j).vx# = -ball(j).vx# * FRICTION#
         ball(j).vy# = ball(j).vy# * FRICTION#
      EndIf
      If ball(j).x#>SCREENWIDTH-10
         ball(j).x#=SCREENWIDTH-10
         ball(j).vx# = -ball(j).vx# * FRICTION#
         ball(j).vy# = ball(j).vy# * FRICTION#
      EndIf
      If ball(j).y#<10.0
         ball(j).y#=10.0
         ball(j).vx# = ball(j).vx# * FRICTION#
         ball(j).vy# = -ball(j).vy# * FRICTION#
      EndIf
      If ball(j).y#>SCREENHEIGHT-10
         ball(j).y#=SCREENHEIGHT-10
         ball(j).vx# = ball(j).vx# * FRICTION#
         ball(j).vy# = -ball(j).vy# * FRICTION#
      EndIf

      `Update trail position
      dx(j,1) = ball(j).x#
      dy(j,1) = ball(j).y#
     
     
      DrawImage 1,ball(j).x#, ball(j).y#,1

Next
endfunction


Function maketiles()
; Loop through and draw a series of randomly coloured Boxes to the screen
Restore cols
cols:
Data RGB(255,255,255)     `  White Block
Data RGB(255,20,25)     `  RED/Block
Data RGB(255,255,25)    `  Yellow Block
Data RGB(25,255,25)     `  GREEN
Data RGB(255,25,255)    `  PURPLE
Data RGB(55,95,155)    `  PURPLE
Data -1

For lp=1 To Number_of_tiles
xpos=lp*tileWidth
Col=ReadData()

BoxC xpos+0,ypos+0,Xpos+tilewidth,Ypos+tileheight,1,RGBFade(Col,50.0)
c1=RGBFade(Col,100.0)
;BoxC xpos,ypos,tilewidth,tileheight,RGB(col)
ShadeBox xpos+1,ypos+1,Xpos+Tilewidth-1,Ypos+Tileheight-2,c1,c1,c2,c2 
Next
; Grab the box graphics that we've just drawn as image.
cls 0
circle 15,15,15,0
TempImage=GetFreeImage()
GetImage TempImage,0,0,32,32
MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)

EndFunction


Function setuplev()
CreateLevel MyMap,currlev,1000,1000

Restore map

For n=1 To 5
xl=ReadData()

SetCursor 1,200
Print xl
Sync
WaitKey
  If t>0
  tilenum=tilenum+1
  PokeLevelTile MyMap,currlev,xl,yl,1
EndIf
Next n
EndFunction
map:
Data 1,1,1,2,1,1,3,1,1,4,1,1,5,1,1


Change the var numberofballs value to whatever,its a real mess,redundant code here n there, totally lacks structure!,but ah well,back to the drawing board for meError Missing Closing Square Bracket

Ian Price

Yeah... err... interesting... :S

What exactly were you trying to do?
I came. I saw. I played some Nintendo.

RaverDave

gimme a day or so and i will show you! ;)

stef

Not that uninteresting

It's  generating some socalled 'strange attractors' (you better see this with 100 balls and without fps-restriction)

For background-activities you can simulate behavior of midges flying around a lamp or the behavior of piranhas :)