如何使用PyBox2d检测碰撞并使用该信息

问题描述 投票:0回答:2

嗨,我有这个简单的代码来测试在Box2d中处理碰撞的情况,它只是将矩形降落到地面物体的高度,当物体向左/向右旋转45度(这意味着它接触地面时,会发生一个有趣的问题)身体的一角)它只是停在那儿,并没有按预期下降。

import pygame
import numpy as np
from Box2D.b2 import world, polygonShape, circleShape,edgeShape, staticBody, dynamicBody, kinematicBody, revoluteJoint, wheelJoint, contact
from Box2D import b2Vec2, b2FixtureDef,b2PolygonShape, b2CircleShape, b2Dot,b2EdgeShape, b2Contact,b2ContactFilter,b2Filter, b2ContactListener


pygame.init()

PPM = 15
SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
pos_X = SCREEN_WIDTH/PPM/3
pos_Y = SCREEN_HEIGHT/PPM

Box_2_World = world(gravity = (0.0, -9.81), doSleep = True)

Rectangle = Box_2_World.CreateDynamicBody(position = (pos_X+5, pos_Y + 5),
                                    angle = np.pi/4,
                                    fixtures = b2FixtureDef(
                                        shape = b2PolygonShape(box= (5,5)),
                                        density = 1000,
                                        friction = 1000,
                                                            ))
Ground = Box_2_World.CreateStaticBody(position = [0,0],
                                    angle = 0,
                                    fixtures = b2FixtureDef(
                                        shape = b2PolygonShape(box= (50,1)),
                                        density = 1000,
                                        friction = 1000,
                                                            ))


screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
colors = {dynamicBody: (133, 187, 101, 0),  staticBody: (15, 0, 89, 0)}
FPS = 24
TIME_STEP = 1.0 / FPS
running = True

def my_draw_polygon(polygon, body, fixture):
    vertices = [(body.transform * v) * PPM for v in polygon.vertices]
    vertices = [(v[0], SCREEN_HEIGHT - v[1]) for v in vertices]
    pygame.draw.polygon(screen, colors[body.type], vertices)
polygonShape.draw = my_draw_polygon

while running:
    # Draw the world
    screen.fill((255, 255, 255, 255))

    for body in Box_2_World.bodies:
        for fixture in body.fixtures:
            fixture.shape.draw(body, fixture)

    # Simulate dynamic equation in each step
    TIME_STEP = 1.0 / FPS
    Box_2_World.Step(TIME_STEP, 10, 10)

        # Flip the screen and try to keep at the target FPS
    pygame.display.flip() # Update the full display Surface to the screen
    pygame.time.Clock().tick(FPS)

pygame.quit()

编辑:主要主题已解决,所以现在我可以移至其他问题(对我来说更重要)

我正在开发一个更复杂的游戏,但是我在碰撞检测上停留了大约一个星期,因此我编写了这个简单的脚本,以便更有效地了解碰撞,但是我仍然陷于困境。我不知道如何设置contactListener或如何检测两个物体是否碰撞。我用Google搜索它,寻找教程,但没有任何帮助(主要是因为这些教程不是用python编写的。)>

所以...如果您向我展示脚本时,我将不胜感激,例如,当矩形碰到地面时如何更改矩形的颜色(我没有向您展示我已经尝试过的内容,因为有多种使用方法例如内存位置,我想使这个示例尽可能简单。

非常感谢您的任何回复。没有这个,我将无法前进...

编辑:我已经找到一个链接,该链接可以完全解决我想做的事情,但是它是用C ++编写的,我不理解http://www.iforce2d.net/b2dtut/collision-callbacks

嗨,我有这个简单的代码来测试在Box2d中处理碰撞的情况,它只是将矩形降到给定的高度,当将主体旋转45度时发生了一个有趣的问题...

python pygame collision-detection box2d
2个回答
0
投票
有趣的是,解决方案多么容易,我只需要添加一个新类:

0
投票
嘿,我刚刚回答了您关于stackexchange的问题:-)
© www.soinside.com 2019 - 2024. All rights reserved.