Box2D:逼真的弹跳墙壁

问题描述

我试图在pyBox2d中创建逼真的仿真,但是在使动态圆按预期方式碰撞时遇到了问题。

这是正在发生的事情:

actual

这是所期望的:

expected

圈/球代码

class Ball(object):
    """Simulates game ball."""

    def __init__(self,world,initPos=(0.55,0.35),radius=0.05,density=0.00001,restitution=1.0):

        bodyDef = Box2D.b2BodyDef()
        bodyDef.type = Box2D.b2_dynamicBody
        bodyDef.position = initPos
        self.body = world.CreateBody(bodyDef)

        shape = Box2D.b2CircleShape(radius=radius)
        self.body.CreateFixture(shape=shape,density=density,restitution=restitution)

    def getPoint(self):
        return (self.body.position[0],self.body.position[1],0)

    def getQuaternion(self):
        angle = self.body.angle % (2.0 * math.pi)
        return quaternion_from_euler(0,angle)

墙壁/世界的代码

class World(Box2D.b2World):
    """Simulates world for car to exist in"""

    WALL_THICKnesS = 0.02 # meters

    def __init__(self,width,height):
        super(World,self).__init__()
        self.gravity = (0,0)
        
        self.gndBody = self.CreateBody()
        gndShape = Box2D.b2polygonShape(Box=(width,height))
        gndFixtureDef = Box2D.b2FixtureDef(shape=gndShape)
        gndFixtureDef.isSensor = True
        self.gndBody.CreateFixture(gndFixtureDef)

        self.wallBodies = []
        wallPos = [(0,height),(width,0),(0,0)]
        for i in range(len(wallPos)):
            wallBody = self.CreateBody(position=wallPos[i])

            if (i % 2) == 0:
                wallShape = Box2D.b2polygonShape(Box=(width,self.WALL_THICKnesS))
            else:
                wallShape = Box2D.b2polygonShape(Box=(self.WALL_THICKnesS,height))
    
            wallFixtureDef = Box2D.b2FixtureDef(shape=wallShape,restitution=0.01)
            wallBody.CreateFixture(wallFixtureDef)

            self.wallBodies.append(wallBody)

让我知道是否需要更多信息。谢谢!

解决方法

我遇到了同样的问题,问题是默认为 1.0 的 restitution threshold 参数。 我建议不要将恢复设置为 1,使其为 0.99