Android Open Source - Tanks Vector3






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.Helpers;
/*from   w  w w.j  a  v a 2s.c o  m*/
/*
 * Operation with prefix get - immutable;
 */
public class Vector3 extends Vector
{
  public final static Vector3 xAxis = new Vector3(1.0f, 0.0f, 0.0f);
  public final static Vector3 yAxis = new Vector3(0.0f, 1.0f, 0.0f);
  public final static Vector3 zAxis = new Vector3(0.0f, 0.0f, 1.0f);
  public final static Vector3 zero = new Vector3(0.0f, 0.0f, 0.0f);

  protected float[] value;

  public Vector3()
  {
    value = new float[4];
  }

  public Vector3(float x, float y, float z)
  {
    this();
    setFrom(x, y, z);
  }

  public Vector3(float[] raw)
  {
    value = raw;
  }

  public Vector3(Vector3 vec)
  {
    this();
    setFrom(vec);
  }

  public void setFrom(float x, float y, float z)
  {
    value[0] = x;
    value[1] = y;
    value[2] = z;
    value[3] = 1.0f;
  }

  public void setFrom(Vector3 vec)
  {
    setFrom(vec.getX(), vec.getY(), vec.getZ());
  }

  public float getX() { return value[0]; }
  public float getY() { return value[1]; }
  public float getZ() { return value[2]; }

  public void setX(float v) { value[0] = v; }
  public void setY(float v) { value[1] = v; }
  public void setZ(float v) { value[2] = v; }

  public void addToX(float v) { value[0] += v; }
  public void addToY(float v) { value[1] += v; }
  public void addToZ(float v) { value[2] += v; }

  public float[] getRaw() { return value; }

  public float getLength()
  {
    double powX = Math.pow(getX(), 2);
    double powY = Math.pow(getY(), 2);
    double powZ = Math.pow(getZ(), 2);

    return (float)Math.sqrt(powX + powY + powZ);
  }

  public float getAngle(Vector3 other)
  {
    Vector3 normal = getCross(other);

    if (normal.equals(Vector3.zero))
      return getScalar(other) > 0 ? 0 : 180;

    Plane plane = new Plane(normal);

    Vector2 vecOne = plane.getProjection(this);
    Vector2 vecTwo = plane.getProjection(other);

    return vecOne.getAngle(vecTwo);
  }

  public void normalize()
  {
    float length = getLength();

    if (length == 0f)
      return;

    value[0] /= length;
    value[1] /= length;
    value[2] /= length;
  }

  public float getScalar(Vector3 other)
  {
    float multOne   = getX() * other.getX();
    float multTwo   = getY() * other.getY();
    float multThree = getZ() * other.getZ();

    return multOne + multTwo + multThree;
  }

  public void scale(float coefficient)
  {
    value[0] *= coefficient;
    value[1] *= coefficient;
    value[2] *= coefficient;
  }

  public void cross(Vector3 other)
  {
    float[] otherValue = other.getRaw();

    float result1 =      value[1] * otherValue[2];
    float result2 = -1 * value[2] * otherValue[1];
    float result3 = -1 * value[0] * otherValue[2];
    float result4 =      value[2] * otherValue[0];
    float result5 =      value[0] * otherValue[1];
    float result6 = -1 * value[1] * otherValue[0];

    setFrom(result1 + result2, result3 + result4, result5 + result6);
  }

  public void orthogonal()
  {
    float x = getX();
    float y = getY();
    float z = getZ();

    setFrom(-y, x, 0);

    if (equals(Vector3.zero))
      setFrom(0, z, -y);
  }

  public void add(Vector3 other)
  {
    setFrom(getX() + other.getX(),
            getY() + other.getY(),
            getZ() + other.getZ());
  }

  public void subtract(Vector3 other)
  {
    setFrom(getX() - other.getX(),
            getY() - other.getY(),
            getZ() - other.getZ());
  }

  public Vector3 getNormalize()
  {
    Vector3 result = new Vector3(this);
    result.normalize();
    return result;
  }

  public Vector3 getScale(float coefficient)
  {
    Vector3 result = new Vector3(this);
    result.scale(coefficient);
    return result;
  }

  public Vector3 getCross(Vector3 other)
  {
    Vector3 result = new Vector3(this);
    result.cross(other);
    return result;
  }

  public Vector3 getOrthogonal()
  {
    Vector3 result = new Vector3(this);
    result.orthogonal();
    return result;
  }

  public Vector3 getSum(Vector3 other)
  {
    Vector3 result = new Vector3(this);
    result.add(other);
    return result;
  }

  public Vector3 getSubtract(Vector3 other)
  {
    Vector3 result = new Vector3(this);
    result.subtract(other);
    return result;
  }

  @Override
  public boolean equals(Object o)
  {
    Vector3 other = o instanceof Vector3 ? (Vector3)o : null;

    return other != null
      && Math.abs(other.getX() - getX()) < epsilon
      && Math.abs(other.getY() - getY()) < epsilon
      && Math.abs(other.getZ() - getZ()) < epsilon;
  }

  @Override
  public String toString()
  {
    return String.format("{%f; %f; %f}", value[0], value[1], value[2]);
  }

  @Override
  public float get(int num)
  {
    return value[num];
  }

  @Override
  public void set(int num, float v)
  {
    value[num] = v;
  }

  @Override
  public int getSize()
  {
    return 3;
  }
}




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