Android Open Source - Tanks Collision2 D






From Project

Back to project page Tanks.

License

The source code is released under:

MIT License

If you think the Android project Tanks listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ThirtyNineEighty.Game.Collisions;
//from  w w w . j a  v a  2 s.c o  m
import com.ThirtyNineEighty.Helpers.Vector2;

import java.util.ArrayList;

public class Collision2D
  extends Collision<Vector2>
{
  private Vector2 mtv;
  private float mtvLength;
  private boolean collide;

  public Collision2D(ArrayList<Vector2> first, ArrayList<Vector2> second)
  {
    CheckResult result = check(first, second);

    if (result == null)
    {
      collide = false;
      return;
    }

    collide = true;
    mtv = result.mtv;
    mtvLength = Math.abs(result.mtvLength);
  }

  private static CheckResult check(ArrayList<Vector2> firstVertices, ArrayList<Vector2> secondVertices)
  {
    Vector2 mtv = null;
    Vector2 normal = new Vector2();
    float mtvLength = 0.0f;
    boolean IsFirst = true;
    int count = firstVertices.size() + secondVertices.size();

    for (int i = 0; i < count; i ++)
    {
      setNormal(normal, firstVertices, secondVertices, i);

      Vector2 firstProjection = getProjection(firstVertices, normal);
      Vector2 secondProjection = getProjection(secondVertices, normal);

      if (firstProjection.getX() < secondProjection.getY() || secondProjection.getX() < firstProjection.getY())
        return null;

      if (IsFirst)
      {
        mtv = new Vector2(normal);
        mtvLength = (secondProjection.getY() - firstProjection.getX() > 0)
          ? secondProjection.getY() - firstProjection.getX()
          : firstProjection.getY() - secondProjection.getX();

        IsFirst = false;
      }
      else
      {
        float tempMTVLength = (secondProjection.getY() - firstProjection.getX() > 0)
          ? secondProjection.getY() - firstProjection.getX()
          : firstProjection.getY() - secondProjection.getX();

        if (Math.abs(tempMTVLength) < Math.abs(mtvLength))
        {
          mtv = new Vector2(normal);
          mtvLength = tempMTVLength;
        }
      }
    }

    return new CheckResult(mtv, mtvLength);
  }

  private static Vector2 getProjection(ArrayList<Vector2> vertices, Vector2 normal)
  {
    Vector2 result = null;

    for (Vector2 current : vertices)
    {
      float scalar = current.getScalar(normal);

      if (result == null)
        result = new Vector2(scalar, scalar);

      if (scalar > result.getX())
        result.setX(scalar);

      if (scalar < result.getY())
        result.setY(scalar);
    }

    return result;
  }

  private static void setNormal(Vector2 normal, ArrayList<Vector2> firstVertices, ArrayList<Vector2> secondVertices, int num)
  {
    if (num < firstVertices.size())
      setNormal(normal, firstVertices, num);
    else
    {
      num -= firstVertices.size();
      setNormal(normal, secondVertices, num);
    }
  }

  private static void setNormal(Vector2 normal, ArrayList<Vector2> vertices, int num)
  {
    Vector2 firstPoint = vertices.get(num);
    Vector2 secondPoint = vertices.get(num + 1 == vertices.size() ? 0 : num + 1);

    Vector2 edge = secondPoint.getSubtract(firstPoint);

    normal.setX(edge.getY());
    normal.setY(-edge.getX());

    normal.normalize();
  }

  @Override
  public Vector2 getMTV()
  {
    return mtv;
  }

  @Override
  public float getMTVLength()
  {
    return mtvLength;
  }

  @Override
  public boolean isCollide()
  {
    return collide;
  }

  private static class CheckResult
  {
    private Vector2 mtv;
    private float mtvLength;

    public CheckResult(Vector2 mtv, float mtvLength)
    {
      this.mtv = mtv;
      this.mtvLength = mtvLength;
    }
  }
}




Java Source Code List

com.ThirtyNineEighty.Game.EngineObject.java
com.ThirtyNineEighty.Game.IEngineObject.java
com.ThirtyNineEighty.Game.Collisions.Collidable.java
com.ThirtyNineEighty.Game.Collisions.Collision2D.java
com.ThirtyNineEighty.Game.Collisions.Collision3D.java
com.ThirtyNineEighty.Game.Collisions.CollisionManager.java
com.ThirtyNineEighty.Game.Collisions.Collision.java
com.ThirtyNineEighty.Game.Collisions.ICollidable.java
com.ThirtyNineEighty.Game.Gameplay.Bullet.java
com.ThirtyNineEighty.Game.Gameplay.GameObject.java
com.ThirtyNineEighty.Game.Gameplay.Tank.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.CharacteristicFactory.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Characteristic.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Upgrade.java
com.ThirtyNineEighty.Game.Menu.BaseMenu.java
com.ThirtyNineEighty.Game.Menu.GameMenu.java
com.ThirtyNineEighty.Game.Menu.IMenu.java
com.ThirtyNineEighty.Game.Menu.Controls.Button.java
com.ThirtyNineEighty.Game.Menu.Controls.IControl.java
com.ThirtyNineEighty.Game.Worlds.GameWorld.java
com.ThirtyNineEighty.Game.Worlds.IWorld.java
com.ThirtyNineEighty.Helpers.Plane.java
com.ThirtyNineEighty.Helpers.Vector2.java
com.ThirtyNineEighty.Helpers.Vector3.java
com.ThirtyNineEighty.Helpers.VectorUtils.java
com.ThirtyNineEighty.Helpers.Vector.java
com.ThirtyNineEighty.Renderable.Renderable.java
com.ThirtyNineEighty.Renderable.Shader2D.java
com.ThirtyNineEighty.Renderable.Shader3D.java
com.ThirtyNineEighty.Renderable.Shader.java
com.ThirtyNineEighty.Renderable.Renderable2D.I2DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable2D.Label.java
com.ThirtyNineEighty.Renderable.Renderable2D.Sprite.java
com.ThirtyNineEighty.Renderable.Renderable3D.I3DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable3D.Model3D.java
com.ThirtyNineEighty.System.ConfigChooser.java
com.ThirtyNineEighty.System.Content.java
com.ThirtyNineEighty.System.GameActivity.java
com.ThirtyNineEighty.System.GameContext.java
com.ThirtyNineEighty.System.IContent.java
com.ThirtyNineEighty.System.ISubprogram.java