簡體   English   中英

在python中碰撞形狀

[英]Colliding shapes in python

我的計划是創建一個“簡單”的2人小游戲(例如,相撲和種族)。

我的目標是有效地與正方形,圓形(和三角形?)形狀的對象(可能是環境的一部分)發生碰撞(我當前的代碼只能處理簡單的牆體物理和對象的運動)。或不可移動的障礙物)或部分用戶控制的物品(例如“汽車”或可推動的障礙物)。 知道如何在碰撞中也考慮質量也很高興。

我需要兩個方面的幫助:

  1. 兩個運動物體(具有質量和2D矢量)之間的不同類型的動態碰撞(物理部分不是檢測部分)。

  2. 確保所有需要碰撞的東西都以足夠快的速度碰撞(這樣我的慢速計算機仍然可以每秒渲染40-60幀以上),並按照特定的規則(或者是否有可能按照一條規則進行渲染?) 。 這樣也就不難管理需要碰撞的對象(添加,刪除,修改等)。

還是我應該為ex實現兩種類型的碰撞? 靜態+動態圈和動態+動態圈?

def checkcollisions(object1, object2):
    # x is the current x position
    # y is the current y position
    # angle is the current vector angle (calculated from x and y with pythagoros
    # speed is the length of the vector
    dx = object1.x - object2.x
    dy = object1.y - object2.y

    dist = hypot(dx, dy)
    if dist < object1.radius + object2.radius:
        angle = atan2(dy, dx) + 0.5 * pi
        total_mass = object1.mass + object2.mass
        '''''http://www.petercollingridge.co.uk/pygame-physics-simulation/mass'''''
        if (0.79 <= object1.angle < 2.36 or 0.79-2*pi <= object1.angle < 2.36-2*pi) or (3.93 <= object1.angle < 5.5 or 3.93-2*pi <= object1.angle < 5.5-2*pi) and ((0.79 <= object2.angle < 2.36 or 0.79-2*pi <= object2.angle < 2.36-2*pi) or (3.93 <= object2.angle < 5.5 or 3.93-2*pi <= object2.angle < 5.5-2*pi)):
            (object2angle, object2speed) = vectorsum((object2.angle, object2.speed*(object2.mass-object1.mass)/total_mass), (angle+pi, 2*object1.speed*object1.mass/total_mass))
            (object1angle, object1speed) = vectorsum((object1.angle, object1.speed*(object1.mass-object2.mass)/total_mass), (angle, 2*object2.speed*object2.mass/total_mass))
        else:
            '''''https://en.wikipedia.org/wiki/Elastic_collision'''''
            CONTACT_ANGLE = angle
            x = (((object1.speed * cos(object1.angle - CONTACT_ANGLE) * (object1.mass-object2.mass)+ 2*object2.mass*object2.speed*cos(object2.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object1.speed*sin(object1.angle - CONTACT_ANGLE)*cos(CONTACT_ANGLE + 0.5 * pi)
            y = (((object1.speed * cos(object1.angle - CONTACT_ANGLE) * (object1.mass-object2.mass)+ 2*object2.mass*object2.speed*cos(object2.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object1.speed*sin(object1.angle - CONTACT_ANGLE)*sin(CONTACT_ANGLE + 0.5 * pi)
            object1angle = pi/2 - atan2(y, x)
            object1speed = hypot(x, y)

            x = (((object2.speed * cos(object2.angle - CONTACT_ANGLE)*(object2.mass-object1.mass)+2*object1.mass*object1.speed*cos(object1.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object2.speed*sin(object2.angle - CONTACT_ANGLE)*cos(CONTACT_ANGLE + 0.5 * pi)
            y = (((object2.speed * cos(object2.angle - CONTACT_ANGLE)*(object2.mass-object1.mass)+2*object1.mass*object1.speed*cos(object1.angle - CONTACT_ANGLE))/total_mass)*cos(CONTACT_ANGLE))+object2.speed*sin(object2.angle - CONTACT_ANGLE)*sin(CONTACT_ANGLE + 0.5 * pi)
            object2angle = pi/2 - atan2(y, x)
            object2speed = hypot(x, y)

        (object2.angle, object2.speed) = (object2angle, object2speed)
        (object1.angle, object1.speed) = (object1angle, object1speed)
        object1.speed *= 0.999
        object2.speed *= 0.999

        overlap = 0.5*(object1.radius + object2.radius - dist+1)
        object1.x += sin(angle)*overlap
        object1.y -= cos(angle)*overlap
        object2.x -= sin(angle)*overlap
        object2.y += cos(angle)*overlap

'''''http://www.petercollingridge.co.uk/pygame-physics-simulation/mass'''''


def vectorsum(vectorx, vectory):  # Every array's first number is the degree from 0, the second is speed
    x = sin(vectory[0]) * vectory[1] + sin(vectorx[0]) * vectorx[1]
    y = cos(vectory[0]) * vectory[1] + cos(vectorx[0]) * vectorx[1]  # Calculating new vectors from anle and lenght
    angle = pi / 2 - atan2(y, x)  # Calculating the degree
    speed = hypot(x, y)  # Calculating the speed
    return angle, speed

(我只是Python(或英語)的初學者,所以請記住這一點。)

  1. 碰撞檢測在pygame中非常容易。 看看使用pygame.sprite 它們具有檢測碰撞的幾種功能。 spritecollidegroupcollide等)如果通常有一些復雜的碰撞交互,則可以使用rect或circle來查看它們是否發生碰撞,然后僅對它們進行復雜的計算。 盡管對於大多數游戲而言,您無需花費完美的碰撞檢測成本,但足夠接近就足夠了。
  2. 到目前為止,當您發生碰撞時會發生什么,那就是比編程更多的物理學。 要記住的一些概念是:動量守恆,彈性與非彈性碰撞,偏轉角。 對於SO問題,“如何構建2D物理引擎”有點太寬泛了。 也許看看如何創建一個自定義的2D物理引擎導向的剛體

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM