B3D like Entitysystem - Parent/Child Rotationsproblem

Übersicht BlitzMax, BlitzMax NG Allgemein

Neue Antwort erstellen

Moep

Betreff: B3D like Entitysystem - Parent/Child Rotationsproblem

BeitragSo, März 23, 2008 22:41
Antworten mit Zitat
Benutzer-Profile anzeigen
Moinsen,

Ich bin dabei ein 2D Entitysystem zu machen mit Parents und Childs.

Hier mal ein Auszug aus der Updatemethode:

Code: [AUSKLAPPEN]
If parent <> Null
       px = parent.x
       py = parent.y
       pr = parent.rotation
         
       winkel = GetWinkel(px, py, x, y)
       dis# = Distance(px, py, x, y)
       x = px + dis * Cos(pr)
       y = py + dis * Sin(pr)          
End If


GetWinkel gibt den Winkel zwischen 2 Punkten zurück und was Distance macht kann man sich denk ich mal denken ^^.

Die x und y Werte am Ende sind die vom Child.

So wie's oben steht klappts..es wird nur die Anfangsposition vom Child nicht berücksichtigt.
Und das is grad mein Problem.
Als Lösung würde mir jetzt nur einfallen, dass man den Anfangswinkel speichert und dann dort mit einbaut.
Aber es wird doch wohl auch eine elegantere Lösung geben oder?

Ich hoffe ich habs verständliche erklärt und danke schon mal vorraus.
Hardstyle Schleichwerbung:
http://www.youtube.com/user/Hackepeter42

Achtung: Suchtgefahr!
moep123.ohost.de

BtbN

BeitragSo, März 23, 2008 22:49
Antworten mit Zitat
Benutzer-Profile anzeigen
Du könntest direkt auf OpenGL zugreifen, und dort per Push/PopMatrix ein parent/child system implementieren.

Moep

BeitragSo, März 23, 2008 22:58
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke für die fixe antwort.

Ich werd mir mal die beiden Befehle angucken.

Aber ich glaube nicht, dass meine opengl Erfahrung dazu ausreicht^^.
Hardstyle Schleichwerbung:
http://www.youtube.com/user/Hackepeter42

Achtung: Suchtgefahr!
moep123.ohost.de

BtbN

BeitragSo, März 23, 2008 23:02
Antworten mit Zitat
Benutzer-Profile anzeigen
Das ganze läuft im Prinzip so ab: Du erstellst deine initiale matrix(sie wird hier von Max2D gegeben).
Dann pusht du sie auf den Stack. Dann modifizierst du sie, so dass dein Objekt so midifziert ist, wie du es willst.
Dann das selbe für alle Childs und Child Childs.
Dann wird die alte matrix wieder per pop zurück geholt. Und so geht das für jedes Objekt.
Allerdings weiss ich nicht, ob Max2D das mit sich machen lässt.

Moep

BeitragSo, März 23, 2008 23:16
Antworten mit Zitat
Benutzer-Profile anzeigen
Also wenn ich das richtig verstanden hab, was anscheinend nicht der Fall ist xD, dann sollte der folgende Code ein um 45° gedrehtes Quadrat zeichnen bei der Position 400|300 und ein weiteres,kleineres Quadrat bei 0|0 und ungedreht.

Code: [AUSKLAPPEN]
While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
   
   glPushMatrix()
   
   SetOrigin 400, 300
   SetRotation 45
   DrawRect 0, 0, 100, 100

   glPopMatrix()
   DrawRect 0, 0, 50, 50
   

   Flip
   Cls
Wend
Hardstyle Schleichwerbung:
http://www.youtube.com/user/Hackepeter42

Achtung: Suchtgefahr!
moep123.ohost.de

BtbN

BeitragSo, März 23, 2008 23:28
Antworten mit Zitat
Benutzer-Profile anzeigen
hm, das scheint mit Max2D in kombination nicht zu klappen, da das ganze Intern bereits geregelt ist, dass du von aussen nurnoch minimal einfluss nehmen kannst.

Moep

BeitragSo, März 23, 2008 23:37
Antworten mit Zitat
Benutzer-Profile anzeigen
Hmm, mist, sonst noch jemand ne Idee wie man das ganze Regeln könnte?
Hardstyle Schleichwerbung:
http://www.youtube.com/user/Hackepeter42

Achtung: Suchtgefahr!
moep123.ohost.de
 

klepto2

BeitragMo, März 24, 2008 0:43
Antworten mit Zitat
Benutzer-Profile anzeigen
Du könntest auch manuell mit Matrizen arbeiten.
Im Prinzip wie BtbN es beschrieben hat, nur halt mit einem eigenem Matrix Typ. Die Positionen etc müsstest du dann wieder aus der Matrix auslesen. Aber der Effekt wäre derselbe.

Parent = InitialMatrix
1 Child = Parent Matrix + eigene Matrix
2 Child das selbe
Child von Child 2 = Parent Matrix + Matrix von Child 2 + eigene Matrix

das ganze dann halt immer so weiter.
Matrix Screensaver
Console Modul für BlitzMax
KLPacker Modul für BlitzMax

HomePage : http://www.brsoftware.de.vu

Moep

BeitragMo, März 24, 2008 13:25
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich glaube ich sollte mir vllcht erstmal angucken wie sone Matrix überhaupt funktioniert, bevor ich da mein Entitysystem weiter mache xD...hilft ja sonst alles nix.

Danke für eure Hilfe..

PS: in der 10. Klasse hat man sowas leider noch nich^^

Edit : Was gehört den in eine 2D Matrix rein? X,Y,W,H,rotation?

mfg
Hardstyle Schleichwerbung:
http://www.youtube.com/user/Hackepeter42

Achtung: Suchtgefahr!
moep123.ohost.de
 

Dreamora

BeitragMo, März 24, 2008 14:24
Antworten mit Zitat
Benutzer-Profile anzeigen
nichts davon steht in der Matrix zumindest nicht in der Form.

Eine NxM Matrix ist eine Projektion die einen Vektor der Dimension M auf die Dimension N abbildet.
In so einer Matrix enthalten sind Position, Rotation und Skalierung des Vektors. Jedoch nicht in einer Form (von der Position abgesehen wenn man zb in 2D eine 2x3, so das man in einer zusätzlichen Spalte die Position hat. Aber Rotation und Skalierung sind immer zusammen im gleichen 2x2 Block)
Ihr findet die aktuellen Projekte unter Gayasoft und könnt mich unter @gayasoft auf Twitter erreichen.

Markus2

BeitragDo, März 27, 2008 19:32
Antworten mit Zitat
Benutzer-Profile anzeigen
Zum abgucken ohne Garantie Smile

Braucht ne Textur parkett.jpg

Wird mit den Tasten 1-6 gedreht

Main.bmx
Code: [AUSKLAPPEN]

SuperStrict

Include "MR_Vector.bmx"

Const GLU_POINT:Int      = 100010
Const GLU_LINE:Int      = 100011
Const GLU_FILL:Int      = 100012
Const GLU_SILHOUETTE:Int= 100013

Global glContext:TGraphics=myGraphics(800,600,0,72)
'Global glContext:TGraphics=myGraphics(320,240,0,72)
'Global glContext:TGraphics=myGraphics(800,600,16,72) 'Full Screen

'########################################################################################

Type TObject

 Field Pos:TV3D
 Field Surfaces:TList
 Field ChildObjects:TList
 Field AX:TV3D
 Field AY:TV3D
 Field AZ:TV3D

 Function Create:TObject(Pos:TV3D)
  Local O:TObject=New TObject

  O.Surfaces=CreateList()
  O.ChildObjects=CreateList()

  O.Pos=Pos.Copy()
  O.AX=TV3D.Create( 1, 0, 0)
  O.AY=TV3D.Create( 0, 1, 0)
  O.AZ=TV3D.Create( 0, 0, 1)

  Return O
 End Function

 Method AddChild(O:TObject)
  ChildObjects.AddLast O
 End Method

 Method AddSurface:TSurface(Name:String="NoName")
  Local S:TSurface=TSurface.Create(Name)
  Surfaces.AddLast S
  Return S
 End Method

 Method Turn(TurnPos:TV3D=Null,Achse:TV3D,Winkel:Double,Childs:Int=True)

    Local M:TMatrix=TMatrix.Create()

    '---------------------------------------
    'Pos drehen
    If TurnPos=Null Then
     TurnPos=Pos.Copy()
    EndIf

    Pos=VSUB(Pos,TurnPos)
    MatrixCreateAxisRotate(Achse,Winkel,M)
    Pos=VTrans(Pos,M)
    Pos=VADD(Pos,TurnPos)

    '---------------------------------------
    'Achsen drehen
    MatrixCreateAxisRotate(Achse,Winkel,M)   
    AX=VTrans(AX,M)
    AY=VTrans(AY,M)
    AZ=VTrans(AZ,M)
   AchsenKorrektur
    '---------------------------------------

    '---------------------------------------
    'Punkte drehen
    Local S:TSurface
    Local T:TTriangle
    Local Q:TQuad
    Local P:TVertex
    Local V:TVerbinder
    For S=EachIn Surfaces
     For P=EachIn S.Punkte
      P.Turn TurnPos,Achse,Winkel
     Next
     For T=EachIn S.Triangles
      T.Normal=VNORMAL(T.P1.Pos,T.P2.Pos,T.P3.Pos)
     Next
     For Q=EachIn S.Quads
      Q.Normal=VNORMAL(Q.P1.Pos,Q.P2.Pos,Q.P3.Pos)
     Next
     For V=EachIn S.Verbinder 'Oririnal längen der Verbinder drehen
      V.L=VTrans(V.L,M)
     Next
    Next

    If Childs Then
     Local O:TObject
     For O=EachIn ChildObjects
      O.Turn TurnPos,Achse,Winkel,Childs
     Next
    EndIf

 End Method

 Method Move(Achse:TV3D,Entfernung:Double,Childs:Int=True)

  'Entlang der Achse Objekt und Punkte verschieben

    Local E:TV3D=TV3D.Create(Entfernung,Entfernung,Entfernung)
    Local D:TV3D=VMUL(Achse,E)

    Pos=VADD(Pos,D)

    Local S:TSurface
    Local T:TTriangle
    Local P:TVertex
    For S=EachIn Surfaces
     'DebugLog "Surface:" + S.Name
     For P=EachIn S.Punkte   
      P.Pos=VADD(P.Pos,D)
     Next
    Next

    If Childs Then
     Local O:TObject
     For O=EachIn ChildObjects
      O.Move Achse,Entfernung,Childs
     Next
    EndIf

 End Method

 Method Shake(Childs:Int=True)

    Local S:TSurface
    Local P:TVertex
    For S=EachIn Surfaces
     For P=EachIn S.Punkte
      P.Shake
     Next
    Next

    If Childs Then
     Local O:TObject
     For O=EachIn ChildObjects
      O.Shake Childs
     Next
    EndIf

 End Method

 Method Render(Childs:Int=True)

    Local S:TSurface
    For S=EachIn Surfaces
     S.Render
    Next

    If Childs Then
     Local O:TObject
     For O=EachIn ChildObjects
      O.Render Childs
     Next
    EndIf

 End Method

 Method RenderAchse(Childs:Int=True)

  glColor3d( 1.0, 0.0, 0.0 ) 'Rot
  glLine Pos,VADD(Pos,VMUL(AX,TV3D.Create(10,10,10)))
  glColor3d( 0.0, 1.0, 0.0 ) 'Grün
  glLine Pos,VADD(Pos,VMUL(AY,TV3D.Create(10,10,10)))
  glColor3d( 0.0, 0.0, 1.0 ) 'Blau
  glLine Pos,VADD(Pos,VMUL(AZ,TV3D.Create(10,10,10)))

   If Childs Then
    Local O:TObject
    For O=EachIn ChildObjects
     O.RenderAchse Childs
    Next
   EndIf

 End Method

 Method AchsenKorrektur()

  AX = VNORMALIZE(AX)
  AY = VCROSS(AZ,AX)
  AY = VNORMALIZE(AY)
  AZ = VCROSS(AX,AY)
  AZ = VNORMALIZE(AZ)

 End Method

End Type

'########################################################################################

Type TSurface

 Field Triangles:TList
 Field Quads:TList
 Field Verbinder:TList
 Field Name:String
 Field Punkte:TList

 Function Create:TSurface(Name:String="NoName")
  Local S:TSurface=New TSurface
  S.Triangles=CreateList()
  S.Quads=CreateList()
  S.Verbinder=CreateList()
  S.Punkte=CreateList()
  S.Name=Name 
  Return S
 End Function

 Method AddTriangle(Tri:TTriangle)
  Triangles.AddLast Tri
 End Method

 Method AddQuad(Quad:TQuad)
  Quads.AddLast Quad
 End Method

 Method AddCube(Size:TV3D,TxHandle:Int=0)

  Local p1:TVertex
  Local p2:TVertex
  Local p3:TVertex
  Local p4:TVertex

  p1=TVertex.Create(TV3D.Create(-10, 10, -10),TV3D.Create( 0,0,0))
  p2=TVertex.Create(TV3D.Create( 10, 10, -10),TV3D.Create( 1,0,0))
  p3=TVertex.Create(TV3D.Create( 10,-10, -10),TV3D.Create( 1,1,0))
  p4=TVertex.Create(TV3D.Create(-10,-10, -10),TV3D.Create( 0,1,0))

  AddTriangle TTriangle.Create(p1,p2,p3,TxHandle)
  AddTriangle TTriangle.Create(p3,p4,p1,TxHandle)

  Verbinder.AddLast TVerbinder.Create(p1,p2)
  Verbinder.AddLast TVerbinder.Create(p2,p3)
  Verbinder.AddLast TVerbinder.Create(p3,p4)
  Verbinder.AddLast TVerbinder.Create(p4,p1)

  'Verbinder.AddLast TVerbinder.Create(p1,p3) 'Diagonal

  Punkte.AddLast p1
  Punkte.AddLast p2
  Punkte.AddLast p3
  Punkte.AddLast p4

  '----------------------------------------------------------------

  Local p5:TVertex
  Local p6:TVertex
  Local p7:TVertex
  Local p8:TVertex

  p8=TVertex.Create(TV3D.Create(-10, 10, 10),TV3D.Create( 0,0,0))
  p7=TVertex.Create(TV3D.Create( 10, 10, 10),TV3D.Create( 1,0,0))
  p6=TVertex.Create(TV3D.Create( 10,-10, 10),TV3D.Create( 1,1,0))
  p5=TVertex.Create(TV3D.Create(-10,-10, 10),TV3D.Create( 0,1,0))

  AddTriangle TTriangle.Create(p5,p6,p7,TxHandle)
  AddTriangle TTriangle.Create(p7,p8,p5,TxHandle)

  Verbinder.AddLast TVerbinder.Create(p5,p6)
  Verbinder.AddLast TVerbinder.Create(p6,p7)
  Verbinder.AddLast TVerbinder.Create(p7,p8)
  Verbinder.AddLast TVerbinder.Create(p8,p5)

  'Verbinder.AddLast TVerbinder.Create(p5,p7) 'Diagonal

  Punkte.AddLast p5 '1
  Punkte.AddLast p6 '2
  Punkte.AddLast p7 '3
  Punkte.AddLast p8 '4

  '----------------------------------------------------------------
  'von fläche zu fläche
  Verbinder.AddLast TVerbinder.Create(p1,p8)
  Verbinder.AddLast TVerbinder.Create(p2,p7)
  Verbinder.AddLast TVerbinder.Create(p3,p6)
  Verbinder.AddLast TVerbinder.Create(p4,p5)

  'hmmm ...

  'Diagonal
  'Verbinder.AddLast TVerbinder.Create(p1,p5)
  'Verbinder.AddLast TVerbinder.Create(p3,p7)

 End Method

 Method AddCube2(Size:TV3D,TxHandle:Int=0)

  '... naja das ist noch Müll ...

  Local Vertex1:TVertex
  Local Vertex2:TVertex
  Local Vertex3:TVertex
  Local Vertex4:TVertex

  Local Vertex5:TVertex
  Local Vertex6:TVertex
  Local Vertex7:TVertex
  Local Vertex8:TVertex

  Local x:Double,y:Double,z:Double

  x=Size.x/2.0
  y=Size.y/2.0
  z=Size.z/2.0

   'Front

   Vertex1=TVertex.Create(TV3D.Create(-x,  y, z),TV3D.Create( 0.0, 0.0,0))
   Vertex2=TVertex.Create(TV3D.Create( x,  y, z),TV3D.Create( 1.0, 0.0,0))
   Vertex3=TVertex.Create(TV3D.Create( x, -y, z),TV3D.Create( 1.0, 1.0,0))
   Vertex4=TVertex.Create(TV3D.Create(-x, -y, z),TV3D.Create( 0.0, 1.0,0))

    AddQuad TQuad.Create(Vertex1,Vertex2,Vertex3,Vertex4,TxHandle)

   'Back

   Vertex1=TVertex.Create(TV3D.Create( x,  y, -z),TV3D.Create( 0.0, 0.0,0))
   Vertex2=TVertex.Create(TV3D.Create(-x,  y, -z),TV3D.Create( 1.0, 0.0,0))
   Vertex3=TVertex.Create(TV3D.Create(-x, -y, -z),TV3D.Create( 1.0, 1.0,0))
   Vertex4=TVertex.Create(TV3D.Create( x, -y, -z),TV3D.Create( 0.0, 1.0,0))

    AddQuad TQuad.Create(Vertex1,Vertex2,Vertex3,Vertex4,TxHandle)
      
   'Top

   Vertex1=TVertex.Create(TV3D.Create(-x, y, -z),TV3D.Create( 0.0, 0.0,0))
   Vertex2=TVertex.Create(TV3D.Create( x, y, -z),TV3D.Create( 1.0, 0.0,0))
   Vertex3=TVertex.Create(TV3D.Create( x, y,  z),TV3D.Create( 1.0, 1.0,0))
   Vertex4=TVertex.Create(TV3D.Create(-x, y,  z),TV3D.Create( 0.0, 1.0,0))

    AddQuad TQuad.Create(Vertex1,Vertex2,Vertex3,Vertex4,TxHandle)

   'Bottom

   Vertex1=TVertex.Create(TV3D.Create(-x, -y,  z),TV3D.Create( 0.0, 0.0,0))
   Vertex2=TVertex.Create(TV3D.Create( x, -y,  z),TV3D.Create( 1.0, 0.0,0))
   Vertex3=TVertex.Create(TV3D.Create( x, -y, -z),TV3D.Create( 1.0, 1.0,0))
   Vertex4=TVertex.Create(TV3D.Create(-x, -y, -z),TV3D.Create( 0.0, 1.0,0))

    AddQuad TQuad.Create(Vertex1,Vertex2,Vertex3,Vertex4,TxHandle)

   'Left

   Vertex1=TVertex.Create(TV3D.Create(-x,  y, -z),TV3D.Create( 0.0, 0.0,0))
   Vertex2=TVertex.Create(TV3D.Create(-x,  y,  z),TV3D.Create( 1.0, 0.0,0))
   Vertex3=TVertex.Create(TV3D.Create(-x, -y,  z),TV3D.Create( 1.0, 1.0,0))
   Vertex4=TVertex.Create(TV3D.Create(-x, -y, -z),TV3D.Create( 0.0, 1.0,0))

    AddQuad TQuad.Create(Vertex1,Vertex2,Vertex3,Vertex4,TxHandle)
      
   'Right

   Vertex1=TVertex.Create(TV3D.Create(x,  y,  z),TV3D.Create( 0.0, 0.0,0))
   Vertex2=TVertex.Create(TV3D.Create(x,  y, -z),TV3D.Create( 1.0, 0.0,0))
   Vertex3=TVertex.Create(TV3D.Create(x, -y, -z),TV3D.Create( 1.0, 1.0,0))
   Vertex4=TVertex.Create(TV3D.Create(x, -y,  z),TV3D.Create( 0.0, 1.0,0))

    AddQuad TQuad.Create(Vertex1,Vertex2,Vertex3,Vertex4,TxHandle)

    'Punkte merken
    Punkte.AddLast Vertex1 'Oben 1-4 im Uhrzeigersinn
    Punkte.AddLast Vertex2
    Punkte.AddLast Vertex3
    Punkte.AddLast Vertex4
    Punkte.AddLast Vertex5 'Unten 1-4
    Punkte.AddLast Vertex6
    Punkte.AddLast Vertex7
    Punkte.AddLast Vertex8

 End Method

 Method Render()

  Local Tri:TTriangle
  For Tri=EachIn Triangles
   Tri.Render
   RenderNormal Tri.P1.Pos,Tri.Normal
  Next

  Local Quad:TQuad
  For Quad=EachIn Quads
   Quad.Render
   RenderNormal Quad.P1.Pos,Quad.Normal
  Next

  Boing
  Physik   

 End Method

 Method RenderNormal(Pos:TV3D,N:TV3D)

  glColor3d( 1.0, 1.0, 0.0 ) 'Gelb
  glLine Pos,VADD(Pos,VMUL(N,TV3D.Create(10,10,10)))

 End Method

 Method Boing()

  Local V:TVerbinder
  For V=EachIn Verbinder
   V.Boing
  Next

    'Oberflächen Normale neu berechnen
    Local T:TTriangle
    Local Q:TQuad
    For T=EachIn Triangles
     T.Normal=VNORMAL(T.P1.Pos,T.P2.Pos,T.P3.Pos)
    Next
    For Q=EachIn Quads
     Q.Normal=VNORMAL(Q.P1.Pos,Q.P2.Pos,Q.P3.Pos)
    Next

 End Method

 Method Physik()

  Local P:TVertex
  For P=EachIn Punkte
   P.Physik
  Next

 End Method

End Type

'########################################################################################

Type TVerbinder
 Field P1:TVertex
 Field P2:TVertex
 Field L:TV3D

 Function Create:TVerbinder(P1:TVertex,P2:TVertex)
  Local V:TVerbinder=New TVerbinder
  V.P1=P1
  V.P2=P2

  V.L=TV3D.Create()
  'halbe Original Entfernung merken bzw. Richtung
  V.L.x=(P2.Pos.x - P1.Pos.x)/2.0
  V.L.y=(P2.Pos.y - P1.Pos.y)/2.0
  V.L.z=(P2.Pos.z - P1.Pos.z)/2.0

  Return V
 End Function

 Method Boing()

  Local ln:TV3D=New TV3D

  'Die Punkte sollen wieder auf den Abstand kommen
  'welcher durch die Länge vorgeben wurde (P1-P2)

  'neue halbe Strecke zwischen den Punkten
  ln.x=(P2.Pos.x - P1.Pos.x)/2.0
  ln.y=(P2.Pos.y - P1.Pos.y)/2.0
  ln.z=(P2.Pos.z - P1.Pos.z)/2.0

  'vom Original abziehen , das man das was zuviel ist bekommt
  ln.x=l.x - ln.x
  ln.y=l.y - ln.y
  ln.z=l.z - ln.z

  'bischen teilen das es nicht sofort zurück springt
  ln.x=ln.x/12.0
  ln.y=ln.y/12.0
  ln.z=ln.z/12.0

  'Der Dehnungskraft entgegen wirken
  If P1.Fest=0 Then
   P1.Speed.x=P1.Speed.x-ln.x
   P1.Speed.y=P1.Speed.y-ln.y
   P1.Speed.z=P1.Speed.z-ln.z
  EndIf

  If P2.Fest=0 Then
   P2.Speed.x=P2.Speed.x+ln.x
   P2.Speed.y=P2.Speed.y+ln.y
   P2.Speed.z=P2.Speed.z+ln.z
  EndIf

  ln=Null

  glColor3d( 1.0, 1.0, 1.0 ) 'Weiß
  glLine P1.Pos,P2.Pos

 End Method

End Type

'########################################################################################

Type TQuad
 Field P1:TVertex
 Field P2:TVertex
 Field P3:TVertex
 Field P4:TVertex
 Field Normal:TV3D 'nx,ny,nz
 Field TextureHandle:Int 'Texture Handle

 Function Create:TQuad(P1:TVertex,P2:TVertex,P3:TVertex,P4:TVertex,TextureHandle:Int=0)
  Local T:TQuad=New TQuad
  T.P1=P1'.Copy()
  T.P2=P2'.Copy()
  T.P3=P3'.Copy()
  T.P4=P4'.Copy()
  T.Normal=VNORMAL(T.P1.Pos,T.P2.Pos,T.P3.Pos)
  T.TextureHandle=TextureHandle
  Return T
 End Function

 Method Render()

  glEnable(GL_BLEND)
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) '!

  BeginRenderTexture TextureHandle

  'glBegin GL_TRIANGLES
  glBegin GL_QUADS
  'glBegin GL_POINTS
  'glBegin GL_TRIANGLES

  glNormal3d Normal.x, Normal.y,Normal.z

  Material()

  p1.Render
  p2.Render
  p3.Render
  p4.Render

  glEnd

  EndRenderTexture TextureHandle

 End Method

 Function Free(T:TQuad Var)
  TVertex.Free T.P1
  TVertex.Free T.P2
  TVertex.Free T.P3
  TVertex.Free T.P4
  T.Normal=Null
  T=Null
 End Function

 Method SetNormal(N:TV3D)
  Normal=N.Copy()
 End Method

End Type

'########################################################################################

Type TTriangle
 Field P1:TVertex
 Field P2:TVertex
 Field P3:TVertex
 Field Normal:TV3D 'nx,ny,nz
 Field TextureHandle:Int 'Texture Handle

 Function Create:TTriangle(P1:TVertex,P2:TVertex,P3:TVertex,TextureHandle:Int=0)
  Local T:TTriangle=New TTriangle
  T.P1=P1'.Copy()
  T.P2=P2'.Copy()
  T.P3=P3'.Copy()
  T.Normal=VNORMAL(T.P1.Pos,T.P2.Pos,T.P3.Pos)
  T.TextureHandle=TextureHandle
  Return T
 End Function

 Method Render()

  glEnable(GL_BLEND)
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) '!

  BeginRenderTexture TextureHandle

  glBegin GL_TRIANGLES
  'glBegin GL_QUADS
  'glBegin GL_POINTS
  'glBegin GL_TRIANGLES

  glNormal3d Normal.x, Normal.y,Normal.z

  Material()

  p1.Render
  p2.Render
  p3.Render

  glEnd

  EndRenderTexture TextureHandle

  'Local O:TV3D=TV3D.Create(0,1,0)

  'Local n:TV3D

  'n=VNORMAL(p1.Pos,p2.Pos,p3.Pos)

  'n.x=n.x-0
  'n.y=n.y-1
  'n.z=n.z-0

  'n=VMUL(n,TV3D.Create(10,10,10)) 'vergrößern x 10
  'n=VADD(p1.Pos,n) 'da wo a1 ist hin schieben

  'VCROSS(a1,o,a2)
  'VCROSS(b1,o,b2)
  'VCROSS(c1,o,c2)
  'VCROSS(d1,o,d2)

  'glColor3d( 0.0, 1.0, 1.0 ) 'cyan

  'glLine p1.Pos,n

  'glLine a1,b1
  'glLine b1,c1
  'glLine c1,d1
  'glLine d1,a1

  'glLine a2,b2
  'glLine b2,c2
  'glLine c2,d2
  'glLine d2,a2

 End Method

 Function Free(T:TTriangle Var)
  TVertex.Free T.P1
  TVertex.Free T.P2
  TVertex.Free T.P3
  T.Normal=Null
  T=Null
 End Function

 Method SetNormal(N:TV3D)
  Normal=N.Copy()
 End Method

End Type

'########################################################################################

Type TVertex

 Field Pos:TV3D
 Field Farbe:TV3D 'x y z=r g b
 Field Tex:TV3D 'Texture x y z=u v w
 Field a:Double 'Alpha

 Field Speed:TV3D
 Field Fest:Int
 Field Radius:Double

 Function Create:TVertex(Pos:TV3D,UVW:TV3D)
  'DebugLog "Create Vertex"
  Local v:TVertex=New TVertex

  v.Pos=Pos.Copy()
  v.Farbe=TV3D.Create(0,0,0) 'r,g,b
  v.Tex=UVW.Copy()
  v.a=1 'Alpha 1=Visible to 0=Hidden

  v.Fest=0
  v.Speed=TV3D.Create(0,0,0) 'r,g,b
  v.Radius=0

  Return v
 End Function

 Method Shake()
  Speed.x=Speed.x+Rand(-2,2)
  Speed.y=Speed.y+Rand(-2,2)
  Speed.z=Speed.z+Rand(-2,2)
 End Method

 Method Copy:TVertex()

  Local v:TVertex=New TVertex

  v.Pos=Pos.Copy()
  v.Farbe=Farbe.Copy()
  v.Tex=Tex.Copy()
  v.a=a

  v.Speed=Speed.Copy()
  v.Fest=Fest
  v.Radius=Radius

  Return v

 End Method

 Function Free(V:TVertex Var)
  V.Pos=Null
  V.Tex=Null
  V.Farbe=Null 
  V.Speed=Null
  V=Null
 End Function

 Method Turn(ObjektPos:TV3D,Achse:TV3D,Winkel:Double)
    Local M:TMatrix=TMatrix.Create()
    Pos=VSUB(Pos,ObjektPos)
    MatrixCreateAxisRotate(Achse,Winkel,M)
    Pos=VTrans(Pos,M)
    Pos=VADD(Pos,ObjektPos)
 End Method

 Method VertexColor(red:Double,green:Double,blue:Double,alpha:Double=1.0)
  'You can see Vertex Color without Lights
  Farbe.Set red,green,blue
  a=alpha
 End Method

 Method VertexTexCoords(tu:Double=0,tv:Double=0,tw:Double=0)
  Tex.Set tu,tv,tw
 End Method

 Method Render()
      glTexCoord2d Tex.x,Tex.y
      'glTexCoord3f Normal.x,Normal.y,Normal.z '???
        glColor4d Farbe.x,Farbe.y,Farbe.z,a 'wenn kein Licht benutzt wird geht das mit Alpha !?
        'glColor3f Farbe.x,Farbe.y,Farbe.z
      glVertex3d Pos.x,Pos.y,Pos.z
 End Method

 Method Physik:Int()

  'Return 0 '!!! AUS

  '! Alle Punkte müssen beim erstellen Oberhalb der Erde (Height) sein und Innerhalb der Breite

  Local Gravity:Int=1
  Local Width:Double=50
  Local Height:Double=0

         If Fest=0 Then
          If Gravity=1 Then
             '... besser das Objekt nach unten bewegen
          'Speed.y = Speed.y - 0.02 'Gravitation bzw. nach unten beschleunigen
            EndIf
      
          'Bewegen wie errechnet
          Pos=VADD(Pos,Speed)
         EndIf
      
         'In einer Box halten
         If Pos.x < -Width Then Pos.x = -Width ; Speed.x = -Speed.x * 0.97
         If Pos.x >  Width Then Pos.x =  Width ; Speed.x = -Speed.x * 0.97 

         If Pos.z < -Width Then Pos.z = -Width ; Speed.z = -Speed.z * 0.97
         If Pos.z >  Width Then Pos.z =  Width ; Speed.z = -Speed.z * 0.97 

         If Pos.y - Radius < Height Then Pos.y = Height+Radius ; Speed.y = -Speed.y * 0.97 ; Speed.x = Speed.x / 2.0 'Speed.x * 0.97

            'sowas wie Reibung sonnst bewegt es sich endlos ...
         Speed.x = Speed.x * 0.92 '99
         Speed.y = Speed.y * 0.92 '99
         Speed.z = Speed.z * 0.92 '99
 End Method

End Type

'########################################################################################

MakeLight()

MainLoop()
End

'########################################################################################

Function MainLoop()

 Local TxHandle:Int=LoadGLTexture("parkett.jpg")

 Local t:TTimer=CreateTimer(60)

 '-----------------------------------------------------------------------------

 Local Obj:TObject=TObject.Create(TV3D.Create(0,0,0))
 Local Sur:TSurface=Obj.AddSurface()

 Sur.AddCube(TV3D.Create(10,10,10),TxHandle)

 Obj.Move TV3D.Create(0,1,0),50,False

 '-----------------------------------------------------------------------------

 Local Obj2:TObject=TObject.Create(TV3D.Create(0,0,0))
 Local Sur2:TSurface=Obj2.AddSurface()

 Sur2.AddCube(TV3D.Create(10,10,10),TxHandle)

 Obj.AddChild Obj2

 Obj2.Move TV3D.Create(0,1,0),50+25,False

 '-----------------------------------------------------------------------------

 Local Obj3:TObject=TObject.Create(TV3D.Create(0,0,0))
 Local Sur3:TSurface=Obj3.AddSurface()

 Sur3.AddCube(TV3D.Create(10,10,10),TxHandle)

 Obj2.AddChild Obj3

 Obj3.Move TV3D.Create(0,1,0),50-25,False

 '-----------------------------------------------------------------------------

 Local Cam:TV3D=TV3D.Create(0,50,-50)

 While Not KeyHit(KEY_ESCAPE)

   WaitTimer t

   glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT   'Clear The Screen And The Depth Buffer

    Camera Cam.x,Cam.y,Cam.z, 0,0,0

    If KeyDown(KEY_UP) Then
     Cam=VADD(Cam,TV3D.Create(0,1,0))
    ElseIf KeyDown(KEY_DOWN) Then
     Cam=VADD(Cam,TV3D.Create(0,-1,0))
    EndIf

    If KeyDown(KEY_LEFT) Then
     Cam=VADD(Cam,TV3D.Create(-1,0,0))
    ElseIf KeyDown(KEY_RIGHT) Then
     Cam=VADD(Cam,TV3D.Create(1,0,0))
    EndIf

    If KeyDown(KEY_1) Then
     Obj.Turn Null,Obj.AX,1
    EndIf
    If KeyDown(KEY_3) Then
     Obj.Turn Null,Obj.AY,1
    EndIf
    If KeyDown(KEY_5) Then
     Obj.Turn Null,Obj.AZ,1
    EndIf

    If KeyDown(KEY_2) Then
     Obj.Turn Null,Obj.AX,-1
    EndIf
    If KeyDown(KEY_4) Then
     Obj.Turn Null,Obj.AY,-1
    EndIf
    If KeyDown(KEY_6) Then
     Obj.Turn Null,Obj.AZ,-1
    EndIf


    Obj2.Turn Null,Obj2.AY,1,False
    Obj3.Turn Null,Obj3.AY,-1,False


    If KeyDown(KEY_7) Then
     Obj.Move Obj.AY,1
    EndIf
    If KeyDown(KEY_8) Then
     Obj.Move Obj.AY,-1
    EndIf


    If KeyHit(KEY_RETURN) Then
     Obj.Shake
    EndIf

    glMatrixMode GL_MODELVIEW

    Raster

    Obj.Render True ' False '<- nur das eine zeigen
    Obj.RenderAchse

    'Obj2.Render
    'Obj2.RenderAchse

  Flip

 Wend

 FreeGlTexture TxHandle

End Function

'########################################################################################

Function glLine(P1:TV3D,P2:TV3D)

   Local l:Int
   l=glIsEnabled(GL_LIGHTING)
   If l Then glDisable GL_LIGHTING
   glBegin GL_LINES
    glVertex3d(P1.x,P1.y,P1.z)
    glVertex3d(P2.x,P2.y,P2.z)
   glEnd   
   If l Then glEnable GL_LIGHTING

End Function

'########################################################################################

Function MakeLight()

 'GL_AMBIENT
 'GL_DIFFUSE
 'GL_SPECULAR
 'GL_EMISSION
 'GL_SHININESS
 'GL_AMBIENT_AND_DIFFUSE

 glEnable(GL_LIGHTING)   'Licht wird wohl immer benutzt

 glEnable(GL_LIGHT0)

 Local p:Float[4]

 p[0]=0
 p[1]=0
 p[2]=0
 p[3]=1

 glLightfv(0,GL_POSITION,p)

 p[0]=0.8
 p[1]=0.8
 p[2]=0.8
 p[3]=1

 glLightfv(0,GL_SPECULAR,p)
 glLightfv(0,GL_DIFFUSE,p)
 glLightfv(0,GL_EMISSION,p)
 'glLightfv(0,GL_SHININESS,p)

 p[0]=0.1
 p[1]=0.1
 p[2]=0.1
 p[3]=1
 glLightfv(0,GL_AMBIENT,p)

 'GL_AMBIENT  (0.0, 0.0, 0.0, 1.0)  ambient intensity
 'GL_DIFFUSE (1.0, 1.0, 1.0, 1.0)  diffuse intensity
 'GL_SPECULAR (1.0, 1.0, 1.0, 1.0)  specular intensity
 'GL_POSITION (0.0, 0.0, 1.0, 0.0)  (x, y, z, w) position of light
 'GL_SPOT_DIRECTION (0.0, 0.0, -1.0)  direction of spot light

End Function

'########################################################################################

Function Camera(PosX:Double,PosY:Double,PosZ:Double,TargetX:Double,TargetY:Double,TargetZ:Double)
    glLoadIdentity()
   gluLookAt PosX,PosY,PosZ, TargetX,TargetY,TargetZ, 0,1,0 
End Function

'########################################################################################

Function myGraphics:TGraphics(width:Int,height:Int,depth:Int=16,herz:Int=0)

 Local flags:Int=GRAPHICS_BACKBUFFER | GRAPHICS_DEPTHBUFFER
 'If depth Then flags=flags | GRAPHICS_FULLSCREEN

 SetGraphicsDriver GLGraphicsDriver()

 glContext=GLGraphics(width,height,depth,herz,flags) '< BMax 1.12
 glViewport 0,0,width,height

 glDepthFunc GL_LESS
 glEnable GL_DEPTH_TEST                        'Enables Depth Testing
 glShadeModel GL_SMOOTH

 glMatrixMode GL_PROJECTION                     'Select The Projection Matrix
 glLoadIdentity                              'Reset The Projection Matrix
 Local fish:Double
 fish=1.2
 glFrustum -10.33*fish,10.33*fish,-10*fish,10*fish, 10.0,200000.0         'Setup The Projection Matrix Frustum
 glMatrixMode GL_MODELVIEW                     'Select The ModelView Matrix
 glLoadIdentity                              'Reset The ModelView Matrix

 glClearColor(0.5,0.5,0.5,1)                'Wie ClsColor aber mit Werte von 0-1 !

 Return glContext

End Function

'########################################################################################

Function LoadGLtexture:Int(Filename:String)

 Local pm:TPixmap=LoadPixmap(Filename)
 Local h:Int=0
 If pm<>Null Then
  'Pixmap=MaskPixmap(pm,0,0,0)
  h=GLTexFromPixmap(pm)
  pm=Null
 EndIf

 DebugLog "LoadGLtexture '"+Filename+"' Handle=" + h

 Return h

End Function

'########################################################################################

Function BeginRenderTexture(tx:Int)

  If tx<>0 Then
   glEnable GL_TEXTURE_2D 
   glBindTexture GL_TEXTURE_2D,tx
   Local Matrix:Int
   glGetIntegerv(GL_MATRIX_MODE, Varptr(Matrix))
   glMatrixMode(GL_TEXTURE)
   glLoadIdentity()
  ' glTranslated(Texture.PosU,Texture.PosV, 0.0)
  ' glRotated(Texture.Rotation, 0.0, 0.0, 1.0)
  ' glScaled(Texture.ScaleU,Texture.ScaleV, 1.0)
   glMatrixMode(Matrix)

'   If Texture.ClampU Then
'    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP)
'   Else
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT)
'   EndIf

'   If Texture.ClampV Then
'    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP)
'   Else
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT)
'   EndIf
 
  EndIf 'Texture are there

End Function

'########################################################################################

Function EndRenderTexture(tx:Int)

  If tx<>0 Then
   'If Texture.UseMask=True Then glDisable GL_BLEND
   glDisable GL_TEXTURE_2D 
  EndIf

End Function

'########################################################################################

Function FreeGlTexture(tx:Int)

 If tx Then glDeleteTextures 1,Varptr(tx) '???

End Function

'########################################################################################

Function Material()

 Local AMBIENT:Float[4] 
 Local DIFFUSE:Float[4]
 Local EMISSION:Float[4]
 Local SHININESS:Float[4]
 Local SPECULAR:Float[4] 'Spiegelreflexion

  AMBIENT  =[1.0, 1.0, 1.0, 1.0] 'Umgebung
  DIFFUSE  =[1.0, 1.0, 1.0, 1.0] '
  EMISSION =[0.1, 0.1, 0.1, 1.0] 'Leuchtet
  SHININESS=[0.0, 0.0, 0.0, 0.0] 'Glanz
  SPECULAR =[0.0, 0.0, 0.0, 0.0] 'Spiegelreflexion

  'Face :  GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
  glMaterialfv (GL_FRONT_AND_BACK ,GL_AMBIENT  , AMBIENT  )
  glMaterialfv (GL_FRONT_AND_BACK ,GL_DIFFUSE  , DIFFUSE  )
  glMaterialfv (GL_FRONT_AND_BACK ,GL_EMISSION , EMISSION )
  glMaterialfv (GL_FRONT_AND_BACK ,GL_SHININESS, SHININESS)
  glMaterialfv (GL_FRONT_AND_BACK ,GL_SPECULAR , SPECULAR)

  '''glEnable(GL_COLOR_MATERIAL)

End Function

'########################################################################################

Function Raster()

    glColor3d( 0.0, 1.0, 1.0 ) 'cyan

    glLine TV3D.Create(-50,0, 50) , TV3D.Create( 50,0, 50) 'Links->Rechts Hinten
    glLine TV3D.Create(-50,0,-50) , TV3D.Create( 50,0,-50)
    glLine TV3D.Create(-50,0, 50) , TV3D.Create(-50,0,-50)
    glLine TV3D.Create( 50,0, 50) , TV3D.Create( 50,0,-50)

    glLine TV3D.Create(-50,0, 50) , TV3D.Create(-50,100, 50)
    glLine TV3D.Create( 50,0, 50) , TV3D.Create( 50,100, 50)

End Function

'########################################################################################

    'glLoadIdentity()

    'double
    'glRotated 0 ,1.0,0.0,0.0 'Pitch in Grad
    'glRotated 0 ,0.0,1.0,0.0 'Yaw
    'glRotated 0 ,0.0,0.0,1.0 'Roll

    'glTranslated 0,0,0

    'glPushMatrix()
    'glPopMatrix()




MR_Vector.bmx
Code: [AUSKLAPPEN]


'#################################################################################################
' Vector Include by M.Rauch 05.08.2007
' MR_Vector.bmx
' für Physik
'#################################################################################################

'----------------------------------------------------------------------

Type TV3D
 Field x:Double
 Field y:Double
 Field z:Double
 
 Function Create:TV3D(x:Double=0,y:Double=0,z:Double=0)
  Local T:TV3D=New TV3D
  T.x=x
  T.y=y
  T.z=z
  Return T
 End Function

 Method Copy:TV3D()
  Local T:TV3D=New TV3D
  T.x=Self.x
  T.y=Self.y
  T.z=Self.z
  Return T
 End Method

 Method Set(x:Double,y:Double,z:Double=0)
  Self.x=x
  Self.y=y
  Self.z=z
 End Method

 'Method Draw(xx:Double=0,yy:Double=0)
 ' DrawLine xx,yy,xx+x*10.0,yy+y*10.0 'TEST
 'End Method

 Method Inv()
  x=-x
  y=-y
  z=-z
 End Method

 Method Clr()
  Self.x=0
  Self.y=0
  Self.z=0
 End Method
 
End Type

'---------------------------------------------------------------

Type TMatrix
 Field A:Double[4,4]

 Function Create:TMatrix()
  Local M:TMatrix = New TMatrix
  Return M
 End Function

End Type

'--------------------------------

Function VNORMAL:TV3D(p1:TV3D,p2:TV3D,p3:TV3D)

 'MR 05.08.2007
 
 'Oberflächen Normale von einer Ebene mit 3 Punkten (Dreieck)
 
  Local a:TV3D
  Local b:TV3D
  Local c:TV3D

  a=VSUB(p2,p1)
  b=VSUB(p3,p1)

  c=VCROSS(a,b)
   
  Return VNORMALIZE(c)

End Function

'--------------------------------

Function VNORMALIZE:TV3D(a:TV3D)

 'MR 05.08.2007
 
 'gibt Normvector zurück , aufpassen auf überlauf !

 'also gesamt Vector auf länge 1 bringen

 Local fa:Double
 Local n:TV3D

 fa = Sqr(VDOT(a, a))
 
 If fa = 0 Then
  n=TV3D.Create(0,0,0)
 Else
  fa = 1.0 / fa
  n=TV3D.Create(a.x * fa,a.y * fa,a.z * fa)
 End If
 
 Return n

End Function

'--------------------------------

Function VDOT:Double(a:TV3D, b:TV3D)
 
 'MR 05.08.2007

 'Dotprodukt - Skalarprodukt
 
 'berechnet ein Skalarprodukt zweier Vectoren
 
 Return (a.x * b.x + a.y * b.y + a.z * b.z)
 
End Function

'--------------------------------

Function VCROSS:TV3D(a:TV3D,b:TV3D)
 
 'MR 05.08.2007
 
 'gibt Vectorprodukt zurück
 
 Local c:TV3D=New TV3D
 
 c.x = a.y * b.z - b.y * a.z
 c.y = a.z * b.x - b.z * a.x
 c.z = a.x * b.y - b.x * a.y

 Return c

End Function

'--------------------------------

Function VADD:TV3D(v1:TV3D, v2:TV3D)

 'MR 05.08.2007
 
 '+
 
 Local vout:TV3D=New TV3D

 vout.x = v1.x + v2.x
 vout.y = v1.y + v2.y
 vout.z = v1.z + v2.z

 Return vout

End Function

'--------------------------------

Function VSUB:TV3D(v1:TV3D, v2:TV3D)

 'MR 05.08.2007
 
 '-

 Local vout:TV3D=New TV3D
 
 vout.x = v1.x - v2.x
 vout.y = v1.y - v2.y
 vout.z = v1.z - v2.z

 Return vout

End Function

'--------------------------------

Function VMUL:TV3D(v1:TV3D, v2:TV3D)

 'MR 05.08.2007
 
 '*

 Local vout:TV3D=New TV3D
 
 vout.x = v1.x * v2.x
 vout.y = v1.y * v2.y
 vout.z = v1.z * v2.z

 Return vout

End Function

'--------------------------------

Function VDIR:TV3D(a:TV3D, b:TV3D)

 'MR 05.08.2007

 Local hyp:Double

 Local vd:TV3D
 
 vd=VSUB(b,a)
 
 hyp = Sqr(vd.x * vd.x + vd.y * vd.y + vd.z * vd.z)
 
 If hyp <> 0.0 Then
  vd.x = vd.x / hyp
  vd.y = vd.y / hyp
  vd.z = vd.z / hyp
 Else
  vd.x = 0.0
  vd.y = 0.0
  vd.z = 0.0
 End If
 
 Return vd
 
End Function

'--------------------------------

Function VENT:Double(a:TV3D, b:TV3D)
 
 'MR 05.08.2007
 
 'Entfernung

 Local ve:TV3D
 Local e:Double

 ve=VSUB(b, a)
 
 e = Sqr(ve.x * ve.x + ve.y * ve.y + ve.z * ve.z)

 ve=Null

 Return e
 
End Function

'--------------------------------

Function VHALF:TV3D(v1:TV3D, v2:TV3D)

 'MR 05.08.2007
 
 'middle Vector between v1 and v2

 Local vout:TV3D=New TV3D
 
 vout.x = (v1.x + v2.x) / 2.0
 vout.y = (v1.y + v2.y) / 2.0
 vout.z = (v1.z + v2.z) / 2.0

 Return vout

End Function

'----------------------------------------------------------------------

Function VENTXY:Double(a:TV3D,b:TV3D)
 Local dx:Double,dy:Double
 dx=b.x-a.x
 dy=b.y-a.y

 Return Sqr(dx*dx + dy*dy)
End Function

'--------------------------------

Function VINV:TV3D(a:TV3D)

 'MR 06.08.2007

 a=a.Copy()
 a.Inv()

 Return a

End Function

'--------------------------------

Function VCOPY:TV3D(v:TV3D)

 'MR 05.08.2007
 
 '=
 
 Return v.Copy()

End Function

'-------------------------------------------------------------------------------------------------

Function VTRANS:TV3D(a:TV3D,M:TMatrix)

 'MR 09.07.2005

 'überschribt den Vector ! AUFPASSEN !

 Local b:TV3D=New TV3D
 
 'DebugLog "vorher A.xyz"

 'DebugLog a.x
 'DebugLog a.y
 'DebugLog a.z

 b.x = a.x * M.A[0, 0] + a.y * M.A[1, 0] + a.z * M.A[2, 0]
 b.y = a.x * M.A[0, 1] + a.y * M.A[1, 1] + a.z * M.A[2, 1]
 b.z = a.x * M.A[0, 2] + a.y * M.A[1, 2] + a.z * M.A[2, 2]

 Return b

 'DebugLog "nacher A.xyz"

 'DebugLog a.x
 'DebugLog a.y
 'DebugLog a.z

 'DebugLog "Matrix"

 'DebugLog M.A[0,0]
 'DebugLog M.A[0,1]
 'DebugLog M.A[0,2]

 'DebugLog M.A[1,0]
 'DebugLog M.A[1,1]
 'DebugLog M.A[1,2]

 'DebugLog M.A[2,0]
 'DebugLog M.A[2,1]
 'DebugLog M.A[2,2]
 
End Function

'-------------------------------------------------------------------------------------------------

Function VTRANS2:TV3D(a:TV3D,M:TMatrix)

 'benutzt von TEntity.Translate

 Local b:TV3D=New TV3D
 
 b.x = a.x * M.A[0, 0] + a.y * M.A[0, 1] + a.z * M.A[0, 2]
 b.y = a.x * M.A[1, 0] + a.y * M.A[1, 1] + a.z * M.A[1, 2]
 b.z = a.x * M.A[2, 0] + a.y * M.A[2, 1] + a.z * M.A[2, 2]

 Return b
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixZero(M:TMatrix Var)
 
 'MR 09.07.2005
 
 Local i:Int, j:Int
 
 For i = 0 To 3
  For j = 0 To 3
   M.A[i, j] = 0.0
  Next
 Next
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixCopy(M:TMatrix,MOut:TMatrix Var)
 
 'MR 09.07.2005
 
 Local i:Int, j:Int
 
 For i = 0 To 3
  For j = 0 To 3
   MOut.A[i, j] = M.A[i, j]
  Next
 Next
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixCreateIdentity(M:TMatrix Var)

 'MR 11.08.2005

 MatrixZero M

 Local i:Int

 For i = 0 To 3
  M.A[i, i] = 1.0
 Next
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixCreateTranslate(a:TV3D,M:TMatrix Var)
 
 'MR 09.07.2005
 
 MatrixCreateIdentity M
 
 M.A[3, 0] = a.x
 M.A[3, 1] = a.y
 M.A[3, 2] = a.z
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixCreateAxisRotate(axis:TV3D,Angle:Double,M:TMatrix Var)

 'MR 09.07.2005
 
 Local sqraxis:TV3D=TV3D.Create()
 
 sqraxis.x = sqare(axis.x)
 sqraxis.y = sqare(axis.y)
 sqraxis.z = sqare(axis.z)
 
 Local cosine:Double
 
 cosine = Cos(Angle)
 
 Local sine:Double
 
 sine = Sin(Angle)
 
 Local one_minus_cosine:Double
 
 one_minus_cosine = 1.0 - cosine
 
 MatrixZero M
 
 M.A[0, 0] = sqraxis.x + (1.0 - sqraxis.x) * cosine
 M.A[0, 1] = axis.x * axis.y * one_minus_cosine + axis.z * sine
 M.A[0, 2] = axis.x * axis.z * one_minus_cosine - axis.y * sine
 
 M.A[1, 0] = axis.x * axis.y * one_minus_cosine - axis.z * sine
 M.A[1, 1] = sqraxis.y + (1.0 - sqraxis.y) * cosine
 M.A[1, 2] = axis.y * axis.z * one_minus_cosine + axis.x * sine
 
 M.A[2, 0] = axis.x * axis.z * one_minus_cosine + axis.y * sine
 M.A[2, 1] = axis.y * axis.z * one_minus_cosine - axis.x * sine
 M.A[2, 2] = sqraxis.z + (1.0 - sqraxis.z) * cosine
 
 M.A[3, 3] = 1.0
 
 sqraxis=Null

End Function

'-------------------------------------------------------------------------------------------------

Function MatrixCreateScale(a:TV3D,M:TMatrix Var)
 
 'MR 09.07.2005

 MatrixZero M
 
 M.A[0, 0] = a.x
 M.A[1, 1] = a.y
 M.A[2, 2] = a.z
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixMultiply(M1:TMatrix,M2:TMatrix,MOut:TMatrix Var)

 'MR 09.07.2005
 
 'Multipliziert Matrix 1 & 2
 
 Local i:Int,j:Int
 
 For i = 0 To 3
  For j = 0 To 3
   MOut.A[i, j] = M1.A[i, 0] * M2.A[0, j] + M1.A[i, 1] * M2.A[1, j] + M1.A[i, 2] * M2.A[2, j] + M1.A[i, 3] * M2.A[3, j]
  Next
 Next
 
End Function

'-------------------------------------------------------------------------------------------------

Function MatrixDebugLog(M:TMatrix)

 'MR 09.07.2005
 
 'Als Debug Ausgabe
 
 DebugLog "MatrixDebug"

 Local i:Int,j:Int
 
 For i = 0 To 3
  For j = 0 To 3
   DebugLog M.A[i, j]
  Next
 Next
 
End Function

'-------------------------------------------------------------------------------------------------

'Function MatrixEntity(Entity:TEntity,MOut:TMatrix Var)
 
 'MR 01.08.2005
   
'  MOut.A[0, 0] = Entity.AchseX.x
'  MOut.A[0, 1] = Entity.AchseY.x
'  MOut.A[0, 2] = Entity.AchseZ.x
'  MOut.A[0, 3] = 0
'  MOut.A[1, 0] = Entity.AchseX.y
'  MOut.A[1, 1] = Entity.AchseY.y
'  MOut.A[1, 2] = Entity.AchseZ.y
'  MOut.A[1, 3] = 0
'  MOut.A[2, 0] = Entity.AchseX.z
'  MOut.A[2, 1] = Entity.AchseY.z
'  MOut.A[2, 2] = Entity.AchseZ.z
'  MOut.A[2, 3] = 0
'  MOut.A[3, 0] = Entity.Pos.x
'  MOut.A[3, 1] = Entity.Pos.y
'  MOut.A[3, 2] = Entity.Pos.z
'  MOut.A[3, 3] = 1
 
'End Function

'-------------------------------------------------------------------------------------------------

'Function MatrixEntityAxis(Entity:TEntity,MOut:TMatrix Var)
 
 'MR 01.08.2005
   
'  MOut.A[0, 0] = Entity.AchseX.x
'  MOut.A[0, 1] = Entity.AchseY.x
'  MOut.A[0, 2] = Entity.AchseZ.x
'  MOut.A[0, 3] = 0
'  MOut.A[1, 0] = Entity.AchseX.y
'  MOut.A[1, 1] = Entity.AchseY.y
'  MOut.A[1, 2] = Entity.AchseZ.y
'  MOut.A[1, 3] = 0
'  MOut.A[2, 0] = Entity.AchseX.z
'  MOut.A[2, 1] = Entity.AchseY.z
'  MOut.A[2, 2] = Entity.AchseZ.z
'  MOut.A[2, 3] = 0
'  MOut.A[3, 0] = 0
'  MOut.A[3, 1] = 0
'  MOut.A[3, 2] = 0
'  MOut.A[3, 3] = 1
 
'End Function

'-------------------------------------------------------------------------------------------------

Function MatrixQuaternion(x:Double,y:Double,z:Double,w:Double,MOut:TMatrix Var) 'not tested ! converted from Java Sample

 'MR 28.07.2005

 Local xx:Double = X * X
 Local xy:Double = X * Y
 Local xz:Double = X * Z
 Local xw:Double = X * W

 Local yy:Double = Y * Y
 Local yz:Double = Y * Z
 Local yw:Double = Y * W

 Local zz:Double = Z * Z
 Local zw:Double = Z * W

 MOut.A[0, 0] = 1.0 - 2.0 * ( yy + zz )
 MOut.A[0, 1] = 2.0 * ( xy - zw )
 MOut.A[0, 2] = 2.0 * ( xz + yw )
 MOut.A[0, 3] = 0
 MOut.A[1, 0] = 2.0 * ( xy + zw )
 MOut.A[1, 1] = 1.0 - 2.0 * ( xx + zz )
 MOut.A[1, 2] = 2.0 * ( yz - xw )
 MOut.A[1, 3] = 0
 MOut.A[2, 0] = 2.0 * ( xz - yw )
 MOut.A[2, 1] = 2.0 * ( yz + xw )
 MOut.A[2, 2] = 1.0 - 2.0 * ( xx + yy )
 MOut.A[2, 3] = 0
 MOut.A[3, 0] = 0
 MOut.A[3, 1] = 0
 MOut.A[3, 2] = 0
 MOut.A[3, 3] = 1

End Function

'-------------------------------------------------------------------------------------------------

Function Sqare:Double(x:Double)

 'MR 09.07.2005

 Return (x * x)

End Function

'-------------------------------------------------------------------------------------------------

'Ergebnis.X=X*XAchse.X + X*YAchse.X + X*ZAchse.X + Position.X;
'Ergebnis.Y=Y*XAchse.Y + Y*YAchse.Y + Y*ZAchse.Y + Position.Y;
'Ergebnis.Z=Z*XAchse.Z + Y*YAchse.Z + Y*ZAchse.Z + Position.Z;

Moep

BeitragFr, März 28, 2008 12:21
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke für eure Hilfe nochmals ^^,

ich hab zur Zeit nich sehr viel Zeit (Oo) zum Programmieren , aber ich werde mit deinen Code aufjedenfall mal angucken.


mfg
Hardstyle Schleichwerbung:
http://www.youtube.com/user/Hackepeter42

Achtung: Suchtgefahr!
moep123.ohost.de

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Allgemein

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group