如何保持pybox2d中的身体角度在-pi和pi之间?

问题描述:

pybox2d manual它规定了以下内容:如何保持pybox2d中的身体角度在-pi和pi之间?

pybox2d uses radians for angles. The body rotation is stored in radians and may grow unbounded. Consider normalizing the angle of your bodies if the magnitude of the angle becomes too large (use b2Body.SetAngle).

然而,当我试图实现的东西“正常化”我得到以下错误的角度:

AttributeError: 'b2Body' object has no attribute 'SetAngle' 

代码片段:

def update_outputs(self): 

    # This is necessary to prevent the angle 
    # from getting too large or small 
    self.body.SetAngle(self.body.angle % 2*pi) 
+1

为什么这个问题标记C++?它与这种语言有关吗? –

+0

pybox2d实际上是一个名为Box2D的C++库的绑定。 – Bill

+0

我使用pybox2d 2.3.1版本,但2.1.0手册,所以我不知道它是否过时。 – Bill

看起来像自从这些文档被写入以后,库已经被python化了。角度是身体的一个属性:

@angle.setter 
def angle(self, angle): 
    self._xf.angle=angle 
    self._transform_updated() 

你应该能够简单的东西,如设置:

def update_outputs(self): 

    # This is necessary to prevent the angle 
    # from getting too large or small 
    self.body.angle %= 2*pi 
+0

非常感谢! (我尝试过'self.body.angle = self.body.angle%2 * pi',但这样做会降低运营商优先级陷阱...)。 – Bill