2010-03-19 56 views
0

在一般條款和僞代碼,這將是有沿牆壁滑動的碰撞響應的最好方式,如果牆面實際上只是一個整個廣場的那一個點的一部分正在碰撞?所使用的碰撞測試方法是測試以確定點是否位於正方形中。滑動響應指向 - 廣場碰撞

我應該把正方形分成四條線,只計算最短的距離,然後將點移回距離?如果是,那我怎樣才能確定點後最接近的平方的邊緣?

回答

2

由靠牆測試的移動矢量檢測碰撞點。如果你知道你的事情的表面(例如,你說這是一個框部分)你也許可以同時測試多個牆壁。

該溶液可以是2D和3D之間略有不同。因爲你說的是​​「正方形」而不是「立方體」或「盒子」,所以我會使用2D。

一旦你知道你的點擊中的位置,就可以將剩餘的運動矢量與牆壁方向對齊(從另一個點減去牆上的一個點然後標準化),然後按照該方向縮放牆的方向。假設沒有摩擦,這是平行於牆的運動量。

編輯添加以下代碼:

樣板:

import math 

class Vector2d: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def __add__(self, rhs): 
     return Vector2d(self.x + rhs.x, self.y + rhs.y) 

    def __sub__(self, rhs): 
     return Vector2d(self.x - rhs.x, self.y - rhs.y) 

    def GetScaled(self, scale): 
     return Vector2d(self.x * scale, self.y * scale) 

    def GetLength(self): 
     return math.sqrt((self.x * self.x) + (self.y * self.y)) 

    def GetNormalized(self): 
     return self.GetScaled(1.0/self.GetLength()) 

def DotProduct(v0, v1): 
    return (v0.x * v1.x) + (v0.y * v1.y) 

真正的業務:

class Wall2d: 
    def init(self, point0, point1): 
     """point0, point1 are Vector2ds""" 
     self.p0 = point0 
     self.p1 = point1 

     # these three steps could be combined to optimize, but 
     # for demonstration are left explicit 
     self.dir = self.p1 - self.p0 
     self.length = self.dir.GetLength() 
     self.dir = self.dir.GetNormalized() 

     # computing the normal in 3D would require three points 
     # on the face and a cross product 
     self.normal = Vector2d(self.length.y, -self.length.x) 

    def LineSegmentCollides(self, pointStart, pointEnd): 
     startDot = DotProduct(pointStart - self.p0, self.normal) 
     endDot = DotProduct(pointEnd - self.p0, self.normal) 
     if startDot * endDot < 0: 
      # the only way a collision can occur is if the start 
      # and end are on opposite sides of the wall, so their 
      # dot product results will have opposite signs, so 
      # the result of the multiplication is negative 
      moveVector = pointEnd - pointStart 

      # scale the movement vector by the ratio of the move 
      # vector on the "start" side versus the total length 
      # of the movement in the axis of the normal 
      collisionDelta = moveVector.GetScaled(startDot/
                (startDot + endDot)) 
      collisionPoint = pointStart + collisionDelta 

      collisionDot = DotProduct(collisionPoint - self.p0, self.dir) 
      if (collisionDot > 0) && (collisionDot < self.length): 
       # we've hit the wall between p0 and p1 (other 
       # values of collisionDot mean we missed on one 
       # end or the other) 

       # now, collision response is up to you. In this 
       # case, we'll just zero out the movement in the 
       # direction of the wall after the collision 
       # (sorry about the poor naming) 
       # note that we don't actually care about the actual 
       # point of collision here. 
       collisionPushBack = moveVector.GetScaled(
             endDot/(startDot + endDot)) 
       endPoint = pointEnd + collisionPushBack 

       return True 
     return False 

我希望這是有益的。

+0

該代碼將是有益的,謝謝。 – mars 2010-03-19 20:55:48