1
\$\begingroup\$

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();
}
\$\endgroup\$
5
  • \$\begingroup\$ it does rotate but it is slow when rotation traverses to half \$\endgroup\$ Commented May 17, 2016 at 6:44
  • \$\begingroup\$ Is there a reason why you are importing apache's Vector3D and Quaternion instead of using Unity's own Quaternion and Vector3? \$\endgroup\$ Commented May 17, 2016 at 14:28
  • \$\begingroup\$ @Sheikz This appears to be a Processing question, not a Unity question. \$\endgroup\$ Commented May 17, 2016 at 15:11
  • \$\begingroup\$ yeah, this isn't unity question, I was looking for conceptual correctness for a simple code piece like this. \$\endgroup\$ Commented May 17, 2016 at 17:29
  • \$\begingroup\$ Do I have to convert result quaternion to something else before plotting ? \$\endgroup\$ Commented May 18, 2016 at 0:06

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.