如何在 PyBox2D 中绘制圆形
How to draw a circular shape in PyBox2D
我有一个代码可以使用 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 没有顶点。我怎样才能画一个圆或广告顶点到那个形状?
非常感谢
编辑:"duplicate" 没有回答我的问题,你能告诉我如何定义循环 body 我试过这个
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)
我有一个代码可以使用 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 没有顶点。我怎样才能画一个圆或广告顶点到那个形状? 非常感谢
编辑:"duplicate" 没有回答我的问题,你能告诉我如何定义循环 body 我试过这个
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)