Android Open Source - Schooner-3D Vector






From Project

Back to project page Schooner-3D.

License

The source code is released under:

Apache License

If you think the Android project Schooner-3D 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

/*
 * Copyright 2012 Dan Mercer/*  w ww .  j  a  va2  s  . com*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.supermercerbros.gameengine.collision;

import com.supermercerbros.gameengine.util.Utils;

/**
 * A mathematical quantity with both magnitude and direction.
 */
public class Vector {
  public final float x;
  public final float y;
  public final float z;
  public final float length;

  /**
   * Creates a new Vector in the given direction and with the given length. If
   * <code>length = 0.0</code>, the components are unchanged.
   * 
   * @param x
   * @param y
   * @param z
   * @param length
   */
  Vector(final float x, final float y, final float z, final float length) {
    if (length == 0.0f) {
      this.x = x;
      this.y = y;
      this.z = z;
      this.length = Utils.pythagF(x, y, z);
    } else {
      final float mag = Utils.pythagF(x, y, z);
      this.x = (x / mag) * length;
      this.y = (y / mag) * length;
      this.z = (z / mag) * length;
      this.length = length;
    }

  }

  /**
   * Creates a new Vector between the given Points and with the given length.
   * If <code>length = 0.0f</code>, the components are unchanged.
   * 
   * @param t
   *            The tail Point
   * @param h
   *            the head Point
   * @param length
   *            The length of the Vector, or 0.0f if the length should be left
   *            as is.
   */
  public Vector(Point t, Point h, final float length) {
    final float x = h.x - t.x;
    final float y = h.y - t.y;
    final float z = h.z - t.z;

    if (length == 0.0f) {
      this.x = x;
      this.y = y;
      this.z = z;
      this.length = Utils.pythagF(x, y, z);
    } else {
      final float mag = Utils.pythagF(x, y, z);
      this.x = (x / mag) * length;
      this.y = (y / mag) * length;
      this.z = (z / mag) * length;
      this.length = length;
    }
  }

  /**
   * Computes the normalized cross product of this Vector and another Vector.
   * (The vectors are normalized before the operation.)
   * 
   * @param v
   *            The other vector.
   * @return The cross product of the two vectors.
   */
  public Vector cross(Vector v) {
    final float vx = v.x / v.length, vy = v.y / v.length, vz = v.z
        / v.length;

    final float rx = this.y * vz - this.z * vy;
    final float ry = this.z * vx - this.x * vz;
    final float rz = this.x * vy - this.y * vx;

    return new Vector(rx, ry, rz, 0.0f);
  }

  /**
   * Computes the cosine of the angle between this Vector and another Vector.
   * The cosine is equal to the dot product ({@link #dot(Vector)}) divided by
   * the product of the vectors' magnitudes.
   * 
   * @param other
   *            The other vector.
   * @return The cosine of the angle between the two vectors.
   */
  public float cos(Vector other) {
    if (equals(other)) {
      return 1.0f;
    } else {
      return (this.x * other.x + this.y * other.y + this.z * other.z)
          / (this.length * other.length);
    }
  }

  /**
   * Computes the dot product of this Vector and another vector. The
   * computation is this:
   * 
   * <pre>
   * this.x * other.x + this.y * other.y + this.z * other.z
   * </pre>
   * 
   * @param other
   *            The other Vector.
   * @return The dot product of the two vectors.
   * @see <a href="http://en.wikipedia.org/wiki/Dot_product">Dot Product
   *      (Wikipedia)</a>
   */
  public float dot(Vector other) {
    if (equals(other)) {
      return length * length;
    } else {
      return this.x * other.x + this.y * other.y + this.z * other.z;
    }
  }

  /**
   * Normalizes this vector.
   * 
   * @return A unit vector (length = 1) whose direction is equal to that of
   *         this Vector.
   */
  public Vector normalize() {
    return new Vector(x, y, z, 1.0f);
  }

  /**
   * Transforms this Vector by the given transformation matrix. Does not
   * currently support non-uniform scale.
   * 
   * @param matrix
   * @return The transformed Vector
   */
  public Vector transform(final Matrix matrix) {
    final float tx = matrix.m0 * x + matrix.m4 * y + matrix.m8 * z;
    final float ty = matrix.m1 * x + matrix.m5 * y + matrix.m9 * z;
    final float tz = matrix.m2 * x + matrix.m6 * y + matrix.m10 * z;
    return new Vector(tx, ty, tz, 0.0f);
  }

  /**
   * Flips this Vector.
   * 
   * @return
   */
  public Vector flip() {
    return new Vector(-x, -y, -z, 0.0f);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }

    if (!(o instanceof Vector)) {
      return false;
    }

    final Vector v = (Vector) o;

    return x == v.x && y == v.y && z == v.z;
  }

  @Override
  public String toString() {
    return "Vector[" + x + ", " + y + ", " + z + "]";
  }
}




Java Source Code List

com.supermercerbros.gameengine.GameActivity.java
com.supermercerbros.gameengine.GameView.java
com.supermercerbros.gameengine.Schooner3D.java
com.supermercerbros.gameengine.TestActivity.java
com.supermercerbros.gameengine.TestMaterials.java
com.supermercerbros.gameengine.TestObjects.java
com.supermercerbros.gameengine.animation.AnimationData.java
com.supermercerbros.gameengine.animation.Keyframe.java
com.supermercerbros.gameengine.animation.MeshAnimation.java
com.supermercerbros.gameengine.armature.ActionData.java
com.supermercerbros.gameengine.armature.Action.java
com.supermercerbros.gameengine.armature.BinarySkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Bone.java
com.supermercerbros.gameengine.armature.SkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Skeleton.java
com.supermercerbros.gameengine.collision.Bounds.java
com.supermercerbros.gameengine.collision.Collider.java
com.supermercerbros.gameengine.collision.CollisionDetector.java
com.supermercerbros.gameengine.collision.Collision.java
com.supermercerbros.gameengine.collision.DebugListener.java
com.supermercerbros.gameengine.collision.Edge.java
com.supermercerbros.gameengine.collision.Face.java
com.supermercerbros.gameengine.collision.Feature.java
com.supermercerbros.gameengine.collision.Intersection.java
com.supermercerbros.gameengine.collision.Line.java
com.supermercerbros.gameengine.collision.LocalDistMinimum.java
com.supermercerbros.gameengine.collision.Matrix.java
com.supermercerbros.gameengine.collision.OnCollisionCheckFinishedListener.java
com.supermercerbros.gameengine.collision.Plane.java
com.supermercerbros.gameengine.collision.Point.java
com.supermercerbros.gameengine.collision.Polyhedron.java
com.supermercerbros.gameengine.collision.SphereBounds.java
com.supermercerbros.gameengine.collision.Vector.java
com.supermercerbros.gameengine.collision.Vertex.java
com.supermercerbros.gameengine.debug.JankCatcher.java
com.supermercerbros.gameengine.debug.LoopLog.java
com.supermercerbros.gameengine.engine.Camera.java
com.supermercerbros.gameengine.engine.DataPipe.java
com.supermercerbros.gameengine.engine.EGLContextLostHandler.java
com.supermercerbros.gameengine.engine.Engine.java
com.supermercerbros.gameengine.engine.GameRenderer.java
com.supermercerbros.gameengine.engine.Light.java
com.supermercerbros.gameengine.engine.Normals.java
com.supermercerbros.gameengine.engine.RenderData.java
com.supermercerbros.gameengine.engine.Scene.java
com.supermercerbros.gameengine.engine.Time.java
com.supermercerbros.gameengine.engine.shaders.Material.java
com.supermercerbros.gameengine.engine.shaders.Program.java
com.supermercerbros.gameengine.engine.shaders.ShaderLib.java
com.supermercerbros.gameengine.engine.shaders.Shader.java
com.supermercerbros.gameengine.engine.shaders.VertexModifier.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteDispatcher.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteListener.java
com.supermercerbros.gameengine.hud.CoordsConverter.java
com.supermercerbros.gameengine.hud.GameHud.java
com.supermercerbros.gameengine.hud.HudElement.java
com.supermercerbros.gameengine.material.CelShadedMaterial.java
com.supermercerbros.gameengine.material.TexturedMaterial.java
com.supermercerbros.gameengine.math.BezierCurve.java
com.supermercerbros.gameengine.math.Curve.java
com.supermercerbros.gameengine.math.MatrixUtils.java
com.supermercerbros.gameengine.math.Quaternion.java
com.supermercerbros.gameengine.motion.CurveMovement.java
com.supermercerbros.gameengine.motion.MovementData.java
com.supermercerbros.gameengine.motion.Movement.java
com.supermercerbros.gameengine.objects.AnimatedMeshObject.java
com.supermercerbros.gameengine.objects.BasicMaterial.java
com.supermercerbros.gameengine.objects.BonedObject.java
com.supermercerbros.gameengine.objects.GameObject.java
com.supermercerbros.gameengine.objects.Metadata.java
com.supermercerbros.gameengine.parsers.ConstantCurve.java
com.supermercerbros.gameengine.parsers.GameFactory.java
com.supermercerbros.gameengine.parsers.PreBoneData.java
com.supermercerbros.gameengine.parsers.PreObjectData.java
com.supermercerbros.gameengine.parsers.Sch3D.java
com.supermercerbros.gameengine.render.Compositor.java
com.supermercerbros.gameengine.shaders.ProgramSource.java
com.supermercerbros.gameengine.texture.BitmapTexture.java
com.supermercerbros.gameengine.texture.ETC1CompressedTexture.java
com.supermercerbros.gameengine.texture.Texture.java
com.supermercerbros.gameengine.util.BetterDataInputStream.java
com.supermercerbros.gameengine.util.DelayedRunnable.java
com.supermercerbros.gameengine.util.GLES2.java
com.supermercerbros.gameengine.util.IPO.java
com.supermercerbros.gameengine.util.LoopingThread.java
com.supermercerbros.gameengine.util.Toggle.java
com.supermercerbros.gameengine.util.Utils.java