Skip to main content

rotation Rotation of a point around axis using quaternions

I'm new to 3D graphics/programming and trying to get my head around quaternions. GotI have a simple processing program to rotate a point around arbitrary axis but it stops after a bit. I'm trying to get full rotation of the point around the axis.

This might be 101 program for anyone here :)

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math3.complex.Quaternion;

float ang = 0f;
void setup() {
      size(600, 600, P3D);
}

void draw() {
  fill(255);
  //rect(0, 0, width, height);
  translate(width/2, height/2);
  Vector3D pt = new Vector3D(30, 145, 40);
  Quaternion ptQ = new Quaternion(0, pt.getX(), pt.getY(), pt.getZ());
  Vector3D axis = new Vector3D(50, 50, 50);
  Quaternion q = getQuaternionFromAxisAngle(ang, axis);
  Quaternion r = q.multiply(ptQ).multiply(q.getConjugate());

  stroke(255, 0, 0);
  line(0, 0, 0, (float)r.getQ1(), (float)r.getQ2(), (float)r.getQ3());
  ang += .1;
  println(r.getQ1() + " " + r.getQ2() + " " + r.getQ3());
}

Quaternion getQuaternionFromAxisAngle(float degAng, Vector3D vec) {
  
  float radAng = radians(degAng/2.0);
  // Here we calculate the sin( theta / 2) once for optimization
  double factor = sin( radAng);

  // Calculate the x, y and z of the quaternion
  double x = vec.getX() * factor;
  double y = vec.getY() * factor;
  double z = vec.getZ() * factor;

  // Calcualte the w value by cos( theta / 2 )
  double w = cos( radAng);
  return (new Quaternion(w, x, y, z)).normalize();
}

rotation of a point around axis using quaternions

I'm new to 3D graphics/programming and trying to get my head around quaternions. Got a simple processing program to rotate a point around arbitrary axis but it stops after a bit. I'm trying to get full rotation of the point around the axis.

This might be 101 program for anyone here :)

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math3.complex.Quaternion;

float ang = 0f;
void setup() {
      size(600, 600, P3D);
}

void draw() {
  fill(255);
  //rect(0, 0, width, height);
  translate(width/2, height/2);
  Vector3D pt = new Vector3D(30, 145, 40);
  Quaternion ptQ = new Quaternion(0, pt.getX(), pt.getY(), pt.getZ());
  Vector3D axis = new Vector3D(50, 50, 50);
  Quaternion q = getQuaternionFromAxisAngle(ang, axis);
  Quaternion r = q.multiply(ptQ).multiply(q.getConjugate());

  stroke(255, 0, 0);
  line(0, 0, 0, (float)r.getQ1(), (float)r.getQ2(), (float)r.getQ3());
  ang += .1;
  println(r.getQ1() + " " + r.getQ2() + " " + r.getQ3());
}

Quaternion getQuaternionFromAxisAngle(float degAng, Vector3D vec) {
  
  float radAng = radians(degAng/2.0);
  // Here we calculate the sin( theta / 2) once for optimization
  double factor = sin( radAng);

  // Calculate the x, y and z of the quaternion
  double x = vec.getX() * factor;
  double y = vec.getY() * factor;
  double z = vec.getZ() * factor;

  // Calcualte the w value by cos( theta / 2 )
  double w = cos( radAng);
  return (new Quaternion(w, x, y, z)).normalize();
}

Rotation of a point around axis using quaternions

I have a simple processing program to rotate a point around arbitrary axis but it stops after a bit. I'm trying to get full rotation of the point around the axis.

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math3.complex.Quaternion;

float ang = 0f;
void setup() {
      size(600, 600, P3D);
}

void draw() {
  fill(255);
  //rect(0, 0, width, height);
  translate(width/2, height/2);
  Vector3D pt = new Vector3D(30, 145, 40);
  Quaternion ptQ = new Quaternion(0, pt.getX(), pt.getY(), pt.getZ());
  Vector3D axis = new Vector3D(50, 50, 50);
  Quaternion q = getQuaternionFromAxisAngle(ang, axis);
  Quaternion r = q.multiply(ptQ).multiply(q.getConjugate());

  stroke(255, 0, 0);
  line(0, 0, 0, (float)r.getQ1(), (float)r.getQ2(), (float)r.getQ3());
  ang += .1;
  println(r.getQ1() + " " + r.getQ2() + " " + r.getQ3());
}

Quaternion getQuaternionFromAxisAngle(float degAng, Vector3D vec) {
  
  float radAng = radians(degAng/2.0);
  // Here we calculate the sin( theta / 2) once for optimization
  double factor = sin( radAng);

  // Calculate the x, y and z of the quaternion
  double x = vec.getX() * factor;
  double y = vec.getY() * factor;
  double z = vec.getZ() * factor;

  // Calcualte the w value by cos( theta / 2 )
  double w = cos( radAng);
  return (new Quaternion(w, x, y, z)).normalize();
}
Source Link

rotation of a point around axis using quaternions

I'm new to 3D graphics/programming and trying to get my head around quaternions. Got a simple processing program to rotate a point around arbitrary axis but it stops after a bit. I'm trying to get full rotation of the point around the axis.

This might be 101 program for anyone here :)

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.apache.commons.math3.complex.Quaternion;

float ang = 0f;
void setup() {
      size(600, 600, P3D);
}

void draw() {
  fill(255);
  //rect(0, 0, width, height);
  translate(width/2, height/2);
  Vector3D pt = new Vector3D(30, 145, 40);
  Quaternion ptQ = new Quaternion(0, pt.getX(), pt.getY(), pt.getZ());
  Vector3D axis = new Vector3D(50, 50, 50);
  Quaternion q = getQuaternionFromAxisAngle(ang, axis);
  Quaternion r = q.multiply(ptQ).multiply(q.getConjugate());

  stroke(255, 0, 0);
  line(0, 0, 0, (float)r.getQ1(), (float)r.getQ2(), (float)r.getQ3());
  ang += .1;
  println(r.getQ1() + " " + r.getQ2() + " " + r.getQ3());
}

Quaternion getQuaternionFromAxisAngle(float degAng, Vector3D vec) {
  
  float radAng = radians(degAng/2.0);
  // Here we calculate the sin( theta / 2) once for optimization
  double factor = sin( radAng);

  // Calculate the x, y and z of the quaternion
  double x = vec.getX() * factor;
  double y = vec.getY() * factor;
  double z = vec.getZ() * factor;

  // Calcualte the w value by cos( theta / 2 )
  double w = cos( radAng);
  return (new Quaternion(w, x, y, z)).normalize();
}