如何制作“复合"pymunk 中的形状? [英] How to make "compound" shapes in pymunk?

查看:48
本文介绍了如何制作“复合"pymunk 中的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我怎样才能加入/约束 2 个 pymunk 物体/形状,使它们不作为一个单独的物体??
例如,在本例中,我有一个板球棒,由 2 个独立的主体和多边形组成.
我想把蝙蝠的手柄"连接到我的蝙蝠的刀片"上,这样我就得到了一个类似蝙蝠的物体.

As the title says, How can I join/constraint 2 pymunk bodies/shapes so that they don't they act as one single object??
For example, in this case, I have a cricket bat, made up of 2 separate bodies and polys.
I want to join the bat's "handle" to my bat's "blade", So that I get a bat-like object.

我的代码:

### BAT n Co. ###
# body format: [vertices, offset, position, mass]
bat_bodies_v = [
# bat
    [[[0, 34], [4, 34], [4, 0], [0, 0]],(-2,-20),(103,190),20], # handle
    [[[6, 90] , [0, 32] , [0, 17], [6, 0] , [10, 0], [10, 90]],(-5,-20),(100,100),1100] # blade
]

bat_bodies = []
for vertices, offset, pos, mass in bat_bodies_v:
    moment = pm.moment_for_poly(mass,vertices,offset)
    b = pm.Body(mass,moment)
    b.position = pos

    poly = pm.Poly(b, vertices,offset)
    poly.friction = 0.9

    bat_bodies.append(poly)
    space.add(b,poly)

# the closest I got.
j1 = pm.constraint.PinJoint(bat_bodies[0].body,bat_bodies[1].body)
j2 = pm.constraint.RotaryLimitJoint(bat_bodies[0].body,bat_bodies[1].body,0,0)
space.add(j1,j2)

<小时>

这个 ============== 变成 ================> <强>这个
我有一个函数可以在身体位置绘制那些绿色圆圈


This ============= becomes ================> This
I have a function that drew those green circles at the body positions

推荐答案

在 pymunk 中构建复杂形状的最佳方法是简单地将这些形状附加到同一个主体上.因此,除非您有充分的理由将它们分开,否则我建议您尝试将两种形状添加到同一主体中.

The best way to build a complex shape in pymunk is simply to attach the shapes to the same body. So unless you have a good reason why you want them separate I suggest you try and add both shapes to the same body.

但是,有时您可能想做其他事情,例如能够打破对象.我自己还没有真正实现任何东西,但 Scott(Chipmunk)在这篇文章中写道http://chipmunk-physics.net/forum/viewtopic.php?f=1&t=2420&p=10644&hilit=breakable#p10644使用 PivotJoint 和 GearJoint 可能是将两个实体保持在一起的好主意.

However, sometimes you might want to do something else, for example be able to break the objects. I havent really implmented anything myself, but Scott (of Chipmunk) writes in this post http://chipmunk-physics.net/forum/viewtopic.php?f=1&t=2420&p=10644&hilit=breakable#p10644 that using a PivotJoint and a GearJoint might be a good idea to keep the two bodies together.

这篇关于如何制作“复合"pymunk 中的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆