Coverage Summary for Class: RigidBody (com.mygdx.game.Components)

Class Method, % Line, %
RigidBody 86.7% (13/15) 92.2% (59/64)
RigidBody$1 100% (1/1) 100% (1/1)
Total 87.5% (14/16) 92.3% (60/65)


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