Search code examples
pythonbox2dopenai-gym

Can I acquire acceleration measure directly from a Box2D body? (Python) (Box2D)


I am working with OpenAI-Gym enviroment CarRacing-v0 which is based on Box2D Physics library. Through env.car.hull I have access to position, angle, velocities but not in acceleration.

Searching inside Box2D I found (but not sure) the attributes showed below and I assume that there is not direct way to have the acceleration of the car

'b2BodyDef': ['active', 'allowSleep', 'angle', 'angularDamping', 'angularVelocity', 
                             'awake', 'bullet', 'fixedRotation', 'fixtures', 
                             'inertiaScale', 'linearDamping', 'linearVelocity', 'position', 
                             'shapeFixture', 'shapes', 'type', 'userData', 
                             ],

Currently using integration of velocity to get acceleration but there must be better way through Box2D. Can someone with experience in the specific library and environment help me out. Thanks


Solution

  • There is no better way you have to do what you are already doing.

    When the step has finished, at that instantaneous moment, it makes no sense to show any average of acceleration over the previous step as you might have accelerated in one way and then the other, for example bouncing off a wall. And as for considering the individual step is iterated from

     step(float timeStep,
                     int velocityIterations,
                     int positionIterations)
    

    you might imagine setting velocityIterations and positionIterations to 1 but thats just a less accurate version of the same situation as above. Comparing -absolute- velocities against the time step is best because you want to convert the acceleration vector (has a direction) to a scalar (magnitude).