Coverage Summary for Class: RigidBody (com.mygdx.game.Components)
| Class | Method, % | Line, % | 
|---|---|---|
| RigidBody | 92.9% (13/14) | 98.3% (57/58) | 
| RigidBody$1 | 100% (1/1) | 100% (1/1) | 
| Total | 93.3% (14/15) | 98.3% (58/59) | 
1 package com.mygdx.game.Components; 2 3 import com.badlogic.gdx.math.Vector2; 4 import com.badlogic.gdx.physics.box2d.*; 5 import com.mygdx.game.Managers.PhysicsManager; 6 import com.mygdx.game.Physics.CollisionCallBack; 7 import com.mygdx.game.Physics.PhysicsBodyType; 8 9 /** 10 * Defines parameters related to collisions of sprites. 11 */ 12 public class RigidBody extends Component { 13 int bodyId; 14 private final Vector2 halfDim; 15 16 /** 17 * Sets up the base values of the RigidBody component 18 */ 19 public RigidBody() { 20 super(); 21 type = ComponentType.RigidBody; 22 halfDim = new Vector2(); 23 setRequirements(ComponentType.Transform, ComponentType.Renderable); 24 } 25 26 /** 27 * Calls constructor with is trigger false 28 * 29 * @param type defines how it interacts with other objects 30 * @param r used for creating the fixture (aka the collider) 31 * @param t used for positioning and scaling the collider 32 */ 33 public RigidBody(PhysicsBodyType type, Renderable r, Transform t) { 34 this(type, r, t, false); 35 } 36 37 /** 38 * Can create body that is trigger or callable 39 * 40 * @param type defines how it interacts with other objects 41 * @param r used for creating the fixture (aka the collider) 42 * @param t used for positioning and scaling the collider 43 * @param isTrigger false allows for collision true doesn't 44 */ 45 public RigidBody(PhysicsBodyType type, Renderable r, Transform t, boolean isTrigger) { 46 this(); 47 BodyDef def = new BodyDef(); 48 switch (type) { 49 case Static: 50 def.type = BodyDef.BodyType.StaticBody; 51 break; 52 case Dynamic: 53 def.type = BodyDef.BodyType.DynamicBody; 54 break; 55 case Kinematic: 56 def.type = BodyDef.BodyType.KinematicBody; 57 break; 58 } 59 float h_x = r.sprite.getWidth() * 0.5f; 60 float h_y = r.sprite.getHeight() * 0.5f; 61 halfDim.set(h_x, h_y); 62 63 def.position.set(t.getPosition().x, t.getPosition().y); //Removed halfDim addition for Assessment 2 64 h_x *= t.getScale().x; 65 h_y *= t.getScale().y; 66 67 def.angle = t.getRotation(); 68 69 PolygonShape shape = new PolygonShape(); 70 shape.setAsBox(h_x, h_y); 71 72 FixtureDef f = new FixtureDef(); 73 f.isSensor = isTrigger; 74 f.shape = shape; 75 f.density = type == PhysicsBodyType.Static ? 0.0f : 1.0f; 76 f.restitution = 0; // prevents bouncing 77 f.friction = 0; 78 79 bodyId = PhysicsManager.createBody(def, f, null); 80 81 shape.dispose(); 82 } 83 84 /** 85 * Adds a new circular fixture to the body as a trigger 86 */ 87 public void addTrigger(float radius, Object data) { 88 Body b = getBody(); 89 90 FixtureDef fDef = new FixtureDef(); 91 fDef.isSensor = true; 92 CircleShape shape = new CircleShape(); 93 shape.setRadius(radius); 94 95 fDef.shape = shape; 96 97 fDef.density = 0.0f; 98 fDef.restitution = 0.0f; 99 fDef.friction = 0.0f; 100 101 Fixture f = b.createFixture(fDef); 102 f.setUserData(data); 103 } 104 105 /** 106 * Is used during collision phase to add more functionality 107 * @param data class that inherits from CollisionCallBack 108 */ 109 public void setCallback(CollisionCallBack data) { 110 getBody().setUserData(data); 111 } 112 113 public void setVelocity(Vector2 vel) { 114 Body b = PhysicsManager.getBody(bodyId); 115 b.setLinearVelocity(vel); 116 } 117 118 public void setVelocity(float x, float y) { 119 setVelocity(new Vector2(x, y)); 120 } 121 122 /** 123 * Sets the center pos of the object 124 */ 125 public void setPosition(Vector2 position) { 126 Body b = PhysicsManager.getBody(bodyId); 127 /* Removed in Assessment 2 128 if (offset) { 129 position.add(halfDim); 130 } 131 */ 132 b.setTransform(position, 0); 133 } 134 135 /** 136 * Gets the current player position. 137 * // Change for Assessment 2 // 138 * @return player position. 139 */ 140 public Vector2 getPosition(){ 141 Body b = getBody(); 142 return b.getTransform().getPosition(); 143 } 144 145 /** 146 * @return the Body of this object 147 */ 148 public Body getBody() { 149 return PhysicsManager.getBody(bodyId); 150 } 151 152 /** 153 * Called every frame translates the transform to match with the box2d body's position factoring offset 154 */ 155 @Override 156 public void update() { 157 super.update(); 158 // parent.getComponent(Transform.class).setPosition(PhysicsManager.getBody(bodyId).getPosition()); 159 Transform t = parent.getComponent(Transform.class); 160 Body b = getBody(); 161 Vector2 p = b.getPosition().cpy(); 162 //p.sub(halfDim); //Removed in Assessment 2 163 t.setPosition(p, false); 164 } 165 166 /** 167 * @return the Linear Velocity of this object 168 */ 169 public Vector2 getVelocity() { 170 return getBody().getLinearVelocity(); 171 } 172 173 /** 174 * @return the Angular Velocity of this object 175 */ 176 public float getAngularVelocity() { 177 return getBody().getAngularVelocity(); 178 } 179 180 /** 181 * @param force the force to apply to the center of the object 182 */ 183 public void applyForce(Vector2 force) { 184 getBody().applyForceToCenter(force, true); 185 } 186 }