@@ -41,10 +41,12 @@ Box2DPhysicsEngine::Box2DPhysicsEngine (const WorldModel &worldModel
4141 , const QList<RobotModel *> &robots)
4242 : PhysicsEngineBase(worldModel, robots)
4343 , mPixelsInCm(worldModel.pixelsInCm() * scaleCoeff)
44- , mWorld(new b2World(b2Vec2(0 , 0 )))
45- , mPrevPosition(b2Vec2(0 , 0 ))
44+ , mPrevPosition(b2Vec2{0 , 0 })
4645 , mPrevAngle(0 )
4746{
47+ b2WorldDef worldDef = b2DefaultWorldDef ();
48+ worldDef.gravity = b2Vec2{0 , 0 };
49+ mWorldId = b2CreateWorld (&worldDef);
4850 connect (&worldModel, &model::WorldModel::wallAdded,
4951 this , [this ](const QSharedPointer<QGraphicsItem> &i) {itemAdded (i.data ());});
5052 connect (&worldModel, &model::WorldModel::skittleAdded,
@@ -58,15 +60,16 @@ Box2DPhysicsEngine::Box2DPhysicsEngine (const WorldModel &worldModel
5860Box2DPhysicsEngine::~Box2DPhysicsEngine (){
5961 qDeleteAll (mBox2DRobots );
6062 qDeleteAll (mBox2DResizableItems );
63+ b2DestroyWorld (mWorldId );
64+ mWorldId = b2_nullWorldId;
6165}
6266
6367QVector2D Box2DPhysicsEngine::positionShift (model::RobotModel &robot) const
6468{
6569 if (!mBox2DRobots .contains (&robot)) {
6670 return QVector2D ();
6771 }
68-
69- return QVector2D (positionToScene (mBox2DRobots [&robot]->getBody ()->GetPosition () - mPrevPosition ));
72+ return QVector2D (positionToScene (b2Body_GetPosition (mBox2DRobots [&robot]->getBodyId ()) - mPrevPosition ));
7073}
7174
7275qreal Box2DPhysicsEngine::rotation (model::RobotModel &robot) const
@@ -75,15 +78,20 @@ qreal Box2DPhysicsEngine::rotation(model::RobotModel &robot) const
7578 return 0 ;
7679 }
7780
78- return angleToScene (mBox2DRobots [&robot]->getBody ()->GetAngle () - mPrevAngle );
81+ auto angle = b2Rot_GetAngle (b2Body_GetRotation (mBox2DRobots [&robot]->getBodyId ()));
82+ return angleToScene (angle - mPrevAngle );
7983}
8084
8185void Box2DPhysicsEngine::onPressedReleasedSelectedItems (bool active)
8286{
8387 for (auto *item : mScene ->selectedItems ()) {
8488 Box2DItem *bItem = mBox2DDynamicItems .value (item, nullptr );
8589 if (bItem) {
86- bItem->getBody ()->SetEnabled (active);
90+ if (active) {
91+ b2Body_Enable (bItem->getBodyId ());
92+ } else {
93+ b2Body_Disable (bItem->getBodyId ());
94+ }
8795 }
8896 }
8997}
@@ -98,8 +106,9 @@ void Box2DPhysicsEngine::addRobot(model::RobotModel * const robot)
98106 PhysicsEngineBase::addRobot (robot);
99107 addRobot (robot, robot->robotCenter (), robot->rotation ());
100108
101- mPrevPosition = mBox2DRobots [robot]->getBody ()->GetPosition ();
102- mPrevAngle = mBox2DRobots [robot]->getBody ()->GetAngle ();
109+ auto bodyId = mBox2DRobots [robot]->getBodyId ();
110+ mPrevPosition = b2Body_GetPosition (bodyId);
111+ mPrevAngle = b2Rot_GetAngle (b2Body_GetRotation (bodyId));
103112
104113 connect (robot, &model::RobotModel::positionChanged, this , [&] (const QPointF &newPos) {
105114 onRobotStartPositionChanged (newPos, dynamic_cast <model::RobotModel *>(sender ()));
@@ -202,14 +211,14 @@ void Box2DPhysicsEngine::onRecoverRobotPosition(const QPointF &pos)
202211{
203212 clearForcesAndStop ();
204213
205- auto stop = [=](b2Body *body ){
206- body-> SetAngularVelocity ( 0 );
207- body-> SetLinearVelocity ( {0 , 0 });
214+ auto stop = [=](b2BodyId bodyId ){
215+ b2Body_SetAngularVelocity (bodyId, 0 );
216+ b2Body_SetLinearVelocity (bodyId, {0 , 0 });
208217 };
209218
210- stop (mBox2DRobots .first ()->getBody ());
211- stop (mBox2DRobots .first ()->getWheelAt (0 )->getBody ());
212- stop (mBox2DRobots .first ()->getWheelAt (1 )->getBody ());
219+ stop (mBox2DRobots .first ()->getBodyId ());
220+ stop (mBox2DRobots .first ()->getWheelAt (0 )->getBodyId ());
221+ stop (mBox2DRobots .first ()->getWheelAt (1 )->getBodyId ());
213222
214223 onMouseReleased (pos, mBox2DRobots .keys ().first ()->startPositionMarker ()->rotation ());
215224}
@@ -225,15 +234,14 @@ void Box2DPhysicsEngine::removeRobot(model::RobotModel * const robot)
225234
226235void Box2DPhysicsEngine::recalculateParameters (qreal timeInterval)
227236{
228- const int velocityIterations = 10 ;
229- const int positionIterations = 6 ;
237+ const int subStepCount = 10 ;
230238
231239 model::RobotModel * const robot = mRobots .first ();
232240 if (!mBox2DRobots [robot]) {
233241 return ;
234242 }
235243
236- b2Body *rBody = mBox2DRobots [robot]->getBody ();
244+ b2BodyId rBodyId = mBox2DRobots [robot]->getBodyId ();
237245 float secondsInterval = timeInterval / 1000 .0f ;
238246
239247 if (mBox2DRobots [robot]->isStopping ()){
@@ -244,7 +252,7 @@ void Box2DPhysicsEngine::recalculateParameters(qreal timeInterval)
244252 const qreal speed1 = pxToM (wheelLinearSpeed (*robot, robot->leftWheel ())) / secondsInterval * sAdpt ;
245253 const qreal speed2 = pxToM (wheelLinearSpeed (*robot, robot->rightWheel ())) / secondsInterval * sAdpt ;
246254
247- if (qAbs (speed1) + qAbs (speed2) < b2_epsilon ) {
255+ if (qAbs (speed1) + qAbs (speed2) < FLT_EPSILON ) {
248256 mBox2DRobots [robot]->stop ();
249257 mLeftWheels [robot]->stop ();
250258 mRightWheels [robot]->stop ();
@@ -255,11 +263,11 @@ void Box2DPhysicsEngine::recalculateParameters(qreal timeInterval)
255263 }
256264 }
257265
258- mPrevPosition = rBody->GetPosition ();
259- mPrevAngle = rBody->GetAngle ();
260266
261- mWorld ->Step (secondsInterval, velocityIterations, positionIterations);
267+ mPrevPosition = b2Body_GetPosition (rBodyId);
268+ mPrevAngle = b2Rot_GetAngle (b2Body_GetRotation (rBodyId));
262269
270+ b2World_Step (mWorldId , secondsInterval, subStepCount);
263271 static volatile auto sThisFlagHelpsToAvoidClangError = true ;
264272 if (" If you want debug BOX2D, fix this expression to be false" && sThisFlagHelpsToAvoidClangError ) {
265273 return ;
@@ -286,8 +294,12 @@ void Box2DPhysicsEngine::recalculateParameters(qreal timeInterval)
286294 }
287295 }
288296
289- qreal angleRobot= angleToScene (mBox2DRobots [robot]->getBody ()->GetAngle ());
290- QPointF posRobot = positionToScene (mBox2DRobots [robot]->getBody ()->GetPosition ());
297+ auto bodyId = mBox2DRobots [robot]->getBodyId ();
298+ auto angle = b2Rot_GetAngle (b2Body_GetRotation (bodyId));
299+ auto position = b2Body_GetPosition (bodyId);
300+
301+ qreal angleRobot = angleToScene (angle);
302+ QPointF posRobot = positionToScene (position);
291303 QGraphicsRectItem *rect1 = new QGraphicsRectItem (-25 , -25 , 60 , 50 );
292304 QGraphicsRectItem *rect2 = new QGraphicsRectItem (-10 , -6 , 20 , 10 );
293305 QGraphicsRectItem *rect3 = new QGraphicsRectItem (-10 , -6 , 20 , 10 );
@@ -296,11 +308,20 @@ void Box2DPhysicsEngine::recalculateParameters(qreal timeInterval)
296308 rect1->setRotation (angleRobot);
297309 rect1->setPos (posRobot);
298310 rect2->setTransformOriginPoint (0 , 0 );
299- rect2->setRotation (angleToScene (mBox2DRobots [robot]->getWheelAt (0 )->getBody ()->GetAngle ()));
300- rect2->setPos (positionToScene (mBox2DRobots [robot]->getWheelAt (0 )->getBody ()->GetPosition ()));
311+
312+ auto wheelBodyId = mBox2DRobots [robot]->getWheelAt (0 )->getBodyId ();
313+ auto wheelBodyAngle = b2Rot_GetAngle (b2Body_GetRotation (wheelBodyId));
314+ auto wheelBodyPosition = b2Body_GetPosition (wheelBodyId);
315+
316+ auto wheel1BodyId = mBox2DRobots [robot]->getWheelAt (1 )->getBodyId ();
317+ auto wheel1BodyAngle = b2Rot_GetAngle (b2Body_GetRotation (wheel1BodyId));
318+ auto wheel1BodyPosition = b2Body_GetPosition (wheel1BodyId);
319+
320+ rect2->setRotation (angleToScene (wheelBodyAngle));
321+ rect2->setPos (positionToScene (wheelBodyPosition));
301322 rect3->setTransformOriginPoint (0 , 0 );
302- rect3->setRotation (angleToScene (mBox2DRobots [robot]-> getWheelAt ( 1 )-> getBody ()-> GetAngle () ));
303- rect3->setPos (positionToScene (mBox2DRobots [robot]-> getWheelAt ( 1 )-> getBody ()-> GetPosition () ));
323+ rect3->setRotation (angleToScene (wheel1BodyAngle ));
324+ rect3->setPos (positionToScene (wheel1BodyPosition ));
304325 mScene ->addItem (rect1);
305326 mScene ->addItem (rect2);
306327 mScene ->addItem (rect3);
@@ -319,7 +340,7 @@ void Box2DPhysicsEngine::recalculateParameters(qreal timeInterval)
319340
320341 const QMap<const view::SensorItem *, Box2DItem *> sensors = mBox2DRobots [robot]->getSensors ();
321342 for (Box2DItem * sensor : sensors.values ()) {
322- const b2Vec2 position = sensor->getBody ()-> GetPosition ( );
343+ const b2Vec2 position = b2Body_GetPosition ( sensor->getBodyId () );
323344 QPointF scenePos = positionToScene (position);
324345 path.addEllipse (scenePos, 10 , 10 );
325346 }
@@ -348,7 +369,8 @@ void Box2DPhysicsEngine::wakeUp()
348369void Box2DPhysicsEngine::nextFrame ()
349370{
350371 for (QGraphicsItem *item : mBox2DDynamicItems .keys ()) {
351- if (mBox2DDynamicItems [item]->getBody ()->IsEnabled () && mBox2DDynamicItems [item]->angleOrPositionChanged ()) {
372+ auto isEnabled = b2Body_IsEnabled (mBox2DDynamicItems [item]->getBodyId ());
373+ if (isEnabled && mBox2DDynamicItems [item]->angleOrPositionChanged ()) {
352374 QPointF scenePos = positionToScene (mBox2DDynamicItems [item]->getPosition ());
353375 item->setPos (scenePos - item->boundingRect ().center ());
354376 item->setRotation (angleToScene (mBox2DDynamicItems [item]->getRotation ()));
@@ -358,11 +380,10 @@ void Box2DPhysicsEngine::nextFrame()
358380
359381void Box2DPhysicsEngine::clearForcesAndStop ()
360382{
361- mWorld ->ClearForces ();
362383 for (auto item : mBox2DDynamicItems ) {
363- b2Body *body = item->getBody ();
364- body-> SetLinearVelocity ( {0 , 0 });
365- body-> SetAngularVelocity ( 0 );
384+ b2BodyId bodyId = item->getBodyId ();
385+ b2Body_SetLinearVelocity (bodyId, {0 , 0 });
386+ b2Body_SetAngularVelocity (bodyId, 0 );
366387 }
367388}
368389
@@ -449,9 +470,9 @@ void Box2DPhysicsEngine::itemRemoved(QGraphicsItem * const item)
449470 mBox2DDynamicItems .remove (item);
450471}
451472
452- b2World & Box2DPhysicsEngine::box2DWorld ()
473+ b2WorldId Box2DPhysicsEngine::box2DWorldId ()
453474{
454- return * mWorld . data () ;
475+ return mWorldId ;
455476}
456477
457478float Box2DPhysicsEngine::pxToCm (qreal px) const
@@ -461,7 +482,7 @@ float Box2DPhysicsEngine::pxToCm(qreal px) const
461482
462483b2Vec2 Box2DPhysicsEngine::pxToCm (const QPointF &posInPx) const
463484{
464- return b2Vec2 ( pxToCm (posInPx.x ()), pxToCm (posInPx.y ())) ;
485+ return b2Vec2{ pxToCm (posInPx.x ()), pxToCm (posInPx.y ())} ;
465486}
466487
467488qreal Box2DPhysicsEngine::cmToPx (float cm) const
@@ -492,7 +513,7 @@ QPointF Box2DPhysicsEngine::positionToScene(b2Vec2 boxCoords) const
492513
493514QPointF Box2DPhysicsEngine::positionToScene (float x, float y) const
494515{
495- b2Vec2 invertedCoords = b2Vec2 ( x, -y) ;
516+ b2Vec2 invertedCoords = b2Vec2{ x, -y} ;
496517 return cmToPx (100 .0f * invertedCoords);
497518}
498519
0 commit comments