Slick2D port of JBox2D testbed

I've adapted the JBox2D testbed to use Slick2D rather than the Processing core for interaction and graphics. Details, Webstart demo, source, etc. are available here:
http://www.crofter.org/?p=5
Very cool stuff! It's great to see stuff running using OpenGL, performance is a lot better than with the software renderer!

Would it be okay with you if I added this in some form to the distribution for the next release? I had a basic Slick demo at one point, but I'm not too familiar with Slick so I never updated it or expanded on it.
ewjordan, I'd be pleased and honored to contribute this code, in whatever form makes sense, to your next release. Thanks.
I've just been notified on the Slick forums that my source code link was incorrect. I've corrected it on my blog. The correct link is http://www.crofter.org/games/jbox2d_dem ... 2d_src.jar

Apologies for any inconvenience.
I just ported the DebugDraw class from C to Java with Slick2D. Maybe somebody can use this.

Code: Select all

import org.jbox2d.collision.AABB;
import org.jbox2d.common.Color3f;
import org.jbox2d.common.Vec2;
import org.jbox2d.common.XForm;
import org.jbox2d.dynamics.DebugDraw;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.opengl.SlickCallable;

public class MyDebugDraw extends DebugDraw {

   public Graphics g;
   public static final float PI = (float) Math.PI;

   public void drawPolygon(final Vec2 vertices[], final int vertexCount, final Color3f color) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               GL11.glColor3f(color.x, color.y, color.z);
               GL11.glBegin(GL11.GL_LINE_LOOP);
               for (int i = 0; i < vertexCount; ++i) {
                  GL11.glVertex2f(vertices[i].x, vertices[i].y);
               }
               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }

   }

   public void drawSolidPolygon(final Vec2 vertices[], final int vertexCount, final Color3f color) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               GL11.glEnable(GL11.GL_BLEND);
               GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
               GL11.glColor4f(0.5f * color.x, 0.5f * color.y, 0.5f * color.z, 0.5f);
               GL11.glBegin(GL11.GL_TRIANGLE_FAN);
               for (int i = 0; i < vertexCount; ++i) {
                  GL11.glVertex2f(vertices[i].x, vertices[i].y);
               }
               GL11.glEnd();
               GL11.glDisable(GL11.GL_BLEND);

               GL11.glColor4f(color.x, color.y, color.z, 1.0f);
               GL11.glBegin(GL11.GL_LINE_LOOP);
               for (int i = 0; i < vertexCount; ++i) {
                  GL11.glVertex2f(vertices[i].x, vertices[i].y);
               }
               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void drawCircle(final Vec2 center, final float radius, final Color3f color) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               float k_segments = 16.0f;
               float k_increment = 2.0f * PI / k_segments;
               float theta = 0.0f;
               GL11.glColor3f(color.x, color.y, color.z);
               GL11.glBegin(GL11.GL_LINE_LOOP);
               for (int i = 0; i < k_segments; ++i) {
                  Vec2 v = new Vec2((float) Math.cos(theta), (float) Math.sin(theta)).mulLocal(radius).addLocal(center);
                  GL11.glVertex2f(v.x, v.y);
                  theta += k_increment;
               }
               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void drawSolidCircle(final Vec2 center, final float radius, final Vec2 axis, final Color3f color) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               float k_segments = 16.0f;
               float k_increment = 2.0f * PI / k_segments;
               float theta = 0.0f;
               GL11.glEnable(GL11.GL_BLEND);
               GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
               GL11.glColor4f(0.5f * color.x, 0.5f * color.y, 0.5f * color.z, 0.5f);
               GL11.glBegin(GL11.GL_TRIANGLE_FAN);
               for (int i = 0; i < k_segments; ++i) {
                  Vec2 v = new Vec2((float) Math.cos(theta), (float) Math.sin(theta)).mulLocal(radius).addLocal(center);
                  GL11.glVertex2f(v.x, v.y);
                  theta += k_increment;
               }
               GL11.glEnd();
               GL11.glDisable(GL11.GL_BLEND);

               theta = 0.0f;
               GL11.glColor4f(color.x, color.y, color.z, 1.0f);
               GL11.glBegin(GL11.GL_LINE_LOOP);
               for (int i = 0; i < k_segments; ++i) {
                  Vec2 v = new Vec2((float) Math.cos(theta), (float) Math.sin(theta)).mulLocal(radius).addLocal(center);
                  GL11.glVertex2f(v.x, v.y);
                  theta += k_increment;
               }
               GL11.glEnd();

               Vec2 p = new Vec2(axis.x, axis.y).mulLocal(radius).addLocal(center);
               GL11.glBegin(GL11.GL_LINES);
               GL11.glVertex2f(center.x, center.y);
               GL11.glVertex2f(p.x, p.y);
               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void drawSegment(final Vec2 p1, final Vec2 p2, final Color3f color) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               GL11.glColor3f(color.x, color.y, color.z);
               GL11.glBegin(GL11.GL_LINES);
               GL11.glVertex2f(p1.x, p1.y);
               GL11.glVertex2f(p2.x, p2.y);
               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void drawXForm(final XForm xf) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               Vec2 p1 = xf.position, p2;
               float k_axisScale = 0.4f;
               GL11.glBegin(GL11.GL_LINES);

               GL11.glColor3f(1.0f, 0.0f, 0.0f);
               GL11.glVertex2f(p1.x, p1.y);
               p2 = new Vec2(xf.R.col1.x, xf.R.col1.y).mulLocal(k_axisScale).add(p1);
               GL11.glVertex2f(p2.x, p2.y);

               GL11.glColor3f(0.0f, 1.0f, 0.0f);
               GL11.glVertex2f(p1.x, p1.y);
               p2 = new Vec2(xf.R.col2.x, xf.R.col2.y).mulLocal(k_axisScale).add(p1);
               GL11.glVertex2f(p2.x, p2.y);

               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void drawPoint(final Vec2 p, final float size, final Color3f color) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               GL11.glPointSize(size);
               GL11.glBegin(GL11.GL_POINTS);
               GL11.glColor3f(color.x, color.y, color.z);
               GL11.glVertex2f(p.x, p.y);
               GL11.glEnd();
               GL11.glPointSize(1.0f);
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public void drawString(float x, float y, String s, Color3f c) {
      g.drawString(s, x, y);
   }

   public void drawAABB(final AABB aabb, final Color3f c) {
      try {
         new SlickCallable() {

            @Override
            protected void performGLOperations() throws SlickException {
               GL11.glColor3f(c.x, c.y, c.z);
               GL11.glBegin(GL11.GL_LINE_LOOP);
               GL11.glVertex2f(aabb.lowerBound.x, aabb.lowerBound.y);
               GL11.glVertex2f(aabb.upperBound.x, aabb.lowerBound.y);
               GL11.glVertex2f(aabb.upperBound.x, aabb.upperBound.y);
               GL11.glVertex2f(aabb.lowerBound.x, aabb.upperBound.y);
               GL11.glEnd();
            }

         }.call();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

}


btw: Color3f should have r, g, b variables instead of x, y, z. It's a little confusing.
BigRob wrote:btw: Color3f should have r, g, b variables instead of x, y, z. It's a little confusing.

Yes, I agree this is not right; as for why it happened, I don't remember the exact details, but IIRC the Color3f class was dropped in as a replacement for javax.vecmath.Color3f, which I stripped out for some reason (don't remember why I used it in the first place, actually, and I also don't remember why I pulled it out, maybe license?).

There's no need to maintain compatibility with that anymore, so I'll likely change that in a future release.
The links are broken.
Anyone has any idea where one can get the files?

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