Android Open Source - Tanks Collision3 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;
/* www .  j  a  v a2  s  . c  om*/
import android.opengl.Matrix;

import com.ThirtyNineEighty.Helpers.Plane;
import com.ThirtyNineEighty.Helpers.Vector;
import com.ThirtyNineEighty.Helpers.Vector2;
import com.ThirtyNineEighty.Helpers.Vector3;

import java.util.ArrayList;

public class Collision3D
  extends Collision<Vector3>
{
  private Vector3 mtv;
  private float mtvLength;
  private boolean collide;

  public Collision3D(ICollidable objectOne, ICollidable objectTwo)
  {
    CheckResult result = check(objectOne, objectTwo);

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

    collide = true;
    mtv = getMTV(result);
    mtvLength = result.collision.getMTVLength();
  }

  private static CheckResult check(ICollidable firstPh, ICollidable secondPh)
  {
    ArrayList<Plane> planes = getPlanes(firstPh, secondPh);

    Collision2D min = null;
    Plane minPlane = new Plane();

    for(Plane plane : planes)
    {
      ArrayList<Vector2> resultOne = firstPh.getConvexHull(plane);
      ArrayList<Vector2> resultTwo = secondPh.getConvexHull(plane);

      Collision2D collision = new Collision2D(resultOne, resultTwo);

      Vector.release(resultOne);
      Vector.release(resultTwo);

      if (!collision.isCollide())
        return null;

      if (min == null || Collision2D.compare(collision, min) < 0)
      {
        min = collision;
        minPlane.setFrom(plane);
      }
    }

    return new CheckResult(min, minPlane);
  }

  private static ArrayList<Plane> getPlanes(ICollidable firstPh, ICollidable secondPh)
  {
    ArrayList<Plane> planes = new ArrayList<Plane>();
    ArrayList<Vector3> firstNormals = firstPh.getGlobalNormals();
    ArrayList<Vector3> secondNormals = secondPh.getGlobalNormals();

    int size = firstNormals.size() + secondNormals.size();

    Plane plane = new Plane();

    for(int i = 0; i < size; i++)
    {
      setPlane(plane, firstNormals, secondNormals, i);

      if (!planes.contains(plane))
        planes.add(new Plane(plane));
    }

    return planes;
  }

  private static void setPlane(Plane plane, ArrayList<Vector3> firstNormals, ArrayList<Vector3> secondNormals, int num)
  {
    if (num < firstNormals.size())
      plane.setFrom(firstNormals.get(num));
    else
    {
      num -= secondNormals.size();
      plane.setFrom(secondNormals.get(num));
    }

    plane.swapZY();
  }

  private static Vector3 getMTV(CheckResult result)
  {
    Vector2 mtv2 = result.collision.getMTV();
    Vector3 mtv3 = new Vector3(mtv2.getX(), mtv2.getY(), 0);

    Vector3 planeX = result.plane.xAxis();
    Vector3 planeY = result.plane.yAxis();
    Vector3 planeZ = result.plane.zAxis();

    float[] matrix = new float[16];
    matrix[0] = planeX.getX();
    matrix[1] = planeX.getY();
    matrix[2] = planeX.getZ();

    matrix[4] = planeY.getX();
    matrix[5] = planeY.getY();
    matrix[6] = planeY.getZ();

    matrix[8] = planeZ.getX();
    matrix[9] = planeZ.getY();
    matrix[10] = planeZ.getZ();

    matrix[15] = 1.0f;

    Matrix.multiplyMV(mtv3.getRaw(), 0, matrix, 0, mtv3.getRaw(), 0);

    mtv3.normalize();
    return mtv3;
  }

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

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

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

  private static class CheckResult
  {
    public Collision2D collision;
    public Plane plane;

    public CheckResult(Collision2D collision, Plane plane)
    {
      this.collision = collision;
      this.plane = plane;
    }
  }
}




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