Black hole - how to speed up?
Posted: Thu Jul 04, 2013 1:24 pm
				
				Hi,
I finally think I have gotten my head around how vectors work and have managed to borrow some math calculations to do things like find angle, distance, direction and movement required. I have cobbled this "black hole simulator!" together.
How would I go about making this faster? Is it the use of circle? Should I be using pointers to speed things up? Sprites?
			I finally think I have gotten my head around how vectors work and have managed to borrow some math calculations to do things like find angle, distance, direction and movement required. I have cobbled this "black hole simulator!" together.
How would I go about making this faster? Is it the use of circle? Should I be using pointers to speed things up? Sprites?
Code: Select all
;black hole - em_uk
;math codes borrowed from pb forums
;
#RAD = 0.0175
max=1000
Structure XY
  X.f
  Y.f
EndStructure
Structure T_PINGPONG
  min.i
  max.i
  counter.i
  direction.i
  delay.i
  inc.i
  val.i
EndStructure
Structure sVector
  c.XY
  angle.f
EndStructure
Structure player
  direction.sVector
  speed.f
  type.l
  state.l
  color.i
EndStructure
;Define pl1.player
Dim pl1.player(max)
Define pl2.player
Procedure addMovement( *V.sVector, speed.f )
  *V\c\x + Cos( *V\angle * #RAD ) * speed
  *V\c\y + Sin( *V\angle * #RAD ) * speed
EndProcedure
Procedure.f findangle(x1.f,y1.f,x2.f,y2.f) 
  
  Protected a.f,b.f,c.f,angle.f
  a.f = x1-x2 
  b.f = y2-y1 
  c.f = Sqr(a*a+b*b) 
  angle.f = ACos(a/c)*57.29577 
  If y1 < y2 
    angle=360.0-angle
  EndIf 
  
  ProcedureReturn angle.f
EndProcedure
Procedure.i PingPong (*p.T_PINGPONG)
  
  If *p\val>*p\delay
    If *p\direction = 0
      *p\direction = *p\inc
      *p\counter = *p\min
    Else
      *p\counter + *p\direction   
      If *p\counter =< *p\min 
        *p\direction = 0
      ElseIf *p\counter >= *p\max
        *p\direction = -*p\direction
      EndIf
      *p\val=0
    EndIf   
  Else
    *p\val + 1
  EndIf
  
  ProcedureReturn *p\counter
EndProcedure
Define PP1.T_PINGPONG 
Define PP2.T_PINGPONG 
Define PP3.T_PINGPONG 
pp1\min=0
pp1\max=699
pp1\delay=0
pp1\inc=1
pp1\delay=5
pp2\min=10
pp2\max=500
pp2\inc=12
pp3\min=20
pp3\max=20
pp3\inc=1
pp3\delay=250
For a = 0 To max
  
  pl1(a)\direction\c\x = Random(749)
  pl1(a)\direction\c\y = Random(599)
  pl1(a)\speed = Random(4,1)
  pl1(a)\state = PingPong(@pp3)
  pl1(a)\type=Random(5,1)
  If  pl1(a)\type>2
    r=100:g=100:b=100
  Else
    r=255:g=255:b=255
  EndIf
  
  pl1(a)\color=RGB(Random(r,1),g,Random(b,1))
Next
pl2\direction\c\x = 400
pl2\direction\c\y = 300
InitSprite() : InitKeyboard()
OpenWindow(0,0,0,800,600,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,600)
;OpenScreen(800,600,32,"")
Repeat
  
  If IsWindow(0)
    event=WindowEvent()
  EndIf
  
  StartDrawing(ScreenOutput())
  
  
  For a = 0 To max
    
    pl1(a)\direction\angle=findangle(pl2\direction\c\x,pl2\direction\c\y,pl1(a)\direction\c\x,pl1(a)\direction\c\y)
    
    addMovement(@pl1(a)\direction,pl1(a)\speed)
    
    If pl1(a)\direction\c\y>299 And pl1(a)\direction\c\y<301 Or pl1(a)\direction\c\x>400 And pl1(a)\direction\c\x<401;pl1(a)\type=0
      
      num=Random(4,1)
      If num=1
        pl1(a)\direction\c\x = -20 + Random(800)
        pl1(a)\direction\c\y = 0
        
      ElseIf num = 2
        pl1(a)\direction\c\x = -20+Random(800)
        pl1(a)\direction\c\y = 600
        
      ElseIf num = 3     
        pl1(a)\direction\c\x = 0
        pl1(a)\direction\c\y = -20 + Random(600)
        
      ElseIf num = 4
        pl1(a)\direction\c\x = 800
        pl1(a)\direction\c\y = -20 + Random(600)
        
      EndIf
      
      pl1(a)\type=Random(2,1)
      If  pl1(a)\type<2
        r=100:g=100:b=100
      Else
        r=255:g=255:b=255
      EndIf
      
      pl1(a)\color=RGB(Random(r,1),g,Random(b,1))
      
    EndIf
    
    Circle(pl1(a)\direction\c\x,pl1(a)\direction\c\y,pl1(a)\type,Pl1(a)\color)
    
  Next
  
  StopDrawing()
  
  pl2\direction\c\X=PingPong(@PP1)
  
  ExamineKeyboard()
  If event=#PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    quit=1
  EndIf
  Delay(1)
  FlipBuffers()
  ClearScreen(#Black)
Until quit
 I'm having a lot of fun with vectors and the trig that goes with it. When I was putting this together last night, I didn't think I would succeed after many failed attempts!
 I'm having a lot of fun with vectors and the trig that goes with it. When I was putting this together last night, I didn't think I would succeed after many failed attempts!