我有一个代码,它使用 pyBox2D 和 PyGame 绘制一个由几个多边形组成的图形。我已经定义了 body 和关节,它工作得很好,它做了它应该做的,但是当我想将头部从多边形更改为圆形时出现问题,但我无法绘制它,因为我用于绘制顶点和圆形有没有顶点。
问题出现在这部分代码(最终绘制):
for body in world.bodies: #(ground_body, dynamic_body): # or: world.bodies
# The body gives us the position and angle of its shapes
for fixture in body.fixtures:
shape = fixture.shape
vertices = [(body.transform * v) * PPM for v in shape.vertices]
vertices = [(v[0], SCREEN_HEIGHT - v[1]) for v in vertices]
pygame.draw.polygon(screen, colors[body.type], vertices)
正如我上面所说,问题在于 box2D. b2circleShape 没有顶点。我怎样才能画一个圆或广告顶点到那个形状?非常感谢
编辑:“重复”没有回答我的问题,你能告诉我如何定义圆形体我试过了
import Box2D # The main library
# Box2D.b2 maps Box2D.b2Vec2 to vec2 (and so on)
from Box2D.b2 import (world, polygonShape, staticBody, dynamicBody, circleShape)
from Box2D import (b2FixtureDef, b2PolygonShape, b2CircleShape)
chest_body = world.CreateDynamicBody(
position=(10, 6.5),
fixtures=b2FixtureDef(
shape=b2PolygonShape(box=(0.5, 1.5)), density=120),
angle=0) # This is a rectangular body which is defined correctly
circle = world.CreateDynamicBody(
position=(10, 6.5),
fixtures=b2FixtureDef(
shape=b2CircleShape(0.5),
angle=0)) # I tried this after checking your manual, this does not work
这个问题可能是因为我不知道如何为 pyBox2D 运行 IntelliSense 或者根本不知道是否有 intellisense。这意味着我不知道需要哪些参数任何帮助表示赞赏
有一本python的手册。检查this
circle = b2CircleShape(pos=(1, 2), radius=0.5)
我是一名优秀的程序员,十分优秀!