java - I need the right Pixel Per Meter Ratio implementation using JBox2D - Not Using LibGDX -
i'm building game engine , i've added jbox2d.
i'm struggling implement correct way of using ppm.
i have 3 important classes magic happens.
please review code , show me change.
this i'd see working: http://imgur.com/a/effwv
however though see that, doesn't behave accordingly.
playstate.java
package enginex.jbox2dtestbed; import java.awt.graphics2d; import java.awt.point; import java.awt.event.mouseevent; import java.util.arraylist; import org.jbox2d.common.vec2; import org.jbox2d.dynamics.world; import enginex.core.state; public class playstate extends state { testgame game; boolean initialized = false; vec2 gravity = new vec2(0.0f, 0.8f); boolean dosleep = true; world world = new world(gravity, dosleep); arraylist<ball> balls = new arraylist<ball>(); box box; protected playstate(testgame game) { super(game); this.game = game; } public void postinit() { if(!initialized) { box = new box(game); initialized = true; } } public void update() { postinit(); updateworld(); for(ball b:balls) b.update(); } public void updateworld() { float timestep = 1.0f / 60.f; int velocityiterations = 6; int positioniterations = 2; for(int = 0; < 60; ++i) world.step(timestep, velocityiterations, positioniterations); } public void render(graphics2d g) { for(ball b:balls) b.render(g); box.render(g); } public void mousepressed(mouseevent e) { point m = game.getmouseposition(); try { balls.add(new ball(game, m.x, m.y)); } catch(exception ex) {} } }
ball.java
package enginex.jbox2dtestbed; import java.awt.color; import java.awt.graphics2d; import org.jbox2d.collision.shapes.circleshape; import org.jbox2d.dynamics.body; import org.jbox2d.dynamics.bodydef; import org.jbox2d.dynamics.bodytype; import org.jbox2d.dynamics.fixturedef; import enginex.core.gameobject; @suppresswarnings("serial") public class ball extends gameobject { testgame game; public double x; public double y; public int w = 20; public int h = 20; body body; bodydef bd = new bodydef(); circleshape cs = new circleshape(); fixturedef fd = new fixturedef(); public ball(testgame game, double x, double y) { super(game); this.game = game; this.x = x - w / 2; this.y = y - h / 2; bd.position.set((float)this.x, (float)this.y); bd.type = bodytype.dynamic; cs.m_radius = w; fd.shape = cs; fd.density = 0.5f; // weight fd.friction = 0.3f; // roughness fd.restitution = 0.5f; // bounciness body = getstate().world.createbody(bd); body.createfixture(fd); } public void update() { this.x = body.getposition().x; this.y = body.getposition().y; } public void render(graphics2d g) { g.setcolor(color.white); g.drawoval((int)x, (int)y, (int)w, (int)h); } playstate getstate() { return (playstate)game.statemachine.getcurrentstate(); } }
box.java
package enginex.jbox2dtestbed; import java.awt.color; import java.awt.graphics2d; import org.jbox2d.collision.shapes.polygonshape; import org.jbox2d.dynamics.body; import org.jbox2d.dynamics.bodydef; import org.jbox2d.dynamics.bodytype; import org.jbox2d.dynamics.fixturedef; import enginex.core.gameobject; @suppresswarnings("serial") public class box extends gameobject { testgame game; public double x = 100; public double y = 100; public int w = 100; public int h = 100; body body; bodydef bd = new bodydef(); polygonshape ps = new polygonshape(); fixturedef fd = new fixturedef(); float ppm = 1/32; public box(testgame game) { super(game); this.game = game; bd.type = bodytype.static; // bd.position.set((float)(x), (float)(y)); // ps.setasbox((float)(w), (float)(h)); bd.position.set((float)(x / ppm), (float)(y / ppm)); ps.setasbox((float)(w / ppm), (float)(h / ppm)); fd.shape = ps; body = getstate().world.createbody(bd); body.createfixture(fd); } public void render(graphics2d g) { g.setcolor(color.white); g.drawrect((int)(x), (int)(y), (int)(w), (int)(h)); } playstate getstate() { return (playstate)game.statemachine.getcurrentstate(); } }
Comments
Post a Comment