Planeten Simulation in 3D :-)

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Markus2

Betreff: Planeten Simulation in 3D :-)

BeitragDo, Okt 21, 2004 20:18
Antworten mit Zitat
Benutzer-Profile anzeigen
Planeten Simulation in 3D .
Berechnungen stammen von Orbital Simulator 2D .

BlitzBasic: [AUSKLAPPEN]

.Top
;-------------------------------------------------------------------------------
; MR 21.10.2004 BlitzBasic 3D V1.87 für PC
;-------------------------------------------------------------------------------

Graphics3D 800,600,16,1
SetBuffer BackBuffer()
AppTitle \" Orbital Simulation\"

;------------------------------------------------------------------------------- Kamera
.Camera

Global Cam=CreateCamera()
CameraRange Cam,1,100000
PositionEntity Cam,0,0,-500
CameraClsMode Cam,0,True ;Wegen Hintergrund Bild CLS aus !

;------------------------------------------------------------------------------- Licht
.Licht

AmbientLight 32,32,32

Global light=CreateLight(Cam)

;-------------------------------------------------------------------------------
.Globale

Global fntStatus=LoadFont(\"Arial\",16,True,False,False)
SetFont fntStatus

Global imgStars=StarImage()

Global Simulating=True
Global PlanetPull=True

Const OBJ_STAR=0
Const OBJ_PLANET=1

Global ePlanet=CreateSphere(8)
ScaleMesh ePlanet,0.25,0.25,0.25

;------------------------------------------------------------------------------- Type
.Typen

Type ObjectType
Field X#,Y#,Z#
Field Radius#
Field Gravity#
Field XSpeed#,YSpeed#,ZSpeed#
Field Red,Green,Blue
Field xgrav#,ygrav#,zgrav#
Field Body ;as in OBJ_STAR,PLANET,MOON..
Field ID
Field Entity
End Type

;-------------------------------------------------------------------------------

CreateObjects()

MainLoop()
End

;###############################################################################

.Main
Function MainLoop()

Local t1# ;zum Zeit messen

While Not KeyHit(1)

t1 = MilliSecs()

If KeyHit(57) Then simulating=Not simulating

DrawBlock imgStars,0,0

;---------------------------------------

If simulating Then
PlanetPhysic(1)
PlanetMove()
EndIf

;---------------------------------------

RenderWorld

;---------------------------------------

If Simulating Then
Color 80,80,80
Text 0,0,\"Simulation: SPACE für Pause\"
Else
Color 80,80,80
Text 0,0,\"Pause: SPACE für Simulation\"
End If

;---------------------------------------

While Abs(MilliSecs() - t1) < 10 ;schneller als 1/50 sekunde = 20 ms ??? :-) Dann kurze Pause machen !
;wenn man hier END einbaut und das Programm sich beenden ist der Computer schnell genug Wink
;nix machen :-)
Wend

;---------------------------------------

Flip

Wend

End Function

;###############################################################################

Function PlanetPhysic(frames)

Local k

For k=1 To frames

For planet.objecttype = Each objecttype
planet\xgrav=0:planet\ygrav=0:planet\zgrav=0
For star.objecttype = Each objecttype

;--------------------------------------------------------------------------
;THIS IS THE MECHANCICS HERE. THIS IS WHAT CAUSES THE GRAVITY
;The rest just makes it look good Wink
;--------------------------------------------------------------------------

If (star\body=OBJ_STAR Or (star\body=OBJ_PLANET And PlanetPull=True)) And star\id<>planet\id And planet\body<>OBJ_STAR Then

gravzpull#=Sqr((star\x-planet\x)^2+(star\z-planet\z)^2)
gravpull#=star\gravity/(Sqr((gravzpull)^2+(star\y-planet\y)^2))
;I needed two Pythag equations because we're in 3D.
;star\gravity DIVIDED by ... means it gets less the further away you are

gravyangle#=Anglebetween(planet\x,planet\y,star\x,star\y)
gravzangle#=Anglebetween(planet\x,planet\z,star\x,star\z)
;Calculate the height angle and base angle (3D) between planet + sun

planet\xgrav=gravpull*Sin(gravyangle)
planet\ygrav=gravpull*Cos(gravyangle)
planet\zgrav=gravpull*Cos(gravzangle)
;Trigonometry. Xgrav,Ygrav and Zgrav are components of the overall
;acceleration, \"gravpull\". Calculate their component values by
;drawing an imaginary triangle and using Sin and Cos.

planet\xspeed=planet\xspeed+planet\xgrav
planet\yspeed=planet\yspeed+planet\ygrav
planet\zspeed=planet\zspeed+planet\zgrav
;Accelerate in whatever direction you need.
End If
Next
If planet\body<>OBJ_SUN Then
planet\x=planet\x + planet\xspeed
planet\y=planet\y + planet\yspeed
planet\z=planet\z + planet\zspeed
End If

;-------------------------------------------------------------------------

;If Abs(planet\x)>2000 Or Abs(planet\y)>2000 Or Abs(planet\z)>2000 Then PlanetFree Planet
Next

Next

End Function

;###############################################################################

Function PlanetMove()

For planet.objecttype = Each objecttype
PositionEntity planet\Entity,planet\x,planet\y,planet\z
Next

End Function

;###############################################################################

Function PlanetFree(planet.objecttype)

FreeEntity planet\Entity
Delete planet

End Function

;###############################################################################

Function AngleBetween(x#,y#,xx#,yy#) ;Takes two points and determines the angle between them from x,y

;This function thinks of y as going upwards, NOT downwards.
;I do this because it's easier mathematically, and then draw everything upside down.

If xx-x=0 And yy-y<0 Then
angle=270
ElseIf xx-x>0 And yy-y=0 Then
angle=90
ElseIf xx-x=0 And yy-y>0 Then
angle=0
ElseIf xx-x<0 And yy-y=0 Then
angle=180
ElseIf yy-y>0 Then
Angle=ATan((xx-x)/(yy-y))
Else
Angle=180+ATan((xx-x)/(yy-y))
End If
Return angle

End Function

;###############################################################################

Function CreateObjects()

SeedRnd 2

;Make some various celestial bodies. aaaaaah
star.objecttype = New objecttype
star\gravity=80
star\radius=50
star\body=OBJ_STAR
star\red=255:star\green=150
star\Entity=CopyEntity(ePlanet)
ScaleEntity star\Entity,star\radius,star\radius,star\radius
EntityColor star\Entity,star\red,star\green,star\blue

For i=1 To 20
planet.objecttype = New objecttype
While Abs(planet\x)<60 Or Abs(planet\y)<60 Or Abs(planet\z)<60
planet\x=Rand(-120,120)
planet\y=Rand(-120,120)
planet\z=Rand(-120,120)
Wend
planet\ID=i
planet\body=OBJ_PLANET

planet\red=Rand(255)
planet\green=Rand(255)
planet\blue=Rand(255)

planet\radius=Rand(10,20)
planet\gravity=planet\radius
planet\Entity=CreateSphere(8)
ScaleMesh planet\Entity,0.25,0.25,0.25
ScaleEntity planet\Entity,planet\radius,planet\radius,planet\radius
EntityColor planet\Entity,planet\red,planet\green,planet\blue
Next

End Function

;###############################################################################

Function StarImage()

Local i,img

img=CreateImage(GraphicsWidth(),GraphicsHeight())
SetBuffer ImageBuffer(img)
Color 255,255,255
For i=0 To 200
Plot Rand(GraphicsWidth()),Rand(GraphicsHeight())
Next
SetBuffer BackBuffer()

Return img

End Function

;###############################################################################

 

getlose

BeitragDo, Okt 28, 2004 12:48
Antworten mit Zitat
Benutzer-Profile anzeigen
hehe, sieht gut aus, dich hätte ich gerne mal als 'Mathe-Lehrer' ;D

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group