Oppure

Loading
18/11/11 18:13
GN
Come già segnalato in topic precedenti (pierotofy.it/pages/extras/forum/16/1032564-%5Bvbnet%5Bdirect3d9algoritmo_per_sparo/), sto sviluppando un semplice gioco di carri armati in directx con vb.net. Da mesi non riesco a farli sparare; ora ho scritto un algoritmo più efficace di quello di prima, utilizzando un sistema di equazioni, ma non funziona. Posto il codice in cui rilevo lo sparo:
                    Dim MinNemico As New Vector3()
                    Dim MaxNemico As New Vector3()
                    Me.BoundingBox(Nemico, MinNemico, MaxNemico)
                    Dim Colpito As Boolean = False
                    'equazione retta
                    'y + giocatore.pos.Z = coeff * x + giocatore.pos.X
                    'y + giocatore.pos.Z - coeff * x - giocatore.pos.X = 0
                    Dim coeff As Double = Math.Tan(90 - Rad(AngoloGiocatore))
                    Dim a1 As Double = -coeff
                    Dim b1 As Double = 1
                    Dim c1 As Double = -(Giocatore.pos.Z - Giocatore.pos.X)
                    'equazione diagonale rettangolo
                    '(y-y1)\(y2-y1)=(x-x1)\(x2-x1)
                    '(y - minnemico.z)\(maxnemico.z - minnemico.z) = (x - minnemico.x)\(maxnemico.x - minnemico.x)
                    Dim n1 As Double = MinNemico.Z
                    Dim n2 As Double = MaxNemico.Z - MinNemico.Z
                    Dim n3 As Double = MinNemico.X
                    Dim n4 As Double = MaxNemico.X - MinNemico.X
                    '(y - n1)\n2 = (x - n3)\n4
                    'n4(y-n1) = n2(x-n3)
                    'n4*y + n4*n1 = n2*x - n2*n3
                    '-n2*x + n4*y = n2*n3 - n4*n3
                    Dim a2 As Double = -n2
                    Dim b2 As Double = n4
                    Dim c2 As Double = n2 * n3 - n4 * n3
                    Dim p As PointF = Me.Cramer(a1, b1, c1, a2, b2, c2)
                    Dim rect As New RectangleF(MinNemico.X, MinNemico.Z, MaxNemico.X - MinNemico.X, MaxNemico.Z - MinNemico.Z)
                    If rect.Contains(p) Then
                        Nemici.RemoveAt(i)
                        Sparo.Play(0, BufferPlayFlags.Default)
                        If Nemici.Count = 0 Then
                            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Livello.txt", Livello + 1, False)
                            FineLivello.Play(0, BufferPlayFlags.Default)
                            TimerTempo.Stop()
                            Me.GeneraLivello()
                        End If
                        Exit For
                    End If

E quello della funzione cramer, che dovrebbe restituire il punto di intersezione.
    Function Cramer(ByVal a1 As Double, ByVal b1 As Double, ByVal c1 As Double, ByVal a2 As Double, ByVal b2 As Double, ByVal c2 As Double) As PointF
        'a1 b1
        'a2 b2
        Dim D As Double = a1 * b2 - b1 * a2
        'c1 b1
        'c2 b2
        Dim Dx As Double = c1 * b2 - b1 * c2
        'a1 c1
        'a2 c2
        Dim Dy As Double = a1 * c2 - c1 * a2
        Return New PointF(Dx / D, Dy / D)
    End Function

Per favore aiutatemi, è un sacco di tempo che ho questo problema :(:(:(:(... grazie in anticipo.
aaa