5

我希望能够锁定车轮相对于汽车底盘的角度。在车轮之间,有弹簧,应该可以让汽车暂停,但现在,角度没有锁定。我正在使用 pymunk 的函数“RotaryLimitJoint”

像这样的行为是目标 (gif)

通缉行为

现在它看起来像这样:

当前状态

我的代码:

car_pos = Vec2d(100,500)
mass = 30
radius = 10
moment = pymunk.moment_for_circle(mass, 20, radius)
wheel1_b = pymunk.Body(mass, moment)
wheel1_s = pymunk.Circle(wheel1_b, radius)
wheel1_s.friction = 1.5
wheel1_s.color = wheel_color
space.add(wheel1_b, wheel1_s)

mass = 30
radius = 10
moment = pymunk.moment_for_circle(mass, 20, radius)
wheel2_b = pymunk.Body(mass, moment)
wheel2_s = pymunk.Circle(wheel2_b, radius)
wheel2_s.friction = 1.5
wheel2_s.color = wheel_color
space.add(wheel2_b, wheel2_s)

mass = 100
size = (80,25)
moment = pymunk.moment_for_box(mass, size)
chassi_b = pymunk.Body(mass, moment)
chassi_s = pymunk.Poly.create_box(chassi_b, size)
chassi_s.color = chassi_color
space.add(chassi_b, chassi_s)


#Positions
chassi_b.position = car_pos + (0,-15)
wheel1_b.position = car_pos + (-25,0)
wheel2_b.position = car_pos + (25,0)

#Joints

spring1 = pymunk.DampedSpring(chassi_b, wheel1_b, (-25,0), (0,0), 20, 100000, 1)
spring1.collide_bodies = False
spring2 = pymunk.DampedSpring(chassi_b, wheel2_b, (25,0), (0,0), 20, 100000, 1)
spring2.collide_bodies = False

wheelAngle1 = pymunk.RotaryLimitJoint(wheel1_b, chassi_b, 0, 0)
wheelAngle1.collide_bodies = False
wheelAngle2 = pymunk.RotaryLimitJoint(chassi_b, wheel2_b, 0, 0)
wheelAngle2.collide_bodies = False



space.add(
    spring1,
    spring2,
    wheelAngle1,
    wheelAngle2
)
speed = 20

space.add(
    pymunk.SimpleMotor(wheel1_b, chassi_b, speed),
    pymunk.SimpleMotor(wheel2_b, chassi_b, speed)
)
4

2 回答 2

2

RotaryLimitJoint 不是您所需要的。它会限制车轮和底盘之间的角度,但车轮需要旋转,所以它不会工作。

您可以尝试使用 GrooveJoint。

这就是代码在 c 代码中的样子,将其转换为 python 应该相当容易:

完整来源:https ://github.com/slembcke/Chipmunk2D/blob/master/demo/Joints.c#L263

相关部分:

boxOffset = cpv(0, 0);
cpBody *wheel1 = addWheel(space, posA, boxOffset);
cpBody *wheel2 = addWheel(space, posB, boxOffset);
cpBody *chassis = addChassis(space, cpv(80, 100), boxOffset);

cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel1, cpv(-30, -10), cpv(-30, -40), cpvzero));
cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel2, cpv( 30, -10), cpv( 30, -40), cpvzero));

cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel1, cpv(-30, 0), cpvzero, 50.0f, 20.0f, 10.0f));
cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel2, cpv( 30, 0), cpvzero, 50.0f, 20.0f, 10.0f));

如果您无法弄清楚,我可以尝试将其转换为 python,但不幸的是,我现在不在一台装有 Python 和 Pymunk 的计算机上。

于 2020-05-06T19:29:25.590 回答
2

首先感谢@viblo。

使以下代码起作用的原因是 GrooveJoint(请参阅文档)是垂直于汽车底盘创建的。GrooveJoint 定义了一条线,身体可以自由滑动。定义 GrooveJoint,它连接到汽车的底盘和车轮(用于前轮和后轮)。

现在看起来像这样:

工作 gif

我将代码(来自@viblo)转换为python,它是:

def car(space, speed, add_car):
    car_pos = Vec2d(100,500)

    #bodies
    wheel_color = 0,0,0
    chassi_color = 255,0,0
    wheelCon_color = 0,255,255

    #Wheel 1
    mass = 25
    radius = 10
    moment = pymunk.moment_for_circle(mass, 20, radius)
    wheel1_b = pymunk.Body(mass, moment)
    wheel1_s = pymunk.Circle(wheel1_b, radius)
    wheel1_s.friction = 1.5
    wheel1_s.color = wheel_color

    #Wheel 2
    mass = 25
    radius = 10
    moment = pymunk.moment_for_circle(mass, 20, radius)
    wheel2_b = pymunk.Body(mass, moment)
    wheel2_s = pymunk.Circle(wheel2_b, radius)
    wheel2_s.friction = 1.5
    wheel2_s.color = wheel_color

    #Chassi
    mass = 30
    size = (80,25)
    moment = pymunk.moment_for_box(mass, size)
    chassi_b = pymunk.Body(mass, moment)
    chassi_s = pymunk.Poly.create_box(chassi_b, size)
    chassi_s.color = chassi_color

    #Positions
    chassi_b.position = car_pos + (0,-15)
    wheel1_b.position = car_pos + (-25,0)
    wheel2_b.position = car_pos + (25,0)

    #Joints
    spring1 = pymunk.constraint.DampedSpring(chassi_b, wheel1_b, (-25,0), (0,0), 15, 5000, 250)
    spring1.collide_bodies = False
    spring2 = pymunk.constraint.DampedSpring(chassi_b, wheel2_b, (25,0), (0,0), 15, 5000, 250)
    spring2.collide_bodies = False

    groove1 = pymunk.constraint.GrooveJoint(chassi_b, wheel1_b, (-25,0), (-25,25), (0, 0))
    groove1.collide_bodies = False
    groove2 = pymunk.constraint.GrooveJoint(chassi_b, wheel2_b, (25,0), (25,25), (0,0))
    groove2.collide_bodies = False

    if add_car:
        motor1 = pymunk.SimpleMotor(wheel1_b, chassi_b, speed)
        motor2 = pymunk.SimpleMotor(wheel2_b, chassi_b, speed)
        space.add(
            spring1,
            spring2,
            groove1,
            groove2,
            motor1,
            motor2,
            chassi_b, 
            chassi_s,
            wheel2_b,
            wheel2_s,
            wheel1_b,
            wheel1_s
        )
于 2020-05-06T21:00:06.943 回答