How do I keep the angle of a body in pybox2d between -pi and pi?

限于喜欢 提交于 2019-12-11 07:34:49

问题


In the pybox2d manual it states the following:

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).

However, when I try to implement something to 'normalize' the angle I get the following error:

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

Code snippet:

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:


Looks like the library has been pythonized since those docs were written. angle is a property of Body:

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

You should be able to simply set it with something like:

def update_outputs(self):

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


来源:https://stackoverflow.com/questions/41419503/how-do-i-keep-the-angle-of-a-body-in-pybox2d-between-pi-and-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!