Java examples for java.lang:Math Vector
Given a point O(oriX,oriY) and a vector V(oriDX,oriDY), with V origin in O and V unit vector, tells how much we need to rotate V around O so that V points to Target.
//package com.java2s; public class Main { public static final float PI = (float) Math.PI; /**// w ww .j a va 2 s . c o m * Given a point O(oriX,oriY) and a vector V(oriDX,oriDY), * with V origin in O and V unit vector, tells how much we * need to rotate V around O so that V points to Target. * @param oriX O x coordinate * @param oriY O y coordinate * @param oriDX V x coordinates * @param oriDY V y coordinates * @param targetX Target x coordinate * @param targetY Target y coordinate * @return Angle to rotate around O so V points to Target */ static public float getAngleBetween(float oriX, float oriY, float oriDX, float oriDY, float targetX, float targetY) { float cos = oriDX; float sin = oriDY; float currentAngle = (float) Math.acos(cos); if (sin < 0) currentAngle = -currentAngle; return getAngleBetween(oriX, oriY, currentAngle, targetX, targetY); } /** * Given a point O(oriX,oriY) and a rotation angle around O * oriAngle, tells how much rotation we still need to add to * oriAngle so we are "looking at the target" from O. * @param oriX O x coordinate * @param oriY O y coordinate * @param oriAngle initial angle of rotation around O, within [-2PI,PI] * @param targetX Target x coordinate * @param targetY Target y coordinate * @return Angle to rotate around O so V points to Target */ static public float getAngleBetween(float oriX, float oriY, float oriAngle, float targetX, float targetY) { float currentAngle = oriAngle; //Normalized vector to target float vectorToTargetX = targetX - oriX; float vectorToTargetY = targetY - oriY; float normSquare = vectorToTargetX * vectorToTargetX + vectorToTargetY * vectorToTargetY; if (normSquare == 0f) { //Target and origin are the same. return 0f; } float norm = (float) Math.sqrt(normSquare); vectorToTargetX /= norm; vectorToTargetY /= norm; //calculate target angle float cos = vectorToTargetX; float sin = vectorToTargetY; float targetAngle = (float) Math.acos(cos); if (sin < 0) targetAngle = -targetAngle; //How much should we rotate? float angleBetweenTargetAndDirection = targetAngle - currentAngle; if (angleBetweenTargetAndDirection < 0 && angleBetweenTargetAndDirection < -Math.PI) { angleBetweenTargetAndDirection += 2 * Math.PI; } else if (angleBetweenTargetAndDirection > 0 && angleBetweenTargetAndDirection > Math.PI) { angleBetweenTargetAndDirection -= 2 * Math.PI; } return angleBetweenTargetAndDirection; } }