Coverage Summary for Class: CollisionManager (com.mygdx.game.Managers)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| CollisionManager | 100% (1/1) | 100% (6/6) | 94.3% (50/53) |
1 package com.mygdx.game.Managers; 2 3 import com.badlogic.gdx.physics.box2d.*; 4 import com.mygdx.game.Entitys.Entity; 5 import com.mygdx.game.Physics.CollisionCallBack; 6 import com.mygdx.game.Physics.CollisionInfo; 7 8 /** 9 * Handels collision callbacks for box2d 10 */ 11 public class CollisionManager implements ContactListener { 12 private static boolean initialized = false; 13 14 public CollisionManager() { 15 if (initialized) { 16 throw new RuntimeException("Collision manager cant be instantiated more then once"); 17 } 18 initialized = true; 19 } 20 21 /** 22 * called for every contact that box2d detects prior to collision restitution (doesn't matter if it is a trigger/sensor) 23 * 24 * @param contact the contact data 25 */ 26 @Override 27 public void beginContact(Contact contact) { 28 // generally calls the correct callback on the appropriate objects (not as intuitive as id like though) 29 Fixture fa = contact.getFixtureA(); 30 Body ba = fa.getBody(); 31 Object oa = ba.getUserData(); 32 CollisionCallBack cbA = (CollisionCallBack) oa; 33 34 Fixture fb = contact.getFixtureB(); 35 Body bb = fb.getBody(); 36 Object ob = bb.getUserData(); 37 CollisionCallBack cbB = (CollisionCallBack) ob; 38 39 final CollisionInfo info = new CollisionInfo(); 40 info.fA = fa; 41 info.fB = fb; 42 43 info.bA = ba; 44 info.bB = bb; 45 46 info.a = (Entity) cbA; 47 info.b = (Entity) cbB; 48 49 if (cbA != null) { 50 // fa is sensor but not fb 51 if (fa.isSensor() && cbB != null && !fb.isSensor()) { 52 cbB.EnterTrigger(info); 53 } else { 54 cbA.BeginContact(info); 55 } 56 } 57 58 if (cbB != null) { 59 if (fb.isSensor() && cbA != null && !fa.isSensor()) { 60 cbA.EnterTrigger(info); 61 } else { 62 cbB.BeginContact(info); 63 } 64 } 65 } 66 67 /** 68 * called for every contact that box2d detects after collision restitution (doesn't matter if it is a trigger/sensor) 69 * 70 * @param contact the contact data 71 */ 72 @Override 73 public void endContact(Contact contact) { 74 // generally calls the correct callback on the appropriate objects (not as intuitive as id like though) 75 Fixture fa = contact.getFixtureA(); 76 Body ba = fa.getBody(); 77 Object oa = ba.getUserData(); 78 CollisionCallBack cbA = (CollisionCallBack) oa; 79 80 Fixture fb = contact.getFixtureB(); 81 Body bb = fb.getBody(); 82 Object ob = bb.getUserData(); 83 CollisionCallBack cbB = (CollisionCallBack) ob; 84 85 final CollisionInfo info = new CollisionInfo(); 86 info.fA = fa; 87 info.fB = fb; 88 89 info.bA = ba; 90 info.bB = bb; 91 92 info.a = (Entity) cbA; 93 info.b = (Entity) cbB; 94 95 if (cbA != null) { 96 if (fa.isSensor() && cbB != null && !fb.isSensor()) { 97 cbB.ExitTrigger(info); 98 } else { 99 cbA.EndContact(info); 100 } 101 } 102 103 if (cbB != null) { 104 if (fb.isSensor() && cbA != null && !fa.isSensor()) { 105 cbA.ExitTrigger(info); 106 } else { 107 cbB.EndContact(info); 108 } 109 } 110 } 111 112 @Override 113 public void preSolve(Contact contact, Manifold oldManifold) { 114 115 } 116 117 @Override 118 public void postSolve(Contact contact, ContactImpulse impulse) { 119 120 } 121 }