Seite 2 von 3

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 09:43
von super_castle
hallo mpz, mit 1,6ghz (notebook,512mb und grafikkarte on board), komme ich auf 65fps.
also eine tolle leistung, was du da rausgeholt hast.

gruss
peter

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 11:38
von gnasen
hier eine Dreieckskollision:

Code: Alles auswählen

EnableExplicit

Procedure.l col_line_line(x1.f,y1.f,x2.f,y2.f,x3.f,y3.f,x4.f,y4.f)
  ;check if two lines collide
  ;modified code from: Stargate (PB Forums)
  
  Protected cross_prod_a.f
  Protected cross_prod_b.f
  Protected new_vectorX.i
  Protected new_VectorY.i
  Protected comp.f
  
  cross_prod_a = (x2-x1)*(y4-y3) - (x4-x3)*(y2-y1)
  
  If cross_prod_a
    
    new_vectorX = x3 - x1
    new_VectorY = y3 - y1
    
    cross_prod_b = new_vectorX * (y4-y3) - new_VectorY * (x4-x3)
    
    comp = cross_prod_b / cross_prod_a
    
    If comp >= 0 And comp <= 1
      
      cross_prod_b = new_vectorX * (y2-y1) - new_VectorY * (x2-x1)
      
      comp = cross_prod_b / cross_prod_a
      
      If comp >= 0 And comp <= 1
        
        ProcedureReturn #True
        
      EndIf
      
    EndIf
    
  EndIf
  
  ProcedureReturn #False
  
EndProcedure

Procedure.l col_point_tri(t1x.f,t1y.f,t2x.f,t2y.f,t3x.f,t3y.f,Px.f,Py.f)
  ;check if point lies in the triangle
  
  Protected r.f
  Protected s.f
  Protected result.f
  
  Protected Ax.f = t2x - t1x
  Protected Ay.f = t2y - t1y
  Protected Bx.f = t3x - t1x
  Protected By.f = t3y - t1y
  
  Px - t1x
  Py - t1y
  
  If By = 0
    By = 0.0001
  EndIf
  
  r = (By*Ax-Ay*Bx)
  
  If r 
    r      = (Px*By-Bx*Py)/r
    s      = (Py-Ay*r)/By
    result = r+s
    
    If result <= 1 And result => 0 And r <= 1 And r => 0 And s <= 1 And s => 0
      ProcedureReturn #True
    EndIf
  EndIf
  
  ProcedureReturn #False
  
EndProcedure

Procedure collision(a1x.i,a1y.i,a2x.i,a2y.i,a3x.i,a3y.i,b1x.i,b1y.i,b2x.i,b2y.i,b3x.i,b3y.i)
  
  If col_point_tri(a1x,a1y,a2x,a2y,a3x,a3y,b1x,b1y) Or col_point_tri(a1x,a1y,a2x,a2y,a3x,a3y,b2x,b2y) Or col_point_tri(a1x,a1y,a2x,a2y,a3x,a3y,b3x,b3y)
    ProcedureReturn #True
  ElseIf col_point_tri(b1x,b1y,b2x,b2y,b3x,b3y,a1x,a1y) Or col_point_tri(b1x,b1y,b2x,b2y,b3x,b3y,a2x,a2y) Or col_point_tri(b1x,b1y,b2x,b2y,b3x,b3y,a3x,a3y)
    ProcedureReturn #True
  ElseIf col_line_line(a1x,a1y,a2x,a2y,b1x,b1y,b2x,b2y) Or col_line_line(a2x,a2y,a3x,a3y,b1x,b1y,b2x,b2y) Or col_line_line(a3x,a3y,a1x,a1y,b1x,b1y,b2x,b2y) 
    ProcedureReturn #True
  ElseIf col_line_line(a1x,a1y,a2x,a2y,b2x,b2y,b3x,b3y) Or col_line_line(a2x,a2y,a3x,a3y,b2x,b2y,b3x,b3y) Or col_line_line(a3x,a3y,a1x,a1y,b2x,b2y,b3x,b3y) 
    ProcedureReturn #True
  ElseIf col_line_line(a1x,a1y,a2x,a2y,b3x,b3y,b1x,b1y) Or col_line_line(a2x,a2y,a3x,a3y,b3x,b3y,b1x,b1y) Or col_line_line(a3x,a3y,a1x,a1y,b3x,b3y,b1x,b1y)
    ProcedureReturn #True
  EndIf
  
  ProcedureReturn #False
  
EndProcedure

Define a1x.i,a1y.i,a2x.i,a2y.i,a3x.i,a3y.i,b1x.i,b1y.i,b2x.i,b2y.i,b3x.i,b3y.i

;no hit

a1x = 1 : a1y = 1
a2x = 3 : a2y = 4
a3x = 5 : a3y = 1

b1x = 4 : b1y = 3
b2x = 9 : b2y = 3
b3x = 6 : b3y = 0

Debug collision(a1x,a1y,a2x,a2y,a3x,a3y,b1x,b1y,b2x,b2y,b3x,b3y)

;hit (one edge in another triangel)

a1x = 1 : a1y = 1
a2x = 4 : a2y = 4
a3x = 5 : a3y = 1

b1x = 3 : b1y = 2
b2x = 7 : b2y = 2
b3x = 6 : b3y = 4

Debug collision(a1x,a1y,a2x,a2y,a3x,a3y,b1x,b1y,b2x,b2y,b3x,b3y)

;hit (only crossing lines)

a1x = 1 : a1y = 1
a2x = 3 : a2y = 4
a3x = 5 : a3y = 1

b1x = 1 : b1y = 3
b2x = 5 : b2y = 3
b3x = 3 : b3y = 0

Debug collision(a1x,a1y,a2x,a2y,a3x,a3y,b1x,b1y,b2x,b2y,b3x,b3y)
Wie gesagt, einfach aus meiner Routine von oben geschnippelt, sollte dir einiges an Arbeit ersparen ;)
Sollte extrem schnell sein, eine zusätzliche Optimierung wäre noch eine vorherige rechteck-rechteck Kollisionsprüfung, habe ich der einfachheit halber aber mal weg gelassen.

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 12:41
von mpz
Hi Gnasen,

da kann ich mich nur bedanken...


Grüß Michael

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 12:47
von super_castle
Also bei mir läuft das von "gnasen" nicht.
..absturz.

Gruss

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 12:55
von gnasen
Wären etwas mehr Informationen möglich? Denn ausser ein paar Grundrechenarten und Prozeduraufrufen passiert da nicht viel, was absturzen könnte. Was sagt denn der Debugger?

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 13:34
von Kaeru Gaman
... oha ... :lurk:

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 14:25
von STARGÅTE
frage:
wieso braucht dein Programm so drigend:
---------------------------
test3.exe - Komponente nicht gefunden
---------------------------
Die Anwendung konnte nicht gestartet werden, weil d3dx9_38.dll nicht gefunden wurde. Neuinstallation der Anwendung könnte das Problem beheben.
---------------------------
OK
---------------------------
das sind doch nur Sprites ...

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 14:27
von super_castle
veraltete funktion "Countlist()" verwendet
Dat is se.....

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 14:28
von super_castle
das sind doch nur Sprites ...
...aber "texturen"...

Re: Suche Kollisionsprüfung Dreieck Dreieck

Verfasst: 05.02.2010 14:32
von Kaeru Gaman
@STARGÅTE

wenn ich mich nicht täusche, benutzt seine Engine DX9 direkt und nicht über PB.
die DLLs von DX müssen gegebenenfalls mal upgedatet werden.
als ich vor ner Weile Civ4 gepatcht hab, musste ich ein halbes Dutzend DLLs nachinstallieren, weil mein letztes DX9 von ner älteren CD war.