How to make dynamicbody fall due to world's gravity?

Hi, I've built some code that contains box2d and sfml code.

Here's a situation I would like to make: A dynamic body that continuously falls with a sprite tracking and following it's position as it falls.

I've managed to brew up some code, but it contains an issue: The dynamic body won't fall automatically in the world. I've tried to see the issue by typing code that shows the current position of the body, and it constantly shows that the body is stuck in the same position.


Here is some of my code:

Code:
#define SFML_STATIC

#include<iostream>
#include<SFML\System.hpp>
#include<SFML\Graphics.hpp>
#include<SFML\Window\Input.hpp>


#include <Box2D\Box2D.h>
#include <cstdio>
using namespace std;
using namespace sf;
using namespace sf::Key;

int main()
{
   
   
   
   
   
   //SFML Window Stuff
   VideoMode Vmode(800,700,32);
   RenderWindow Window(Vmode, "I am Awesome.");
   
      
   float x = 400.0;
   float y = 300.0;
   Sprite.SetPosition(x, y);
   Event Event;
   
   Clock Clock;
   float ElapsedTime = Clock.GetElapsedTime();
   Window.Draw(Sprite3);
   
   //More SFML Stuff
   Window.Display();
   
   
   //Here comes Box2D
   bool doSleep = true;
   b2Vec2 physgravity(0, -9.8f);
   int iterations = 10;
   float scale = 30; //a scale factor for meters and pixels
   float timestep = 1/60;

   

   //Creating the world,
   b2World world(physgravity);
   
   //now with the world formed, build sprite that will manipulate physics data
   sf::Image Image3;
   Image3.LoadFromFile("1up.png");
   sf::Sprite Sprite3;
   Sprite3.SetImage(Image3);
   Sprite3.SetPosition(150,150);
   
   //Physical Part   
   b2BodyDef BodyDef;
   BodyDef.type = b2_dynamicBody;
   BodyDef.position.Set(150.0f,150.0f);
   BodyDef.gravityScale = 1.0f;
   b2Body* Body = world.CreateBody(&BodyDef);
   
        //shape
   b2PolygonShape dynamicBox;
   dynamicBox.SetAsBox(1.0f,1.0f);
   
        //fixture
   b2FixtureDef fixtureDef;
   fixtureDef.shape = &dynamicBox;
   fixtureDef.density = 1.0f;
   fixtureDef.friction = 0.3f;
   Body->CreateFixture(&fixtureDef);

   

   b2Vec2 pos =Body->GetPosition();

   while(Window.IsOpened())
   
   {
      
      pos = Body->GetPosition();
      Window.SetFramerateLimit(30);
      
      Clock.GetElapsedTime();
   
      
      
      
      
      //re-animation sequence
      Window.Clear();
      Sprite3.SetPosition(pos.x,pos.y);
      Window.Draw(Sprite3);
      Window.Display();   
      
      cout << pos.x << "    " << pos.y << endl;
      }
      
      
      
   return 0;
   }


Note: The sprite that is supposed to be affected by the dynamicbody is Sprite3.
- values of pos.x and pos.y do NOT change



How do I fix my code? How can I make the dynamic body finally FALL due to the world's gravity? Cheers.

PS: I use C++
and the version of Box2D i'm using is v2.2.1
What line of this code are you expecting to make the body move??
irresistible force wrote:
What line of this code are you expecting to make the body move??


I thought that I could make the sprite move by resetting it's location according to the location of the dynamicbody, which is covered by the b2Vec2 called pos

Code:
//re-animation sequence
      Window.Clear();
      Sprite3.SetPosition(pos.x,pos.y);
      Window.Draw(Sprite3);
      Window.Display();   


pos is a b2vec2 that updates itself with the location of the falling body, the code where it updates is somewhere near the top of the window loop.
Actually I was hoping that would give you a hint 😉
Check out the hello world example, you will see that you need to call Step on the Box2D world for anything to happen.

http://code.google.com/p/box2d/source/b ... oWorld.cpp
irresistible force wrote:
Actually I was hoping that would give you a hint 😉
Check out the hello world example, you will see that you need to call Step on the Box2D world for anything to happen.

http://code.google.com/p/box2d/source/b ... oWorld.cpp


wow that was it!!!! Thanks for the push!!!

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