如何在PyBox2D中绘制圆形

时间:2019-05-28 08:45:01

标签: python pygame box2d

我有一个代码,可以使用pyBox2D和PyGame绘制由几个多边形组成的图形。我已经定义了实体和关节,可以很好地工作,可以完成预期的工作,但是当我想将头部从多边形更改为圆形时会出现问题,但是我无法绘制它,因为我用于绘制顶点,并且圆形具有没有顶点。

此部分代码(最终图形)中出现问题:

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或根本没有智能感知而引起的(可能是由于引起的)。这意味着我不知道需要哪些参数 任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

退房:

http://box2d.org/manual.pdf

b2CircleShape circle;
circle.m_p.Set(2.0f, 3.0f)

答案 1 :(得分:0)

也有python手册。选中this

circle = b2CircleShape(pos=(1, 2), radius=0.5)