From e87920c7a395b702ad458e636303340aed0f8e0c Mon Sep 17 00:00:00 2001 From: sableRaf Date: Thu, 14 Feb 2013 22:59:26 +0100 Subject: [PATCH 1/3] New Processing example (quaternions to Euler rotation) Uses Toxiclib's Quaternion class. --- .../quaternions_to_rotate.pde | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde diff --git a/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde b/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde new file mode 100644 index 00000000..d0afa904 --- /dev/null +++ b/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde @@ -0,0 +1,69 @@ +// Import the Quaternion class from Toxiclib +// Get the lib at: http://toxiclibs.org/downloads/ +import toxi.geom.Quaternion; + +// Import the PS Move API Package +import io.thp.psmove.*; + + +PSMove move; + +Quaternion orientation; + +void setup() { + size(200, 200, P3D); + + orientation = new Quaternion(); // With a little help from Toxiclib + + move = new PSMove(0); + move.enable_orientation(1); // We need to tell the PSMove object to activate sensor fusion + move.reset_orientation(); // Set the values of the quaternions to their start values [1, 0, 0, 0] +} + +void draw() { + background(155); + + float [] quat0 = {0.f}, quat1 = {0.f}, quat2 = {0.f}, quat3 = {0.f}; + + while (move.poll() != 0) {} + + move.get_orientation(quat0, quat1, quat2, quat3); + + // Get the individual values + float w = quat0[0]; + float x = quat1[0]; + float y = quat2[0]; + float z = quat3[0]; + + orientation.set(w,x,y,z); + //println("quatW = "+w+" | quatX = "+x+" | quatY = "+y+" | quatZ = "+z); + + // Converts the quaternion into a float array consisting of: rotation angle in radians, rotation axis x,y,z + float[] axisAngle = orientation.toAxisAngle(); + + float angle = axisAngle[0]; + float vecX = axisAngle[1]; + float vecY = axisAngle[2]; + float vecZ = axisAngle[3]; + + long [] pressed = {0}; // Button press events + long [] released = {0}; // Button release events + + // Reset the values of the quaternions to [1, 0, 0, 0] when the MOVE button is pressed + move.get_button_events(pressed, released); + if ((pressed[0] & Button.Btn_MOVE.swigValue()) != 0) { + move.reset_orientation(); + println("Orientation reset"); + } + + lights(); + pushMatrix(); + translate(height*.5f, width*.5f); + rotate(angle, vecX, vecY, vecZ); // The rotation directions are not accurate right now + fill(255); + box(40, 20, 100); + popMatrix(); + +} + + From b56675afb71a417fbdbc1600be5f1210ca98afa4 Mon Sep 17 00:00:00 2001 From: sableRaf Date: Thu, 14 Feb 2013 23:05:17 +0100 Subject: [PATCH 2/3] Corrected rotation directions --- .../processing/quaternions_to_rotate/quaternions_to_rotate.pde | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde b/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde index d0afa904..f62253ac 100644 --- a/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde +++ b/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde @@ -59,7 +59,7 @@ void draw() { lights(); pushMatrix(); translate(height*.5f, width*.5f); - rotate(angle, vecX, vecY, vecZ); // The rotation directions are not accurate right now + rotate(angle, -vecX, vecY, -vecZ); // The rotation directions for X and Z have to be inverted fill(255); box(40, 20, 100); popMatrix(); From 3cdd7706e2142c5d504b5271834fa4f06649ac46 Mon Sep 17 00:00:00 2001 From: sableRaf Date: Thu, 14 Feb 2013 23:17:04 +0100 Subject: [PATCH 3/3] Corrected translate values and box scale --- .../quaternions_to_rotate/quaternions_to_rotate.pde | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde b/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde index f62253ac..e5849344 100644 --- a/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde +++ b/examples/processing/quaternions_to_rotate/quaternions_to_rotate.pde @@ -58,10 +58,11 @@ void draw() { lights(); pushMatrix(); - translate(height*.5f, width*.5f); + translate(width*.5f, height*.5f); rotate(angle, -vecX, vecY, -vecZ); // The rotation directions for X and Z have to be inverted fill(255); - box(40, 20, 100); + scale(height*.05); + box(4, 2, 10); popMatrix(); }