JOGL and JBOX2D

Hello, im doing a test with JBox2D with Jogamp (JOGL)

so my problem is the “Box” doesnt fallout , i made this test , reading a bunch of examples that appears to work, but not to me,


//AWT
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//OpenGL
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;

//Box2D
import org.jbox2d.dynamics.*;
import org.jbox2d.common.*;
import org.jbox2d.collision.*;
import org.jbox2d.collision.shapes.PolygonShape;

public class SimpleScene implements GLEventListener {

	private final int SCREEN_WIDTH = 800;
	private final int SCREEN_HEIGHT = 600;
	
	private GLU glu;
	
    private World world;
    
    private Body block;
    private int blockW = 50;
    private int blockH = 50;

    
    public static void main(String[] args) {
        GLProfile glp = GLProfile.getDefault();
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);

        Frame frame = new Frame("JBox2D Test");
        frame.setSize(800, 600);
        frame.add(canvas);
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        canvas.addGLEventListener(new SimpleScene());
        
        FPSAnimator animator = new FPSAnimator(canvas, 60);
        animator.add(canvas);
        animator.start();

    }
    
    public SimpleScene(){
        //InitPhysx
        initPhysx();

    }
    
    private void initPhysx(){
    	 Vec2 gravity = new Vec2(0.f,-9.8f);
         world = new World(gravity, true);
         world.setGravity(gravity);
         
         createBlock();
    }

    private void createBlock(){
        BodyDef blockDef;
        PolygonShape blockShape;

        blockDef = new BodyDef();
        blockDef.type = BodyType.DYNAMIC;
        blockDef.position.set(100, 100);
        

        blockShape = new PolygonShape();
        blockShape.setAsBox(blockW, blockH);
      
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = blockShape;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.8f;
        fixtureDef.restitution = 0.3f;
        
    
        block = world.createBody(blockDef);
        block.createFixture(fixtureDef);
        block.setType(BodyType.DYNAMIC);
       
    }
    
    @Override
    public void display(GLAutoDrawable drawable) {
    	
        update();
        render(drawable);
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void init(GLAutoDrawable drawable) {
    	
    	GL2 gl = drawable.getGL().getGL2();
        glu = new GLU();
        
        establishProjectionMatrix(gl,SCREEN_WIDTH,SCREEN_HEIGHT);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);        
        gl.glEnable(GL2.GL_TEXTURE_2D);
        
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
    	  GL2 gl = drawable.getGL().getGL2();
    	  establishProjectionMatrix(gl,SCREEN_WIDTH,SCREEN_HEIGHT);
    	  
    }

    public void establishProjectionMatrix(GL2 gl,int width,int height){

    	gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();

        glu.gluOrtho2D(0,width,height,0);
    }
    
    private void update() {
    	world.step(1/60, 8, 1);

    }

    private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear( GL2.GL_COLOR_BUFFER_BIT );
        gl.glMatrixMode( GL2.GL_MODELVIEW );
        gl.glLoadIdentity();
      
        //RED
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        
        for (Body body = world.getBodyList(); body != null; body = body.getNext()) {
        	
        	 Vec2 p = body.getPosition();
        	 float angle = (float) Math.toDegrees(body.getAngle());
        	
        	 
        	  drawQuad(gl,p.x,p.y,blockW,blockH,angle);
         	  System.out.println(body.getPosition() +" " + world.getGravity());
        }
    }
    
   private void drawLine(GL2 gl, float x1,float y1, float x2, float y2){
       
       gl.glBegin(GL.GL_LINES);
	   	gl.glVertex2f((x1), (y1));
	   	gl.glVertex2f((x2), (y2));
	   gl.glEnd();

   }
   
   private void drawQuad(GL2 gl, float x,float y, float w, float h,float angle){
       
	   gl.glTranslatef(x, y, 0.0f); 
	   gl.glRotatef(angle, 0, 0, 1);
       //gl.glBindTexture(GL.GL_TEXTURE_2D, textures[3]);  
	   
       gl.glBegin(GL2.GL_QUADS);  
       	  gl.glTexCoord2f(0.0f,0.0f); gl.glVertex2f(-w, h);
	      gl.glTexCoord2f(0.0f,1.0f); gl.glVertex2f(-w,-h);
	  	  gl.glTexCoord2f(1.0f,1.0f); gl.glVertex2f( w,-h);
	      gl.glTexCoord2f(1.0f,0.0f); gl.glVertex2f( w, h); 
       gl.glEnd();        
           

   }
   


}

Are you having problems drawing the box or having the box move? Is it a problem with JOGL or JBox2D?

my problem is moving the box, it supposed to fall down just with that set

Hi!

This line is wrong in my humble opinion:

world.step(1/60, 8, 1);

Rather do:

world.step(1.0f/60.0f, 8, 1);

I read this in the manual:

[quote]float32 timeStep = 1.0f / 60.0f;
[/quote]

oh Thx! gouessej that actually works! , and i realized that the gravity was inverted too xD

You’re welcome. Actually, 1 / 60 is equal to 0 as the division is performed on integer values, that’s why your world was on the same step.