- import java.lang.Math;
- public class Test{
- public static class Vector3{
- public double x, y, z;
- public void normalize(){
- double mag = Math.sqrt(x*x+y*y+z*z);
- x = x/mag;
- y = y/mag;
- z = z/mag;
- }
- public void printVector(){
- System.out.println((Double.toString(x) + " " + Double.toString(y) + " " + Double.toString(z)));
- }
- }
- public static class Quaternion{
- public double qi, qj, qk, qr;
- public void vectorToQuaternion(double angle, Vector3 in){
- angle = angle*3.141592654/180;
- qr = Math.cos(angle/2);
- qi = in.x*Math.sin(angle/2);
- qj = in.y*Math.sin(angle/2);
- qk = in.z*Math.sin(angle/2);
- }
- public Vector3 rotateVector(Vector3 in){
- Vector3 out = new Vector3();
- double M1, M2, M3,
- M4, M5, M6,
- M7, M8, M9;
- M1 = 1 - 2*qj*qj - 2*qk*qk;
- M2 = 2*(qi*qj - qk*qr);
- M3 = 2*(qi*qk + qj*qr);
- M4 = 2*(qi*qj + qk*qr);
- M5 = 1 - 2*qi*qi - 2*qk*qk;
- M6 = 2*(qj*qk - qi*qr);
- M7 = 2*(qi*qk - qj*qr);
- M8 = 2*(qj*qk + qi*qr);
- M9 = 1 - 2*qi*qi - 2*qj*qj;
- out.x = M1*in.x + M2*in.y + M3*in.z;
- out.y = M4*in.x + M5*in.y + M6*in.z;
- out.z = M7*in.x + M8*in.y + M9*in.z;
- return out;
- }
- public void printQuaternion(){
- }
- }
- public static void main(String args[]) {
- Vector3 forward, other;
- Quaternion myQ;
- forward = new Vector3();
- myQ = new Quaternion();
- other = new Vector3();
- // Set the vector you rotate about:
- other.x = 0.0;
- other.y = 0.0;
- other.z = 1.0;
- // Set the vector you are trying to rotate:
- forward.x = 1.0;
- forward.y = 0.0;
- forward.z = 0.0;
- System.out.println("Initially:");
- forward.printVector();
- System.out.println("\nFinally: ");
- myQ.vectorToQuaternion(30.0, other);
- other = myQ.rotateVector(forward);
- other.printVector();
- }
- }
SHARE
TWEET
Untitled
a guest
Oct 27th, 2015
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

