From 24b6a9d3edaf2a5ddb7a1399cd286716d1a1db13 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Wed, 20 Nov 2019 00:16:10 -0800 Subject: [PATCH] Fixed Box2D world generation Fixed Box2D world generation by subtracting tile offset Added Box2D debug renderer Disabled RenderSystem debug subtile flag --- .../com/riiablo/camera/IsometricCamera.java | 4 ++ .../system/debug/Box2DDebugRenderSystem.java | 45 +++++++++++++++++++ .../com/riiablo/map/Box2DPhysicsSystem.java | 10 +++-- core/src/com/riiablo/map/RenderSystem.java | 2 +- 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 core/src/com/riiablo/engine/system/debug/Box2DDebugRenderSystem.java diff --git a/core/src/com/riiablo/camera/IsometricCamera.java b/core/src/com/riiablo/camera/IsometricCamera.java index a4994039..fca7b311 100644 --- a/core/src/com/riiablo/camera/IsometricCamera.java +++ b/core/src/com/riiablo/camera/IsometricCamera.java @@ -22,6 +22,10 @@ public class IsometricCamera extends OrthographicCamera { toWorld(pixOffset.x, pixOffset.y, tileOffset.setZero()); } + public Vector2 getTileOffset(Vector2 dst) { + return dst.set(tileOffset); + } + @Override public void translate(Vector2 vec) { translate(vec.x, vec.y); diff --git a/core/src/com/riiablo/engine/system/debug/Box2DDebugRenderSystem.java b/core/src/com/riiablo/engine/system/debug/Box2DDebugRenderSystem.java new file mode 100644 index 00000000..f90bc8ba --- /dev/null +++ b/core/src/com/riiablo/engine/system/debug/Box2DDebugRenderSystem.java @@ -0,0 +1,45 @@ +package com.riiablo.engine.system.debug; + +import com.badlogic.ashley.core.Engine; +import com.badlogic.ashley.core.EntitySystem; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; +import com.riiablo.map.Box2DPhysicsSystem; +import com.riiablo.map.RenderSystem; + +public class Box2DDebugRenderSystem extends EntitySystem { + private static final float BOX2D_ZOOM_FACTOR = 0.04419419f; + + RenderSystem renderSystem; + Box2DPhysicsSystem physicsSystem; + OrthographicCamera camera; + Box2DDebugRenderer renderer; + + public Box2DDebugRenderSystem(RenderSystem renderSystem) { + setProcessing(false); + this.renderSystem = renderSystem; + renderer = new Box2DDebugRenderer(); + camera = new OrthographicCamera(); + camera.setToOrtho(true); + camera.near = -1024; + camera.far = 1024; + camera.rotate(Vector3.X, -60); + camera.rotate(Vector3.Z, -45); + } + + @Override + public void addedToEngine(Engine engine) { + assert physicsSystem == null; + physicsSystem = engine.getSystem(Box2DPhysicsSystem.class); + assert physicsSystem != null; + } + + @Override + public void update(float delta) { + camera.position.set(renderSystem.iso().position, 0); + camera.zoom = BOX2D_ZOOM_FACTOR * renderSystem.zoom(); + camera.update(); + renderer.render(physicsSystem.world, camera.combined); + } +} diff --git a/core/src/com/riiablo/map/Box2DPhysicsSystem.java b/core/src/com/riiablo/map/Box2DPhysicsSystem.java index b3d8828c..fb2459ed 100644 --- a/core/src/com/riiablo/map/Box2DPhysicsSystem.java +++ b/core/src/com/riiablo/map/Box2DPhysicsSystem.java @@ -19,6 +19,7 @@ import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.IntMap; import com.badlogic.gdx.utils.ObjectMap; +import com.riiablo.camera.IsometricCamera; import com.riiablo.engine.component.Box2DComponent; import com.riiablo.engine.component.PositionComponent; import com.riiablo.engine.component.VelocityComponent; @@ -76,15 +77,16 @@ public class Box2DPhysicsSystem extends IntervalIteratingSystem implements Entit world.destroyBody(body); } - public void setMap(Map map) { + public void setMap(Map map, IsometricCamera iso) { if (this.map != map) { this.map = map; - createBodies(); + Vector2 tileOffset = iso.getTileOffset(new Vector2()).scl(-1); // offset inverse of tile offset + createBodies(tileOffset); if (DEBUG) Gdx.app.debug(TAG, "bodies=" + world.getBodyCount()); } } - private void createBodies() { + private void createBodies(Vector2 offset) { IntMap filters = new IntMap<>(); BodyDef def = new BodyDef(); @@ -116,7 +118,7 @@ public class Box2DPhysicsSystem extends IntervalIteratingSystem implements Entit } int lenY = endY - ty; - def.position.set((endX + tx) / 2f, (endY + ty) / 2f); + def.position.set((endX + tx) / 2f, (endY + ty) / 2f).add(offset); PolygonShape shape = new PolygonShape(); shape.setAsBox(lenX / 2f, lenY / 2f); diff --git a/core/src/com/riiablo/map/RenderSystem.java b/core/src/com/riiablo/map/RenderSystem.java index 49e423d3..d90fe323 100644 --- a/core/src/com/riiablo/map/RenderSystem.java +++ b/core/src/com/riiablo/map/RenderSystem.java @@ -50,7 +50,7 @@ public class RenderSystem extends EntitySystem { private static final boolean DEBUG = true; private static final boolean DEBUG_MATH = DEBUG && !true; private static final boolean DEBUG_BUFFER = DEBUG && true; - private static final boolean DEBUG_SUBTILE = DEBUG && true; + private static final boolean DEBUG_SUBTILE = DEBUG && !true; private static final boolean DEBUG_TILE = DEBUG && !true; private static final boolean DEBUG_CAMERA = DEBUG && true; private static final boolean DEBUG_OVERSCAN = DEBUG && true;