Help, object not falling from gravity

I need some help getting JBox2D to work. ive been trying to get this to work for a few days now. my problem is that the ball that i create isn't falling from gravity. heres the code that creates it and renderes it

Code:
package objects;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.lwjgl.opengl.GL11;

import util.GLColor;
import util.Location;
import util.Size;

public class PlayerCircle extends DefaultObject{


   GLColor c = new GLColor(25,255,255);

   
   
   public PlayerCircle(Location l,Size s){
      super(l,s);
      load(l.getX(),l.getY());
   }
   
    BodyDef bd = new BodyDef();
    FixtureDef fd = new FixtureDef();
    public static CircleShape  cd = new CircleShape();
   
   public void load(double x, double y){


         bd.type = BodyType.DYNAMIC;
        // bd.position = new Vec2(0.0f, -10.0f);
         cd.m_radius = s.getRadius()/Main.Main.scaleX;
        
         //cd.m_p.set((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
         bd.position = new Vec2((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);
         fd.friction = .1f;
         fd.density = 2f;
         fd.shape = cd;
        
         Main.Main.getWorld().createBody(bd).createFixture(fd);
        // bd.allowSleep = false;
   }
   
   public void draw(){
      if(bd.awake){
         c = new GLColor(50,50,50);
      }
      else {
         c = new GLColor(50,0,0);
      }
      
      System.out.println(bd.position.y);      
        GL11.glColor3f(c.getR(),c.getB(),c.getG());
        double increment = 2*Math.PI/50;
        double cx = cd.m_p.x * Main.Main.scaleX - Main.Main.index ;
        double cy =  cd.m_p.y * Main.Main.scaleY ;
      //Defining radius of circle equal to 70 pixels 
        double radius = s.getRadius();
 
     for(double angle = 0; angle < 2*Math.PI; angle+=increment){
        GL11.glBegin(GL11.GL_POLYGON);
      //One vertex of each triangle is at center of circle
        GL11.glVertex2d(cx, cy);
      //Other two vertices form the periphery of the circle      
        GL11.glVertex2d(cx + Math.cos(angle)* radius, cy + Math.sin(angle)*radius);
        GL11.glVertex2d(cx + Math.cos(angle + increment)*radius, cy + Math.sin(angle + increment)*radius);
        GL11.glEnd();
     }
       
   }

}
   
   
   
   



and heres the code thats in my main loop that does physics stuff

Code:
                Vec2 gravity = new Vec2(0 , -10f);
      world = new World(gravity, true);
      
      float tempStep = 1.0f/60.0f;
      int VI = 6;
      int POSI = 2;
      int a = 0;
      
      while(!done){  //MAIN GAME LOOP

         
         world.step(tempStep, VI, POSI);

                        ... Other code
}




Any help is much appreciated, this has been giving me a headache for quite some time 😢
The problem you are seeing is because you are trying to get the position from the BodyDef and not the Body you created in the world.

The Body is the representation of the physical object where as the BodyDef is what you use to instantiate it. Think of BodyDef as a cookie cutter and the Body as the cookie. You want to know the position of the cookie don't you?

That being said, you should be interested in storing a reference to the Body within your user defined object instead of the BodyDef (the FixtureDef is also useless IMHO, if you want a reference to the fixture then do so, but don't use the FixtureDef it is just a cookie cutter!).

You class will look like the following instead:

Code:
package objects;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.FixtureDef;
import org.lwjgl.opengl.GL11;

import util.GLColor;
import util.Location;
import util.Size;

public class PlayerCircle extends DefaultObject
{

    GLColor c = new GLColor( 25, 255, 255 );

    public PlayerCircle( Location l, Size s )
    {
        super( l, s );
        load( l.getX(), l.getY() );
    }

    // BodyDef bd = new BodyDef();
    // FixtureDef fd = new FixtureDef();
    Body body;
    Fixture fixture;

    // public static CircleShape cd = new CircleShape();

    public void load( double x, double y )
    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DYNAMIC;
        // bd.position = new Vec2(0.0f, -10.0f);

        CircleShape cd = new CircleShape();
        cd.m_radius = s.getRadius() / Main.Main.scaleX;

        // cd.m_p.set((float)x/Main.Main.scaleX,(float)y/Main.Main.scaleY);

        // Think about creating a conversion function in Main.Main
        // Something like "Vec2 getWorldCoordinate( Vec2 sceenCoorinate )"
        bd.position = new Vec2( (float) x / Main.Main.scaleX, (float) y
                / Main.Main.scaleY );

        // Attach the user data to the body so that you can know what
        // PlayerCircle the body represents when doing collision detection
        bd.userData = this;

        FixtureDef fd = new FixtureDef();
        fd.friction = .1f;
        fd.density = 2f;
        fd.shape = cd;

        body = Main.Main.getWorld().createBody( bd );
        fixture = body.createFixture( fd );
        // bd.allowSleep = false;
    }

    public void draw()
    {
        if( body.isAwake() )
        {
            c = new GLColor( 50, 50, 50 );
        }
        else
        {
            c = new GLColor( 50, 0, 0 );
        }

        System.out.println( body.getPosition().y );

        CircleShape cd = (CircleShape) fixture.getShape();

        GL11.glColor3f( c.getR(), c.getB(), c.getG() );
        double increment = 2 * Math.PI / 50;

        // Again you should be using functions to perform the conversion between
        // screen and world coordinates.

        // And as a heads up. These positions are currently set to 0 since you
        // didn't set them in your load function. You should be interested in
        // the body position instead i.e. body.getPosition();
        double cx = cd.m_p.x * Main.Main.scaleX - Main.Main.index;
        double cy = cd.m_p.y * Main.Main.scaleY;
       
        // More like this
        // Vec2 position = body.getPosition();
        // double cx = position.x * Main.Main.scaleX;
        // double cy = position.y * Main.Main.scaleY;
       
        // Defining radius of circle equal to 70 pixels
        double radius = s.getRadius();

        // Big frowny face here :(. Try not to do cosines this often...
        for( double angle = 0; angle < 2 * Math.PI; angle += increment )
        {
            GL11.glBegin( GL11.GL_POLYGON );
            // One vertex of each triangle is at center of circle
            GL11.glVertex2d( cx, cy );
            // Other two vertices form the periphery of the circle
            GL11.glVertex2d( cx + Math.cos( angle ) * radius,
                    cy + Math.sin( angle ) * radius );
            GL11.glVertex2d( cx + Math.cos( angle + increment ) * radius, cy
                    + Math.sin( angle + increment ) * radius );
            GL11.glEnd();
        }

    }

}


Keep in mind I have not tried to correct your rendering function. Nor have I tested this code. But it should work.

This following might interest you.

Code:
// Create a body using a previously defined BodyDef.
Body body = world.createBody(bodyDef);

// Get the body's current position.
body.getPosition();

// Get the body's current angle.
body.getAngle();

// Check if the body is awake.
body.isAwake()

// You can also get the bodies fixtures by doing something like so.

Fixture fixture = body.getFixtureList()

// Iterate through fixtures
while( fixture != null )
{
  // get fixture's shape
  Shape shape = fixture.getShape();

  // get next fixture
  fixture = fixture.getNext();
}


Also you might want to look into tips for rendering using OpenGL. The matrix stack is your friend and you best find out how to utilize it best. But since this is above and beyond what your question asks I will shy away from saying more.

And there is always this to read: http://www.box2d.org/manual.html

If some how this isn't quite straight forward enough please feel free to ask more questions.
Ok cool, im testing this now. This is my first project in both JBox2D and lwjgl. Ill post the results in a minute.

EDIT: Ok cool, that worked perfectly. Now how to make collisions work 😕
Ok, So now i have gravity and collisions working corectly, now my problem is that my ball dosent bounce. It just falls and hits the ground and turns red (meaning its asleep)

Any help with this would be much appreciated

Thanks
Check the restitution you have given the fixture, it should be more than zero if you want it to bounce.
Ive tried settings it to everything from .001 to 50 and i get the same results
How fast is the ball moving when it hits the ground? Maybe it is too slow to be treated as a bounce.
See here the question about "Why do very slow collisions cause no rebound?"
http://www.iforce2d.net/b2dtut/gotchas
check to see if you defined the points of your ground body in the right direction

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