Body doesn't move/fall?

Hey everyone, I'm attempting to start using JBox2D with little success at the moment, if anyone could tell me what I'm doing wrong here I would really appreciate it.

I think I'm doing MOST things right, but the "Stickman" Body (actually just a ball right now, haha) just stays suspended in midair and doesn't fall even though it isn't on anything and the gravity has been set. Nothing happens even when I apply force to the Body.

Well here's the code; update() in GameState is called every frame.

GameState.java:
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sync.test;

import org.jbox2d.collision.AABB;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Michael.Su2
 */
public class GameState extends BasicGameState {

   private World world;
   private Stickman player;

   public GameState() {

   }

   public int getID() {
      return 1;
   }

   public void init(GameContainer gc, StateBasedGame sbg) {
      try {
         ImageCache.load();
      } catch (Exception e) {
         Logger.log("Error loading images: " + e, e);
      }
      world = new World(new AABB(new Vec2(0, 0), new Vec2(800, 500)),
            new Vec2(0, -10), true);
      player = new Stickman(world.createDynamicBody(new Stickman.StickBody()));
   }

   public void update(GameContainer gc, StateBasedGame sbg, int delta) {
      world.step(delta / 1000, 1);
      player.update(gc.getInput(), delta);
   }

   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {
      player.render(g);
   }
}


Stickman.java:
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sync.test;

import org.jbox2d.collision.CircleDef;
import org.jbox2d.collision.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;

/**
 *
 * @author Michael.Su2
 */
public class Stickman {

   private Body body;
   private CircleShape circle;

   private Image sprite;
   private float x;
   private float y;

   public Stickman(Body b) {
      body = b;
      circle = (CircleShape) body.createShape(new StickDef());
      body.setMassFromShapes();
   }

   public void update(Input i, int delta) {
      float move = 0;
      if (i.isKeyDown(Input.KEY_LEFT))
         move = -0.3f * delta;
      if (i.isKeyDown(Input.KEY_RIGHT))
         move = 0.3f * delta;
      if (move != 0) {
         body.applyForce(new Vec2(move, 0), body.getPosition());
      }
   }

   public void render(Graphics g) {
      float d = circle.getRadius() * 2;
      g.drawOval(circle.m_localPosition.x, circle.m_localPosition.y, d, d);
   }

   public static class StickBody extends BodyDef {

      public StickBody() {
         super();
         allowSleep = false;
         fixedRotation = true;
         position = new Vec2(100, 400);
      }
   }

   private class StickDef extends CircleDef {

      private StickDef() {
         density = 2;
         localPosition = body.getPosition();
         radius = 15;
      }
   }
}


Thanks.
found it right away, when you update you do 1 / 1000, which is 0. instead do delta *1f/1000 or (float) delta / 1000 , etc

also, pretty big coordinate plane and object, if you check in the readme files I think the engine runs the best with objects between .1 and 10 units in size.

let me know if that helps
toucansam wrote:
found it right away, when you update you do 1 / 1000, which is 0. instead do delta *1f/1000 or (float) delta / 1000 , etc


Good point, haha. Except that still didn't fix my problem :/ I even tried getting rid of the /1000 completely (so it's supposed to be 1,000 times faster) but it's still stuck. The ball just hangs there doing absolutely nothing at all.

toucansam wrote:
also, pretty big coordinate plane and object, if you check in the readme files I think the engine runs the best with objects between .1 and 10 units in size.


Not quite sure how to do that, if you could provide a small example that'd be awesome (I live off examples 😀) Don't think this is the reason that the stickman/ball is hanging though.
I really can't spot anything. I would run a debugger with a breakpoint at your update() call to make sure it's getting called.... also, what color is the ball drawn as?

When using the debugger check the mass of the ball, make sure it's > 0 (that means something messed up with density/mass)

Also check to see if the position changes between calls. Maybe you just aren't redrawing.

When I said the between .1 and 10, that means if you have a ball, it's best if the diameter is between .1 and 10 units. It doesn't matter that much, but that's what the engine is optimized for. It wouldn't make the ball freeze though, so I'm not sure.
Looks like in your render you're using the shape's local coordinates, not the body's. Been months since I used JBox, but I believe the shape's local translation is just a relative offset from the center of the body. So even if the body's translation changes the shape's wouldn't. Rendering the individual shapes can be tricky if there are more than one per body (i.e. with one per body you can cheat and just use the body's info).
Edge wrote:
Looks like in your render you're using the shape's local coordinates, not the body's. Been months since I used JBox, but I believe the shape's local translation is just a relative offset from the center of the body. So even if the body's translation changes the shape's wouldn't. Rendering the individual shapes can be tricky if there are more than one per body (i.e. with one per body you can cheat and just use the body's info).


Yeah, now I'm noticing that you're also setting the localposition of the shape to the position of the body. the localposition should just be 0,0 for a single shape.

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