2

我编写了以下程序,旨在模拟使用刀刃的重量平衡实验。但是,盒子的重量不会影响模拟时显示的段。我目前是学习如何使用 pymunk 编码的新手。请帮我解决这个问题

    import pyglet
    import pymunk
    import chipmunk
    from pymunk import Vec2d
    from pymunk.pyglet_util import DrawOptions

    window = pyglet.window.Window(800,600,"Knife Edge Mass Balancing Simulation",resizable=False)# W,H
    options=DrawOptions()

    space=pymunk.Space()
    space.gravity= 0,-100

    mass = 1
    radius=30


    ground_moment = pymunk.moment_for_segment(800,(0,0),(800,0),2)
    ground_body = pymunk.Body(body_type=pymunk.Body.STATIC)
    ground_shape = pymunk.Segment(ground_body,(0,0),(800,0),20)
    ground_body.position=0,100
    ground_body.elasticity=0.1
    ground_body.friction=0.1

    poly_shape=pymunk.Poly(None,((0,0),(100,0),(50,150)))
    poly_moment = pymunk.moment_for_poly(500,poly_shape.get_vertices())
    poly_body=pymunk.Body(body_type=pymunk.Body.STATIC)
    poly_shape.body=poly_body
    poly_body.position = 350,100
    poly_body.elasticity=0.1


    segment_moment = pymunk.moment_for_segment(mass,(0,0),(300,0),2)
    segment_body = pymunk.Body(mass,segment_moment)
    segment_shape = pymunk.Segment(segment_body,(0,0),(400,0),5)
    segment_body.position= 210,250
    segment_shape.elasticity = 0.1
    segment_shape.friction = 0.1

    size = 20
    box_mass = 0.0

    moment = pymunk.moment_for_box(box_mass, (size, size))
    box_body = pymunk.Body(box_mass, moment)
    box_body.position = Vec2d(300, 265.5)
    box_shape = pymunk.Poly.create_box(box_body, (size, size))
    box_shape.friction = 0.8
    box_shape.elasticity = 0.1


    space.add(ground_body, ground_shape,poly_body,poly_shape,segment_body, segment_shape, box_body, box_shape)


   @window.event
   def on_draw():
   window.clear()
   space.debug_draw(options)

   def update(dt):
   space.step(dt)

   if __name__=="__main__":
       pyglet.clock.schedule_interval(update,1.0/60)
       pyglet.app.run()
4

1 回答 1

1

问题是线段形状的重量在一端,而不是在中心。发生这种情况是因为 Pymunk 中的重量是在形状的主体位置收集的。

尝试将段代码更改为如下所示:

segment_moment = pymunk.moment_for_segment(mass,(-150,0),(150,0),2)
segment_body = pymunk.Body(mass,segment_moment)
segment_shape = pymunk.Segment(segment_body,(-200,0),(200,0),5)
segment_body.position= 400,250
segment_shape.elasticity = 0.1
segment_shape.friction = 0.1
于 2020-08-01T06:40:07.913 回答