2011-12-17 31 views
1

我正在使用raycast來確定ropejoint的錨點位置。通過使用一些簡單的繪製調用,我可以看到在raycast返回點可靠地創建了ropejoint。我的問題在於迴歸點。它偶爾會穿過身體,在對面的邊界上返回一個點,有時會在身體內部返回。當它發生時,它似乎總是失敗,也就是說,如果我投射了重複通過的光線,它會繼續通過並返回相同的不正確點。這使我相信我的身體存在問題。我正在使用TextureToBody轉換器來處理有問題的機構。Raycast在VB.NET.XNA中使用Farseer Physics丟失檢測結果

另一個較小的問題是,我必須在每個方向上從我的關節位置減去10/64以準確連接。我不知道爲什麼會發生這種情況。 (64pixels =1米是我使用的轉換比率)

光線投射方法:

Private Sub castRay(startPoint As Vector2, direction As Vector2) 
     direction *= 25 
     direction.Y = (-direction.Y) 
     world.RayCast(Function(fixture As Fixture, point As Vector2, normal As Vector2, fraction As Single) 
          Dim body As Body = fixture.Body 

          ropeContactFixture = fixture 
          ropeContactPoint = point 
          ropeJoint = New RopeJoint(Me.body, fixture.Body, New Vector2(0, 0), point - ropeContactFixture.Body.Position - (New Vector2(10, 10)/64)) 
          Return 0 
         End Function, startPoint, startPoint + direction) 
    End Sub 

回答

0

根據我的使用先知,應該儘量通過光線投射返回的所有點的列表,然後根據距離進行排序。

從Farseers碼兩者 -

 Ray-cast the world for all fixtures in the path of the ray. Your callback 
    controls whether you get the closest point, any point, or n-points. 
    The ray-cast ignores shapes that contain the starting point. 

    Inside the callback: 
    return -1: ignore this fixture and continue 
    return 0: terminate the ray cast 
    return fraction: clip the ray to this point 
    return 1: don't clip the ray and continue 

因此,使用這些知識,你應該能夠做出點名單沿射線,找到最近的,然後讓繩子。

作爲一個方面的說明,我不確定你爲什麼反轉direction.Y,但你應該確保這是你打算做的。