Android Open Source - Tanks Vector2






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

  protected float[] value;

  public Vector2()
  {
    value = new float[2];
  }

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

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

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

  public void setFrom(float x, float y)
  {
    value[0] = x;
    value[1] = y;
  }

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

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

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

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

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

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

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

  public float getAngle(Vector2 other)
  {
    float scalar = getScalar(other);
    float lengthOne = this.getLength();
    float lengthTwo = other.getLength();
    float angle = (float)Math.toDegrees(Math.acos(scalar / (lengthOne * lengthTwo)));

    return getCross(other) > 0 ? angle : 360 - angle;
  }

  public float getScalar(Vector2 other)
  {
    float multOne = getX() * other.getX();
    float multTwo = getY() * other.getY();

    return multOne + multTwo;
  }

  public float getCross(Vector2 other)
  {
    return getX() * other.getY() - getY() * other.getX();
  }

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

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

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

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

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

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

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

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

  @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 2;
  }
}




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