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:
Stickman.java:
Thanks.
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);
}
}
* 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;
}
}
}
* 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.