Car body's angle not changing box2D C++












0















i'm making a side-scrolling car game with box2D.
I'm currently working on the car and it seems that i'm stuck.
The chassis of my car isn't rotating when for example the car is trying to climb a hill. I don't know if its normal or if i should set the angle of the body.
Here's a quick video that shows the problem : https://streamable.com/d802n



This is my code :



b2BodyDef carBox = b2BodyDef();
carBox.position = b2Vec2(bodyCenterPosition.x, bodyCenterPosition.y);
carBox.type = b2_dynamicBody;
car = game->getWorld()->CreateBody(&carBox);

b2PolygonShape carPolygon = b2PolygonShape();
carPolygon.SetAsBox(bodySize.x, bodySize.y);
b2FixtureDef carFix = b2FixtureDef();
carFix.density = 0.0f;
carFix.shape = &carPolygon;
car->CreateFixture(&carFix);

b2PolygonShape headPolygon = b2PolygonShape();
headPolygon.SetAsBox(headSize.x, headSize.y);
for (int i = 0; i < 8; i++) {
headPolygon.m_vertices[i].x -= 8.0f / RATIO;
headPolygon.m_vertices[i].y -= 24.0f / RATIO;
}
b2FixtureDef headFix = b2FixtureDef();
headFix.density = 0.0f;
headFix.shape = &headPolygon;
car->CreateFixture(&headFix);


b2CircleShape circleShape;
circleShape.m_radius = 0.35f;
circleShape.m_p.SetZero();

b2FixtureDef fd;
fd.shape = &circleShape;
fd.density = 1.0f;
fd.friction = 0.9f;

b2BodyDef wheel1Def;
wheel1Def.type = b2_dynamicBody;
wheel1Def.position = b2Vec2(backWheelCenterPosition.x, backWheelCenterPosition.y);
backWheel = game->getWorld()->CreateBody(&wheel1Def);
backWheel->CreateFixture(&fd);

b2BodyDef wheel2Def;
wheel2Def.type = b2_dynamicBody;
wheel2Def.position = b2Vec2(frontWheelCenterPosition.x, frontWheelCenterPosition.y);
frontWheel = game->getWorld()->CreateBody(&wheel2Def);
frontWheel->CreateFixture(&fd);

b2WheelJointDef springDef1;
springDef1.dampingRatio = 50.0f;
springDef1.maxMotorTorque = 1.0f;
springDef1.frequencyHz = 15.0f;
springDef1.motorSpeed = 0.0f;
springDef1.enableMotor = true;
springDef1.Initialize(car, backWheel, backWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
backSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);

springDef1.Initialize(car, frontWheel, frontWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
frontSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);









share|improve this question



























    0















    i'm making a side-scrolling car game with box2D.
    I'm currently working on the car and it seems that i'm stuck.
    The chassis of my car isn't rotating when for example the car is trying to climb a hill. I don't know if its normal or if i should set the angle of the body.
    Here's a quick video that shows the problem : https://streamable.com/d802n



    This is my code :



    b2BodyDef carBox = b2BodyDef();
    carBox.position = b2Vec2(bodyCenterPosition.x, bodyCenterPosition.y);
    carBox.type = b2_dynamicBody;
    car = game->getWorld()->CreateBody(&carBox);

    b2PolygonShape carPolygon = b2PolygonShape();
    carPolygon.SetAsBox(bodySize.x, bodySize.y);
    b2FixtureDef carFix = b2FixtureDef();
    carFix.density = 0.0f;
    carFix.shape = &carPolygon;
    car->CreateFixture(&carFix);

    b2PolygonShape headPolygon = b2PolygonShape();
    headPolygon.SetAsBox(headSize.x, headSize.y);
    for (int i = 0; i < 8; i++) {
    headPolygon.m_vertices[i].x -= 8.0f / RATIO;
    headPolygon.m_vertices[i].y -= 24.0f / RATIO;
    }
    b2FixtureDef headFix = b2FixtureDef();
    headFix.density = 0.0f;
    headFix.shape = &headPolygon;
    car->CreateFixture(&headFix);


    b2CircleShape circleShape;
    circleShape.m_radius = 0.35f;
    circleShape.m_p.SetZero();

    b2FixtureDef fd;
    fd.shape = &circleShape;
    fd.density = 1.0f;
    fd.friction = 0.9f;

    b2BodyDef wheel1Def;
    wheel1Def.type = b2_dynamicBody;
    wheel1Def.position = b2Vec2(backWheelCenterPosition.x, backWheelCenterPosition.y);
    backWheel = game->getWorld()->CreateBody(&wheel1Def);
    backWheel->CreateFixture(&fd);

    b2BodyDef wheel2Def;
    wheel2Def.type = b2_dynamicBody;
    wheel2Def.position = b2Vec2(frontWheelCenterPosition.x, frontWheelCenterPosition.y);
    frontWheel = game->getWorld()->CreateBody(&wheel2Def);
    frontWheel->CreateFixture(&fd);

    b2WheelJointDef springDef1;
    springDef1.dampingRatio = 50.0f;
    springDef1.maxMotorTorque = 1.0f;
    springDef1.frequencyHz = 15.0f;
    springDef1.motorSpeed = 0.0f;
    springDef1.enableMotor = true;
    springDef1.Initialize(car, backWheel, backWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
    backSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);

    springDef1.Initialize(car, frontWheel, frontWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
    frontSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);









    share|improve this question

























      0












      0








      0








      i'm making a side-scrolling car game with box2D.
      I'm currently working on the car and it seems that i'm stuck.
      The chassis of my car isn't rotating when for example the car is trying to climb a hill. I don't know if its normal or if i should set the angle of the body.
      Here's a quick video that shows the problem : https://streamable.com/d802n



      This is my code :



      b2BodyDef carBox = b2BodyDef();
      carBox.position = b2Vec2(bodyCenterPosition.x, bodyCenterPosition.y);
      carBox.type = b2_dynamicBody;
      car = game->getWorld()->CreateBody(&carBox);

      b2PolygonShape carPolygon = b2PolygonShape();
      carPolygon.SetAsBox(bodySize.x, bodySize.y);
      b2FixtureDef carFix = b2FixtureDef();
      carFix.density = 0.0f;
      carFix.shape = &carPolygon;
      car->CreateFixture(&carFix);

      b2PolygonShape headPolygon = b2PolygonShape();
      headPolygon.SetAsBox(headSize.x, headSize.y);
      for (int i = 0; i < 8; i++) {
      headPolygon.m_vertices[i].x -= 8.0f / RATIO;
      headPolygon.m_vertices[i].y -= 24.0f / RATIO;
      }
      b2FixtureDef headFix = b2FixtureDef();
      headFix.density = 0.0f;
      headFix.shape = &headPolygon;
      car->CreateFixture(&headFix);


      b2CircleShape circleShape;
      circleShape.m_radius = 0.35f;
      circleShape.m_p.SetZero();

      b2FixtureDef fd;
      fd.shape = &circleShape;
      fd.density = 1.0f;
      fd.friction = 0.9f;

      b2BodyDef wheel1Def;
      wheel1Def.type = b2_dynamicBody;
      wheel1Def.position = b2Vec2(backWheelCenterPosition.x, backWheelCenterPosition.y);
      backWheel = game->getWorld()->CreateBody(&wheel1Def);
      backWheel->CreateFixture(&fd);

      b2BodyDef wheel2Def;
      wheel2Def.type = b2_dynamicBody;
      wheel2Def.position = b2Vec2(frontWheelCenterPosition.x, frontWheelCenterPosition.y);
      frontWheel = game->getWorld()->CreateBody(&wheel2Def);
      frontWheel->CreateFixture(&fd);

      b2WheelJointDef springDef1;
      springDef1.dampingRatio = 50.0f;
      springDef1.maxMotorTorque = 1.0f;
      springDef1.frequencyHz = 15.0f;
      springDef1.motorSpeed = 0.0f;
      springDef1.enableMotor = true;
      springDef1.Initialize(car, backWheel, backWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
      backSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);

      springDef1.Initialize(car, frontWheel, frontWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
      frontSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);









      share|improve this question














      i'm making a side-scrolling car game with box2D.
      I'm currently working on the car and it seems that i'm stuck.
      The chassis of my car isn't rotating when for example the car is trying to climb a hill. I don't know if its normal or if i should set the angle of the body.
      Here's a quick video that shows the problem : https://streamable.com/d802n



      This is my code :



      b2BodyDef carBox = b2BodyDef();
      carBox.position = b2Vec2(bodyCenterPosition.x, bodyCenterPosition.y);
      carBox.type = b2_dynamicBody;
      car = game->getWorld()->CreateBody(&carBox);

      b2PolygonShape carPolygon = b2PolygonShape();
      carPolygon.SetAsBox(bodySize.x, bodySize.y);
      b2FixtureDef carFix = b2FixtureDef();
      carFix.density = 0.0f;
      carFix.shape = &carPolygon;
      car->CreateFixture(&carFix);

      b2PolygonShape headPolygon = b2PolygonShape();
      headPolygon.SetAsBox(headSize.x, headSize.y);
      for (int i = 0; i < 8; i++) {
      headPolygon.m_vertices[i].x -= 8.0f / RATIO;
      headPolygon.m_vertices[i].y -= 24.0f / RATIO;
      }
      b2FixtureDef headFix = b2FixtureDef();
      headFix.density = 0.0f;
      headFix.shape = &headPolygon;
      car->CreateFixture(&headFix);


      b2CircleShape circleShape;
      circleShape.m_radius = 0.35f;
      circleShape.m_p.SetZero();

      b2FixtureDef fd;
      fd.shape = &circleShape;
      fd.density = 1.0f;
      fd.friction = 0.9f;

      b2BodyDef wheel1Def;
      wheel1Def.type = b2_dynamicBody;
      wheel1Def.position = b2Vec2(backWheelCenterPosition.x, backWheelCenterPosition.y);
      backWheel = game->getWorld()->CreateBody(&wheel1Def);
      backWheel->CreateFixture(&fd);

      b2BodyDef wheel2Def;
      wheel2Def.type = b2_dynamicBody;
      wheel2Def.position = b2Vec2(frontWheelCenterPosition.x, frontWheelCenterPosition.y);
      frontWheel = game->getWorld()->CreateBody(&wheel2Def);
      frontWheel->CreateFixture(&fd);

      b2WheelJointDef springDef1;
      springDef1.dampingRatio = 50.0f;
      springDef1.maxMotorTorque = 1.0f;
      springDef1.frequencyHz = 15.0f;
      springDef1.motorSpeed = 0.0f;
      springDef1.enableMotor = true;
      springDef1.Initialize(car, backWheel, backWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
      backSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);

      springDef1.Initialize(car, frontWheel, frontWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
      frontSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);






      c++ box2d game-physics






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 20:05









      KizekoKizeko

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It is normal for a body with a fixture having a zero density to behave like an infinite mass. Sounds like that's not what you intended however.



          Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f. Set these to a positive non-zero value, like 1.0f, and the dynamic body they're created on should behave more like a physical mass would. That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well.



          For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures.



          Hope this solves the problem or at least helps.



          By the way, I loved the artwork shown in the video!






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452396%2fcar-bodys-angle-not-changing-box2d-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            It is normal for a body with a fixture having a zero density to behave like an infinite mass. Sounds like that's not what you intended however.



            Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f. Set these to a positive non-zero value, like 1.0f, and the dynamic body they're created on should behave more like a physical mass would. That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well.



            For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures.



            Hope this solves the problem or at least helps.



            By the way, I loved the artwork shown in the video!






            share|improve this answer




























              0














              It is normal for a body with a fixture having a zero density to behave like an infinite mass. Sounds like that's not what you intended however.



              Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f. Set these to a positive non-zero value, like 1.0f, and the dynamic body they're created on should behave more like a physical mass would. That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well.



              For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures.



              Hope this solves the problem or at least helps.



              By the way, I loved the artwork shown in the video!






              share|improve this answer


























                0












                0








                0







                It is normal for a body with a fixture having a zero density to behave like an infinite mass. Sounds like that's not what you intended however.



                Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f. Set these to a positive non-zero value, like 1.0f, and the dynamic body they're created on should behave more like a physical mass would. That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well.



                For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures.



                Hope this solves the problem or at least helps.



                By the way, I loved the artwork shown in the video!






                share|improve this answer













                It is normal for a body with a fixture having a zero density to behave like an infinite mass. Sounds like that's not what you intended however.



                Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f. Set these to a positive non-zero value, like 1.0f, and the dynamic body they're created on should behave more like a physical mass would. That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well.



                For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures.



                Hope this solves the problem or at least helps.



                By the way, I loved the artwork shown in the video!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 19:03









                Louis LangholtzLouis Langholtz

                1,8903824




                1,8903824
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452396%2fcar-bodys-angle-not-changing-box2d-c%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin