[SFML & BOX2D] A strange behavior !!!

Hi everybody 🙂
I noticed a strange behavior of my box, when the body is rotating and when I apply an impluse to it, it steel rotating.
I don't why, this is my code:

Code:

/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#include "Box2D.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdio>
#include <cmath>

// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
int main(int argc, char** argv)
{
   B2_NOT_USED(argc);
   B2_NOT_USED(argv);

   sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes");
   sf::Event Event;


   b2AABB worldAABB;
   float32 scale=10.0f;
   worldAABB.lowerBound.Set(-1000.0f, -1000.0f);
   worldAABB.upperBound.Set(1000.0f, 1000.0f);
   b2Vec2 gravity(0.0f, 10.0f);
   bool doSleep = true;
   b2World world(worldAABB, gravity, doSleep);

   b2BodyDef groundBodyDef;

   groundBodyDef.position.Set(0.0f,550/scale);
   b2Body* groundBody = world.CreateBody(&groundBodyDef);


   b2PolygonDef groundShapeDef;
   groundShapeDef.SetAsBox(400.0f/scale, 25.0f/scale);
   groundBody->CreateShape(&groundShapeDef);

   b2BodyDef bodyDef;
   bodyDef.position.Set(0.0f,50.0f/scale);
   b2Body* body = world.CreateBody(&bodyDef);

   b2BodyDef bodyDef2;
   bodyDef2.position.Set(0.0f,100.0f/scale);
   b2Body* body2 = world.CreateBody(&bodyDef2);


   b2PolygonDef shapeDef;
   shapeDef.SetAsBox(10.0f/scale, 10.0f/scale);

   b2PolygonDef shapeDef2;
   shapeDef2.SetAsBox(10.0f/scale, 10.0f/scale);

   shapeDef.density = 1.25f;
   shapeDef.friction = 0.3f;

   shapeDef2.density = 1.0f;
   shapeDef2.friction = 0.3f;

   body->CreateShape(&shapeDef);
   body->SetMassFromShapes();

   body2->CreateShape(&shapeDef2);
   body2->SetMassFromShapes();
   float32 timeStep = 1.0f / 60.0f;
   int32 iterations = 10;
   float angle=0.0f;
   float angle2=0.0f;

   sf::Shape Box=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Red);
   Box.SetCenter(10,10);
    sf::Shape Box2=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Green);
    Box2.SetCenter(10,10);



   while (App.IsOpened())
    {

        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }



        world.Step(timeStep, iterations);
        b2Vec2 position = body->GetPosition();
        angle=body->GetAngle();
        angle2=body2->GetAngle();
        b2Vec2 position2 = body2->GetPosition();


        if (App.GetInput().IsKeyDown(sf::Key::Left))  body->ApplyForce(b2Vec2(-20,0),b2Vec2(position.x,position.y));
        if (App.GetInput().IsKeyDown(sf::Key::Right)) body->ApplyForce(b2Vec2(20,0),b2Vec2(position.x,position.y));
        if (App.GetInput().IsKeyDown(sf::Key::Up))    body->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position.x,position.y));
        if (App.GetInput().IsKeyDown(sf::Key::Down))  body->ApplyImpulse(b2Vec2(0,1),b2Vec2(position.x,position.y));

        if (App.GetInput().IsKeyDown(sf::Key::Q))  body2->ApplyForce(b2Vec2(-20,0),b2Vec2(position2.x,position2.y));
        if (App.GetInput().IsKeyDown(sf::Key::D)) body2->ApplyForce(b2Vec2(20,0),b2Vec2(position2.x,position2.y));
        if (App.GetInput().IsKeyDown(sf::Key::Z))    body2->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position2.x,position2.y));
        if (App.GetInput().IsKeyDown(sf::Key::S))  body2->ApplyImpulse(b2Vec2(0,1),b2Vec2(position2.x,position2.y));

        App.Clear();
        Box.SetPosition(position.x*scale+10,position.y*scale+10);
        Box.SetRotation(-(angle*180)/M_PI);
        Box2.SetRotation(-(angle2*180)/M_PI);
        Box2.SetPosition(position2.x*scale+10,position2.y*scale+10);
        App.Draw(Box);
        App.Draw(Box2);

        App.Display();



    }

   return 0;
}




you can compile it,and try to rotate the red box then before it touch anything apply an impluse and you'll see what I'm talking about...
Do you mean it's still rotating after you apply the impulse? This is normal. Applying an impulse affects the object with the force you applied. To get your object to stop rotating, you'll need to set the angular velocity to 0.
Hi! 😀

But setting the angular velocity to zero it's also an illogical move...
i have also another question: what GetAngle() function return? and have i to divide the gravity vector by "scale"?

thanks
GetAngle returns radians and the scale is only used for displaying purposes so no you don't need to divide gravity by it.
Thanks a lot, i'll what can i do.

thanks
I have a new probleme:
i'm trying to render the Ground, compile the code to see what i'm talking about:

the code:

Code:

/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#include "Box2D.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdio>
#include <cmath>

// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
int main(int argc, char** argv)
{
   B2_NOT_USED(argc);
   B2_NOT_USED(argv);

   sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes");
   sf::Event Event;


   b2AABB worldAABB;
   float32 scale=9.0f;
   worldAABB.lowerBound.Set(-1000.0f, -1000.0f);
   worldAABB.upperBound.Set(1000.0f, 1000.0f);
   b2Vec2 gravity(0.0f, 10.0f/3);
   bool doSleep = true;
   b2World world(worldAABB, gravity, doSleep);

   b2BodyDef groundBodyDef;

   groundBodyDef.position.Set(400.0f/scale,550.0f/scale);
   b2Body* groundBody = world.CreateBody(&groundBodyDef);


   b2PolygonDef groundShapeDef;
   groundShapeDef.SetAsBox(400.0f/scale, 25.0f/scale);
   groundBody->CreateShape(&groundShapeDef);

   b2BodyDef bodyDef;
   bodyDef.position.Set(0.0f,50.0f/scale);
   b2Body* body = world.CreateBody(&bodyDef);

   b2BodyDef bodyDef2;
   bodyDef2.position.Set(2.0f,100.0f/scale);
   b2Body* body2 = world.CreateBody(&bodyDef2);


   b2PolygonDef shapeDef;
   shapeDef.SetAsBox(10.0f/scale, 10.0f/scale);

   b2PolygonDef shapeDef2;
   shapeDef2.SetAsBox(10.0f/scale, 10.0f/scale);

   shapeDef.density = 1.25f;
   shapeDef.friction = 0.3f;

   shapeDef2.density = 1.0f;
   shapeDef2.friction = 0.3f;

   body->CreateShape(&shapeDef);
   body->SetMassFromShapes();

   body2->CreateShape(&shapeDef2);
   body2->SetMassFromShapes();
   float32 timeStep = 1.0f / 60.0f;
   int32 iterations = 10;
   float angle=0.0f;
   float angle2=0.0f;

   sf::Shape Box=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Red);
   Box.SetCenter(10,10);
    sf::Shape Box2=sf::Shape::Rectangle(0, 0, 20, 20, sf::Color::Green);
    Box2.SetCenter(10,10);
    sf::Shape Gr=sf::Shape::Rectangle(0, 0, 800, 50, sf::Color::White);
    Gr.SetCenter(400,25);


   while (App.IsOpened())
    {

        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }



        world.Step(timeStep, iterations);
        b2Vec2 position = body->GetPosition();
        angle=body->GetAngle();
        angle2=body2->GetAngle();
        b2Vec2 position2 = body2->GetPosition();
        b2Vec2 position3 = groundBody->GetPosition();


        if (App.GetInput().IsKeyDown(sf::Key::Left))  body->ApplyForce(b2Vec2(-20,0),b2Vec2(position.x,position.y));
        if (App.GetInput().IsKeyDown(sf::Key::Right)) body->ApplyForce(b2Vec2(20,0),b2Vec2(position.x,position.y));
        if (App.GetInput().IsKeyDown(sf::Key::Up))    body->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position.x,position.y));
        if (App.GetInput().IsKeyDown(sf::Key::Down))  body->ApplyImpulse(b2Vec2(0,1),b2Vec2(position.x,position.y));

        if (App.GetInput().IsKeyDown(sf::Key::Q))  body2->ApplyForce(b2Vec2(-20,0),b2Vec2(position2.x,position2.y));
        if (App.GetInput().IsKeyDown(sf::Key::D)) body2->ApplyForce(b2Vec2(20,0),b2Vec2(position2.x,position2.y));
        if (App.GetInput().IsKeyDown(sf::Key::Z))    body2->ApplyImpulse(b2Vec2(0,-1),b2Vec2(position2.x,position2.y));
        if (App.GetInput().IsKeyDown(sf::Key::S))  body2->ApplyImpulse(b2Vec2(0,1),b2Vec2(position2.x,position2.y));

        App.Clear();
        Box.SetPosition(position.x*scale+10,position.y*scale+10);
        Box.SetRotation(-(angle*180)/M_PI);
        Box2.SetRotation(-(angle2*180)/M_PI);
        Box2.SetPosition(position2.x*scale+10,position2.y*scale+10);
        Gr.SetPosition(position3.x*scale,position3.y*scale);

        App.Draw(Box);
        App.Draw(Box2);
        App.Draw(Gr);

        App.Display();

    }



   return 0;
}

also how can I simulate water ???
what is the position of an object: it's the left upper point position or what??
thanks in advance
UP!!!!

😁
😐 😐
😮 😮 😮 😮
🙄 🙄 🙄 🙄 🙄 🙄
❗ ❗
❓ ❓
💡 💡
Arrow fail.

if I remember correctly, positions of bodies are reported via their origin (check the comments of GetPosition).

I don't know exactly how you can simulate water. If you take a look at nVidia's water physics demos (3d, same principle though) they used some balls that had special grouping movement. You might be able to use circles that (attempt to) match the density of water, but this is somewhat beyond Box2D, since Box2D is about rigid bodies.

-P

Original thread: https://box2d.org/forum/viewtopic.php?t=3861