Coverage Summary for Class: PhysicsManager (com.mygdx.game.Managers)
Class |
Class, %
|
Method, %
|
Line, %
|
PhysicsManager |
100%
(1/1)
|
50%
(6/12)
|
37.3%
(19/51)
|
1 package com.mygdx.game.Managers;
2
3 import com.badlogic.gdx.maps.MapLayer;
4 import com.badlogic.gdx.maps.MapLayers;
5 import com.badlogic.gdx.maps.MapObject;
6 import com.badlogic.gdx.maps.MapObjects;
7 import com.badlogic.gdx.maps.objects.RectangleMapObject;
8 import com.badlogic.gdx.math.Rectangle;
9 import com.badlogic.gdx.math.Vector2;
10 import com.badlogic.gdx.physics.box2d.*;
11 import com.mygdx.game.Components.TileMap;
12
13 import java.util.ArrayList;
14 import java.util.Objects;
15
16 import static com.mygdx.utils.Constants.PHYSICS_TIME_STEP;
17
18 /**
19 * Manages the box2D world and bodies for the collision detection and physics
20 */
21 public final class PhysicsManager {
22 private static final float TILE_SIZE_INV = 1.0f;
23 public static boolean initialized = false;
24 public static World box2DWorld;
25 private static ArrayList<Body> box2DBodies;
26 private static Box2DDebugRenderer debug;
27
28 public static void Initialize() {
29 Initialize(false);
30 }
31
32 /**
33 * Draw the box2D world with debug borders shown.
34 *
35 * @param drawDebug true to show debug borders
36 */
37 public static void Initialize(boolean drawDebug) {
38 if (initialized) {
39 return;
40 }
41 initialized = true;
42
43 box2DWorld = new World(new Vector2(0, 0), true);
44 box2DBodies = new ArrayList<>();
45 box2DWorld.setContactListener(new CollisionManager());
46 if (drawDebug) {
47 debug = new Box2DDebugRenderer(true, false, true, true, false, true);
48 }
49 }
50
51 public static int createBody(BodyDef bDef, FixtureDef fDef, Object userData) {
52 tryInit();
53 bDef.fixedRotation = true;
54 Body b = box2DWorld.createBody(bDef);
55 b.createFixture(fDef);
56 b.setUserData(userData);
57 box2DBodies.add(b);
58 return box2DBodies.size();
59 }
60
61 private static Shape tile_getShape(Rectangle rectangle) {
62 PolygonShape polygonShape = new PolygonShape();
63 polygonShape.setAsBox(
64 rectangle.width * 0.5f * TILE_SIZE_INV,
65 rectangle.height * 0.5f * TILE_SIZE_INV);
66 return polygonShape;
67 }
68
69 private static Vector2 tile_getCenter(Rectangle rectangle) {
70 Vector2 center = new Vector2();
71 rectangle.getCenter(center);
72 return center.scl(TILE_SIZE_INV);
73 }
74
75 /**
76 * Populates the map with box2D bodies necessary for collisions to happen.
77 * @param map tilemap we are operating on
78 */
79 public static void createMapCollision(TileMap map) {
80 MapLayers layers = map.getTileMap().getLayers();
81 MapObjects objects = null;
82 for (MapLayer layer : layers) {
83 if (Objects.equals(layer.getName(), "Objects")) {
84 objects = layer.getObjects();
85 break;
86 }
87 }
88 if (objects == null) {
89 return;
90 }
91
92 for (MapObject object : objects) {
93 Rectangle rectangle = ((RectangleMapObject) object).getRectangle();
94
95 //create a dynamic within the world body (also can be KinematicBody or StaticBody
96 BodyDef bodyDef = new BodyDef();
97 bodyDef.type = BodyDef.BodyType.StaticBody;
98 Body body = box2DWorld.createBody(bodyDef);
99
100 //create a fixture for each body from the shape
101 Fixture fixture = body.createFixture(tile_getShape(rectangle), 1);
102 fixture.setFriction(0.1f);
103
104 //setting the position of the body's origin. In this case with zero rotation
105 body.setTransform(tile_getCenter(rectangle), 0);
106 }
107 }
108
109 public static Body getBody(int id) {
110 return box2DBodies.get(id - 1);
111 }
112
113 private static void tryInit() {
114 if (!initialized) {
115 Initialize();
116 }
117 }
118
119 public static void update() {
120 tryInit();
121 box2DWorld.step(PHYSICS_TIME_STEP, 6, 2);
122
123 if (debug != null) {
124 debug.render(box2DWorld, RenderingManager.getCamera().combined);
125 }
126 }
127
128 public static void cleanUp() {
129 tryInit();
130 box2DWorld.dispose();
131 if (debug != null) {
132 debug.dispose();
133 }
134 }
135 }