[B3D]Flaggen Simulator

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Foppele

Betreff: [B3D]Flaggen Simulator

BeitragDo, Dez 13, 2007 12:04
Antworten mit Zitat
Benutzer-Profile anzeigen
Ein Programm zum Erstellen von Flaggen Animationen.
Die importierte Flagge muss bereits die richtige Ausrichtung und Grösse haben, weil mir Tformpoint leider Stress machte. Die erzeugten Dims enthalten Skinning und Deformerinformation, wenn mann sie speichert kann man die Flagge in jedem anderen Programm benutzen ohne das ganze drumherum.
user posted image

Code: [AUSKLAPPEN]

Graphics3D 800,600,32,2
SetBuffer BackBuffer()
frametimer= CreateTimer(30)


light=CreateLight(1)
LightColor light,255,255,255
PositionEntity light,10,10,-10
RotateEntity light,10,0,0

AmbientLight 10,10,10


Global dummy=CreatePivot()
Global cam=CreateCamera(dummy)
PositionEntity cam,0,5,-20

RotateEntity cam,5,0,0
CameraClsColor cam,100,100,200

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

Type showVertexType
Field mesh                          
End Type
Global s.showVertexType
;----------------------------------------------------

Type cursorType
Field mesh                          
End Type
Global c.cursorType
;----------------------------------------------------

Type deformerType
Field mesh                          
End Type
Global d.deformerType
;----------------------------------------------------

Type markerType
Field mesh                          
End Type
Global m.markerType
;---------------------------------------------------

Type newflagType
Field mesh
Field pos[3]
Field scale
Field rgb[3]                         
End Type
Global f.newflagType
;---------------------------------------------------





auswahl =0
Global anzahl=20
While auswahl = 0
RenderWorld
If KeyHit(205) Then anzahl = anzahl + 1
Text 100,100,"Wie viele DeformationsPunkte maximal?"
Text 100,150, anzahl
Flip

If KeyHit(28) Then auswahl=1
If KeyHit(57) Then auswahl=1
Wend

;============================================================================================================================

Global flagge = LoadMesh("c:\_austausch\flagge.x")    ; Hier die Flagge importieren
PositionEntity flagge,0,0,0
EntityColor flagge,255,100,100
EntityShininess flagge,1
surface=GetSurface(flagge,1)
Vcount=CountVertices(surface)

;============================================================================================================================

Dim defList#(anzahl,3)         ;defList und vertexList können gespeichert und woanders weiterverwendet werden.

Dim vertexList#(Vcount,4)      ;auch "array" muss dort vorhanden sein, kann aber dort neu erstellt werden.

Dim array(anzahl)

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

Global defcount=0

Global windspeed#=10

Global effect#=2

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

setDeformer(flagge) ;wird zum erstellen der Dims benötigt. Wird nicht gebraucht wenn die Dims importiert werden.


killMarkers()

;==============================================================================================================================

While Not KeyHit(1)


   If KeyDown(30)  Then TurnEntity dummy,0,-2,0
   If KeyDown(32)  Then TurnEntity dummy,0,2,0
   If KeyDown(17)  Then TurnEntity dummy,-2,0,0
   If KeyDown(31)  Then TurnEntity dummy,2,0,0
   
   If KeyHit(203) Then windspeed = windspeed + 0.5
   If KeyHit(205) Then windspeed = windspeed - 0.5
   If KeyHit(200) Then effect = effect + 0.1
   If KeyHit(208) Then effect = effect - 0.1
   

randomDeformers(windspeed,effect) 

animateFlag(flagge)   


moreFlags()
   
   
   
   
   
   


   

UpdateWorld
RenderWorld

Text 10,10,"windspeed Pfeiltasten left/right"
Text 300,10,windspeed
Text 10,30,"effekt    Pfeiltasten up/down"
Text 300,30,effect
Text 10,50,"camera    w,a,s,d"
Text 10,70,"mehr Flaggen   n"
WaitTimer frametimer
Flip 0    


Wend

;===========================================================================================================================

; hier werden Kontrollpunkte erstellt und Vertices zugewiesen


Function setDeformer(mesh)

   c.cursorType = New cursorType
   c\mesh = CreateCube()
   EntityColor c\mesh,255,255,0
   ScaleEntity c\mesh,0.2,0.2,0.2
   
defCount=0   

Repeat

   If KeyDown(203) Then TranslateEntity c\mesh,-0.1,0,0
   If KeyDown(205) Then TranslateEntity c\mesh,0.1,0,0

   If KeyHit(57) Then

   d.deformerType = New deformerType
   d\mesh = CreateSphere(4)
   EntityColor d\mesh,255,255,0
   ScaleEntity d\mesh,0.2,0.2,0.2
   PositionEntity d\mesh, EntityX#(c\mesh),EntityY#(c\mesh),EntityZ#(c\mesh)

   defCount= defCount + 1

   defList(defCount,1) = EntityX#(c\mesh)
   defList(defCount,2) = EntityY#(c\mesh)
   defList(defCount,3) = EntityZ#(c\mesh)

   EndIf

   UpdateWorld
   RenderWorld
   
   Text 10,10, "Plaziere den ersten Kontrollpunkt VOR der Flagge und den zweiten an der Fahnenmastposition"
   Text 10,30, "Die Flagge muss sich an Z = 0 befinden. Wenn sie jetzt nicht zu dir zeigt, drehe sie in "
   Text 10,45, "deinem 3D Programm und exportiere neu. Um die Flagge zu bewegen/rotieren/skalieren, erst eine "
   Text 10,60, "Meshkopie erstellen und mit dieser arbeiten "
   Flip


Until KeyHit(28)


ScaleEntity c\mesh,0.1,10,0.1


Local percentonPath=0

Local CP1#[3]
Local CP2#[3]
Local CP3#[3]
Local WP1#[3]
Local WP2#[3]

Local VecCP1to2#[3]
Local VecCP2to3#[3]

Local VecWP1toCP2#[3]
Local VecCP2toWP2#[3]

Local VectoVec#[3]

Local nextPosition#[3]


CP1[1] = defList(1,1)
CP1[3] = defList(1,3)

CP2[1] = defList(2,1)
CP2[3] = defList(2,3)

CP3[1] = defList(3,1)
CP3[3] = defList(3,3)

Local nextDef=4



Repeat   
   
   

   If percentonPath=0 Then

   ; Berechne WP1 und WP2 aus CP1,2,3
   
      VecCP1to2[1] = CP2[1]-CP1[1]
      VecCP1to2[3] = CP2[3]-CP1[3]
   
   
      VecCP2to3[1] = CP3[1]-CP2[1]
      VecCP2to3[3] = CP3[3]-CP2[3]
   
   
      WP1[1] = CP1[1]+VecCP1to2[1]/2
      WP1[3] = CP1[3]+VecCP1to2[3]/2
   
   
      WP2[1] = CP2[1]+VecCP2to3[1]/2
      WP2[3] = CP2[3]+VecCP2to3[3]/2
   
   
   EndIf
   
   
   ; Berechne Hilfsvektoren
   
   VecWP1toCP2[1] = CP2[1] - WP1[1]
   VecWP1toCP2[3] = CP2[3] - WP1[3]
   
   VecCP2toWP2[1] = WP2[1] - CP2[1]
   VecCP2toWP2[3] = WP2[3] - CP2[3]
   
   
   
   
   ; Berechne neue Position
   
   

   
   VectoVec[1] = (CP2[1] + VecCP2toWP2[1]*percentonPath/100) - (WP1[1] + VecWP1toCP2[1]*percentonPath/100)   
   VectoVec[3] = (CP2[3] + VecCP2toWP2[3]*percentonPath/100) - (WP1[3] + VecWP1toCP2[3]*percentonPath/100)
   
   nextPosition[1] = WP1[1] + (VecWP1toCP2[1]*percentonPath/100) + (VectoVec[1]*percentonPath/100)
   nextPosition[3] = WP1[3] + (VecWP1toCP2[3]*percentonPath/100) + (VectoVec[3]*percentonPath/100)
   
   ; Bewege Cursor
   
   PositionEntity c\mesh,nextPosition[1],0,nextPosition[3]
   
   UpdateWorld
   RenderWorld
   Flip    
   
   ; Speichere Fortschritt
   
   percentonPath = percentonPath + 1
   
   
   ; Punkte zuweisen
   
   surface = GetSurface(mesh,1)
      
   For V=0 To CountVertices(surface)-1
      
      TFormPoint VertexX#(surface,V),VertexY#(surface,V),VertexZ#(surface,V),mesh,0
      
      If TFormedX#() < nextPosition[1]+0.1 And TFormedX#() > nextPosition[1]-0.1 Then
      
         vertexList(V,1) = nextDef-3
         vertexList(V,2) = nextDef-2
         vertexList(V,3) = nextDef-1
         vertexList(V,4) = percentonPath
      
         s.showVertexType = New showVertexType
         s\mesh = CreateSphere(4)
         EntityColor s\mesh,0,0,255
         ScaleEntity s\mesh,0.1,0.1,0.1
         PositionEntity s\mesh,TFormedX#(),TFormedY#(),TFormedZ#()
      
      EndIf
   
   Next
   
   
   ; Kontrollpunkte weiterreichen
   
   If percentonPath=101 Then
   
      CP1[1]=CP2[1]
      CP1[3]=CP2[3]
   
      CP2[1]=CP3[1]
      CP2[3]=CP3[3]
   
      CP3[1]=defList(nextDef,1)   
      CP3[3]=defList(nextDef,3)
   
      nextDef = nextDef + 1
   
      percentonPath = 0
   
   EndIf   
   

   
   
Until nextDef = defCount+2




End Function


;========================================================================================================================

Function killMarkers()
    
   For s.showVertexType = Each showVertexType
      
      FreeEntity s\mesh
      Delete s
   
   Next
   
   
   For c.cursorType = Each cursorType
   
      FreeEntity c\mesh
      Delete c
   
   Next
   
   
   For d.deformerType = Each deformerType
   
      FreeEntity d\mesh
      Delete d
   
   Next
   
End Function

;========================================================================================================================

Function animateFlag(mesh)

Local percentonPath=0

Local CP1#[3]
Local CP2#[3]
Local CP3#[3]
Local WP1#[3]
Local WP2#[3]

Local VecCP1to2#[3]
Local VecCP2to3#[3]

Local VecWP1toCP2#[3]
Local VecCP2toWP2#[3]

Local VectoVec#[3]

Local newPosition#[3]

surface = GetSurface(mesh,1)


For V=0 To CountVertices(surface)-1
   
   
   percentonPath = vertexList(V,4)
   
   
   CP1[1] = defList(vertexList(V,1),1)
   CP1[3] = defList(vertexList(V,1),3)

   CP2[1] = defList(vertexList(V,2),1)
   CP2[3] = defList(vertexList(V,2),3)

   CP3[1] = defList(vertexList(V,3),1)
   CP3[3] = defList(vertexList(V,3),3)
   
   
   VecCP1to2[1] = CP2[1]-CP1[1]
   VecCP1to2[3] = CP2[3]-CP1[3]
   
   
   VecCP2to3[1] = CP3[1]-CP2[1]
   VecCP2to3[3] = CP3[3]-CP2[3]
   
   
   WP1[1] = CP1[1]+VecCP1to2[1]/2
   WP1[3] = CP1[3]+VecCP1to2[3]/2
   
   
   WP2[1] = CP2[1]+VecCP2to3[1]/2
   WP2[3] = CP2[3]+VecCP2to3[3]/2
   
   
   VecWP1toCP2[1] = CP2[1] - WP1[1]
   VecWP1toCP2[3] = CP2[3] - WP1[3]
   
   
   VecCP2toWP2[1] = WP2[1] - CP2[1]
   VecCP2toWP2[3] = WP2[3] - CP2[3]
   
   
   VectoVec[1] = (CP2[1] + VecCP2toWP2[1]*percentonPath/100) - (WP1[1] + VecWP1toCP2[1]*percentonPath/100)   
   VectoVec[3] = (CP2[3] + VecCP2toWP2[3]*percentonPath/100) - (WP1[3] + VecWP1toCP2[3]*percentonPath/100)
   

   newPosition[1] = WP1[1] + (VecWP1toCP2[1]*percentonPath/100) + (VectoVec[1]*percentonPath/100)
   newPosition[3] = WP1[3] + (VecWP1toCP2[3]*percentonPath/100) + (VectoVec[3]*percentonPath/100)
   
   
   
   
   VertexCoords surface,V,VertexX#(surface,V),VertexY#(surface,V),newPosition[3]
           

Next
   


End Function


;=================================================================================================================================

Function randomDeformers(windspeed#,effect#)




If array(1)=0 And array(2)=0 And array(3)=0 Then

   For x=1 To anzahl
   
       array(x) = Rnd(1,360)

   Next

EndIf


   For x=3 To 20
   
      array(1) = array(1)+windspeed/2
      array(1) = array(2)+windspeed/4
      array(x) = array(x)+windspeed
      If array(x) > 361 Then array(x) = 1
       defList(x,3)=(effect * Sin#(array(x))) + (0.3 * effect * Sin#(array(1))) + (0.16 * effect * Sin#(array(2)))

   Next
   

   

End Function


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


Function moreFlags()

For n.newFlagType = Each newflagType
   
   FreeEntity n\mesh
   n\mesh = CopyMesh(flagge)
   PositionEntity n\mesh,n\pos[1],n\pos[2],n\pos[3]
    ScaleEntity n\mesh,n\scale,n\scale,n\scale
   EntityColor n\mesh,n\rgb[1],n\rgb[2],n\rgb[3]
   EntityShininess n\mesh,1
   
Next

If KeyHit(49) Then
   n.newFlagType = New newflagType
   n\mesh = CopyMesh(flagge)
   n\pos[1] = Rnd#(-20,20)
   n\pos[2] = Rnd#(-20,20)
   n\pos[3] = Rnd#(-10,30)
   n\scale  = Rnd#(0.3,1)
   n\rgb[1] = Rnd#(0,255)
   n\rgb[2] = Rnd#(0,255)
   n\rgb[3] = Rnd#(0,255)

   PositionEntity n\mesh,n\pos[1],n\pos[2],n\pos[3]
   ScaleEntity n\mesh,n\scale,n\scale,n\scale
   EntityColor n\mesh,n\rgb[1],n\rgb[2],n\rgb[3]
   EntityShininess n\mesh,1
   
EndIf




End Function


[Edit] Jetzt mit Flaggenvervielfältigungsmethode
  • Zuletzt bearbeitet von Foppele am Do, Dez 13, 2007 15:16, insgesamt 2-mal bearbeitet

Dottakopf

BeitragDo, Dez 13, 2007 13:35
Antworten mit Zitat
Benutzer-Profile anzeigen
kannst du vll ne "testflagge" hochladen ?
hab grad keine "flagge.x" da .... Rolling Eyes


mfg Dottakopf
Rechtschreibfehler gelten der allgemeinen Belustigung!

Foppele

BeitragDo, Dez 13, 2007 13:40
Antworten mit Zitat
Benutzer-Profile anzeigen
http://rapidshare.de/files/38038533/flagge.x.html

Bitteschön!

Dottakopf

BeitragDo, Dez 13, 2007 13:53
Antworten mit Zitat
Benutzer-Profile anzeigen
ich finds echt toll !
sehr schön. aber das "einscannen" der flagge wird bei meinem altem rechner doch sehr langsam, aber trotzdem erträglich. Muss noch n bisschen rumspielen mit dem punkte setzten, bei mir schneidets dann immer vorne die flaggenspitze weg.

Du solltest vll noch ne kleine anleitung hinzufügen.Wie speichere ich dan die ani ab ?

mfg Dottakopf
Rechtschreibfehler gelten der allgemeinen Belustigung!

Foppele

BeitragDo, Dez 13, 2007 14:19
Antworten mit Zitat
Benutzer-Profile anzeigen
Tja, zum abspeichern musst du dir leider selber was einfallen lassen, da hab ich noch null Erfahrung mit.
Die Animation brauchst du eh nicht zu speichern, die wird durch randomDeformers() erzeugt.
Du musst halt nach dem einscannen die beiden Dims vertexList und deformerList abspeichern, in denen ist die Information enthalten welcher Vertex von welchem Deformer beeinflusst wird.

In dem Spiel in das du die Flagge packen willst müssen die drei Dims und die Funktionen randomDeformers() und animateFlag() enthalten sein, setDeformers() brauchst du dann nicht mehr.

Ausserdem darfst du die Flagge nicht bewegen,skalieren oder rotieren, weil ich das mit Tformpoint irgendwie nicht hingekriegt habe.
Aber du kannst sie einfach unsichtbar machen, und dann per CopyMesh vervielfältigen und platzieren.

Es ist sicher nicht die schönste Lösung, aber ich wollt jetzt auch nicht Ewigkeiten dran rumfrickeln weil ich eigentlich grade was anderes mache.

[Edit] Das Einscannen muss man ja nur einmal machen, wenns trotzdem schneller gehen soll muss man nur einen Wert in der Funktion ändern, das was auf percentonPath in jedem Durchgang addiert wird. Könnte allerdings ungenauer werden.
[Edit2] Blödsinn hab ich da geschrieben, das ist nur so langsam weil immer gerendert wird. Kickt das raus und es geht in ner sekunde.

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group