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
and heres the code thats in my main loop that does physics stuff
Any help is much appreciated, this has been giving me a headache for quite some time 😢
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();
}
}
}
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
}
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 😢