[B3D]Elastischer Stoss(Physik)

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Joel

Betreff: [B3D]Elastischer Stoss(Physik)

BeitragDo, Okt 07, 2010 16:46
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich habe mal eine kleine simulation von einem Elastischen Stoss von Kugeln gemacht.
Perfekt funktioniert hat es nie, aber trotzdem kann vielleicht jemand brauchen.

Hier der Code: [AUSKLAPPEN]

Graphics 800,600,16,2
SetBuffer BackBuffer()
timer=CreateTimer(30)



Type physikObjekt
   Field x#,y#,speed#,winkel,num
End Type
Global distance,zahl,numzahl

;Objekte erstellen
Global teil1=CreatePhysikObjekt(400,300)


For n=-10 To 10
   If n><0 Then CreatePhysikObjekt(400+25*n,300)
Next


Global ZentralVectorX#,ZentralVectorY#,TangentialVectorX#,TangentialVectorY#,x_dist#[1],y_dist#[1],Dist,t#,u#,v#,w#,Exit_




While Not KeyHit(1)
   Text 0,10,"pfeiltaste um impulse zu geben"
   
   
   If KeyDown(200)Then AddImpulse(teil1,-90,5)
   If KeyDown(208)Then AddImpulse(teil1,90,5)
   If KeyDown(205)Then AddImpulse(teil1,0,5)
   If KeyDown(203)Then AddImpulse(teil1,180,5)
   
   
   
   
   UpdatePhysikObjekt()
   WaitTimer timer
   Flip 0
   Cls
Wend
End


Function CreatePhysikObjekt(x,y)
   
   p.physikObjekt=New physikObjekt
   p\x=x
   p\y=y
   p\num=numzahl
   numzahl=numzahl+1
   Return p\num
End Function

Function UpdatePhysikObjekt()
   
   For p.physikObjekt=Each physikObjekt
      
      p\x=p\x+Cos(p\winkel)*p\speed; Kugeln bewegen
      p\y=p\y+Sin(p\winkel)*p\speed
      
      
      
      
      If p\y=>590 Or p\y=<10 Then p\winkel=p\winkel*-1      ;wenn eine kugel an den rand vom bildschirm kommen dann abprallen
      If p\x=>790 Or p\x=<10 Then p\winkel=(p\winkel+180)*-1
      
      
      For p2.physikObjekt=Each physikObjekt
         If p\num<>p2\num And p2\num><p\num
            If Sqr((p\x-p2\x)^2+(p\y-p2\y)^2)=<20 And Exit_=0;distanz prüfen
               
               Dist=Sqr((p\x-p2\x)^2+(p\y-p2\y)^2)
                  
                  
                  ZentralVectorX=(p2\x-p\x)/Dist
                  ZentralVectorY=(p2\y-p\y)/Dist
                  TangentialVectorX=ZentralVectorY
                  TangentialVectorY=-ZentralVectorX
                  
                  t=(p\speed*Sin(p\winkel)*TangentialVectorX+p\speed*Cos(p\winkel)*TangentialVectorY)
                  u=(p2\speed*Sin(p2\winkel)*ZentralVectorX+p2\speed*Cos(p2\winkel)*ZentralVectorY)
                  v=(p2\speed*Sin(p2\winkel)*TangentialVectorX+p2\speed*Cos(p2\winkel)*TangentialVectorY)
                  w=(p\speed*Sin(p\winkel)*ZentralVectorX+p\speed*Cos(p\winkel)*ZentralVectorY)
                  
                  
                  
                  p2\speed=Sqr(t^2+u^2)
                  p2\winkel=(ATan2(u*ZentralVectorX+t*TangentialVectorX,u*ZentralVectorY+t*TangentialVectorY))
                  
                  p\speed=Sqr(v^2+w^2)
                  p\winkel=(ATan2(v*TangentialVectorX+w*ZentralVectorX,v*TangentialVectorY+w*ZentralVectorY))
                  
                  
                  
                  
                  Exit_=1
                  
            EndIf
            If Sqr((p\x-p2\x)^2+(p\y-p2\y)^2)>20 Then Exit_=0;Überprüfen ob die Objekte auseinandergegangen sind
         EndIf
      Next
      
      
      
      
      
      Oval p\x-10,p\y-10,20,20
      
   Next
End Function

Function AddImpulse(hanle,winkel,speed#)
   For p.physikObjekt=Each physikObjekt
      If hanle=p\num
         p\winkel=winkel
         p\speed=speed
      EndIf
   Next
End Function


Komentare erwünscht.

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group