Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently use OBBs and I have a vehicle that is a rigid body and some buildings.

Here is my update()

private void update()

    camera.setPosition((vehicle.getPosition().x * camera.getScale()) - ((getWidth() ) / 2.0f),
            (vehicle.getPosition().y * camera.getScale()) - ((getHeight() ) / 2.0f));

    //camera.move(input.getAnalogStick().getStickValueX() * 15.0f, input.getAnalogStick().getStickValueY() * 15.0f);
        vehicle.setThrottle(1.0f, false);



    vehicle.update(16.6666f / 1000.0f);

    ArrayList<Building> buildings = city.getBuildings();
    for(Building b : buildings)
            vehicle.update(-17.0f / 1000.0f);

The collision detection works well. What doesn't is how they are dealt with.

My goal is simple. If the vehicle hits a building, it should stop, and never go into the building. When I apply negative torque to reverse the car should not feel buggy and move away from the building. I don't want this to look buggy.

This is my rigid body class:

class RigidBody extends Entity
  private Vector2D velocity = new Vector2D();
  private Vector2D forces = new Vector2D();
  private float mass;

  private float angularVelocity;
  private float torque;
  private float inertia;

  private Vector2D halfSize = new Vector2D();
  private Bitmap image;

  public RigidBody()
      //set these defaults so we don't get divide by zeros
      mass = 1.0f;
      inertia = 1.0f;

  //intialize out parameters
  public void initialize(Vector2D halfSize, float mass, Bitmap bitmap)
      //store physical parameters
      this.halfSize = halfSize;
      this.mass = mass;
      image = bitmap;
      inertia = (1.0f / 20.0f) * (halfSize.x * halfSize.x) * (halfSize.y * halfSize.y) * mass;

      RectF rect = new RectF();
      float scalar = 10.0f;
      rect.left = (int)-halfSize.x * scalar; = (int)-halfSize.y * scalar;

      rect.right = rect.left + (int)(halfSize.x * 2.0f * scalar);
      rect.bottom = + (int)(halfSize.y * 2.0f * scalar);

  public void setLocation(Vector2D position, float angle)
      getRect().set(position, getWidth(), getHeight(), angle);

  public Vector2D getPosition()
      return getRect().getCenter();

  public void update(float timeStep)
      //integrate physics
      Vector2D acceleration = Vector2D.scalarDivide(forces, mass);
      velocity = Vector2D.add(velocity, Vector2D.scalarMultiply(acceleration, timeStep));
      Vector2D c = getRect().getCenter();
      c = Vector2D.add(getRect().getCenter(), Vector2D.scalarMultiply(velocity , timeStep));
      setCenter(c.x, c.y);
      forces = new Vector2D(0,0); //clear forces

      float angAcc = torque / inertia;
      angularVelocity += angAcc * timeStep;
      setAngle(getAngle() +  angularVelocity * timeStep);
      torque = 0; //clear torque

  //take a relative Vector2D and make it a world Vector2D
  public Vector2D relativeToWorld(Vector2D relative)
      Matrix mat = new Matrix();
      float[] Vector2Ds = new float[2];

      Vector2Ds[0] = relative.x;
      Vector2Ds[1] = relative.y;


      return new Vector2D(Vector2Ds[0], Vector2Ds[1]);

  //take a world Vector2D and make it a relative Vector2D
  public Vector2D worldToRelative(Vector2D world)
      Matrix mat = new Matrix();
      float[] Vectors = new float[2];

      Vectors[0] = world.x;
      Vectors[1] = world.y;


      return new Vector2D(Vectors[0], Vectors[1]);

  //velocity of a point on body
  public Vector2D pointVelocity(Vector2D worldOffset)
      Vector2D tangent = new Vector2D(-worldOffset.y, worldOffset.x);
      return Vector2D.add( Vector2D.scalarMultiply(tangent, angularVelocity) , velocity);

  public void applyForce(Vector2D worldForce, Vector2D worldOffset)
      //add linear force
      forces = Vector2D.add(forces ,worldForce);
      //add associated torque
      torque += Vector2D.cross(worldOffset, worldForce);

  public void draw( GraphicsContext c)
      c.drawRotatedScaledBitmap(image, getPosition().x, getPosition().y,
              getWidth(), getHeight(), getAngle());


Essentially, when any rigid body hits a building it should exhibit the same behavior.

How is collision solving usually done?


share|improve this question
Can you post the code that handles collision detection and resolution? It is difficult to steer you in the right direction without knowing what you are trying now. – ktodisco Nov 5 '12 at 2:02

I wrote an article a while back about the basics of 2D collision detection, which covers circles, AABBs, OBBs and polygons moving linearly and rotating.

It should give you some pointers :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.