I have a model rotated by a quaternion. I can only set the rotation, I can't add or subtract from anything. I need to get the value of an axis, and than add an angle to it (maybe a degree or radian?) and than re-add the modified quaternion.
How can I do this? (answer on each axis).
You can multiply two quaternions together to produce a third quaternion that is the result of the two rotations. Note that quaternion multiplication is not commutative, meaning order matters (if you do this in your head a few times, you can see why).
You can produce a quaternion that represents a rotation by a given angle around a particular axis with something like this (excuse the fact that it is c++, not java):
Quaternion Quaternion::create_from_axis_angle(const double &xx, const double &yy, const double &zz, const double &a)
{
// Here we calculate the sin( theta / 2) once for optimization
double factor = sin( a / 2.0 );
// Calculate the x, y and z of the quaternion
double x = xx * factor;
double y = yy * factor;
double z = zz * factor;
// Calcualte the w value by cos( theta / 2 )
double w = cos( a / 2.0 );
return Quaternion(x, y, z, w).normalize();
}
So to rotate around the x axis for example, you could create a quaternion with createFromAxisAngle(1, 0, 0, M_PI/2) and multiply it by the current rotation quaternion of your model.
Made a runable code from sje397's post for testing other details later, thought I would share. Eventually using C the reason for no Quaternion class.
#include <iostream>
#include <math.h>
using namespace std;
struct float4{
float x;
float y;
float z;
float w;
};
float4 make_float4(float x, float y, float z, float w){
float4 quat = {x,y,z,w};
return quat;
}
float dot(float4 a)
{
return (((a.x * a.x) + (a.y * a.y)) + (a.z * a.z)) + (a.w * a.w);
}
float4 normalize(float4 q)
{
float num = dot(q);
float inv = 1.0f / (sqrtf(num));
return make_float4(q.x * inv, q.y * inv, q.z * inv, q.w * inv);
}
float4 create_from_axis_angle(const float &xx, const float &yy, const float &zz, const float &a)
{
// Here we calculate the sin( theta / 2) once for optimization
float factor = sinf( a / 2.0f );
float4 quat;
// Calculate the x, y and z of the quaternion
quat.x = xx * factor;
quat.y = yy * factor;
quat.z = zz * factor;
// Calcualte the w value by cos( theta / 2 )
quat.w = cosf( a / 2.0f );
return normalize(quat);
}
int main()
{
float degrees = 10.0f;
float4 quat = create_from_axis_angle(1, 0, 0, degrees*(3.14159f/180.0f));
cout << "> (" << quat.x << ", " <<quat.y << ", " <<quat.z << ", " <<quat.w << ")" << endl;
return 0;
}
Output
(0.0871557, 0, 0, 0.996195)
Related
I'm creating a Quaternion from input from a serial device. In Processing I rotate around the x-axis in the code below. My Quaternion object takes the input and uses the set function to set the values, euler angles, and normalize. Is there something wrong with the math?
I commented out rotation for z and y, but basically the object doesn't rotate around very well or is jerky compared to the x-axis, which works perfectly. What am I doing wrong in the code below?
For reference, the shape(model) line below is the loaded 3d object from a .obj file loaded in using loadShape and the shape function displays it in the draw loop.
Quaternion q = new Quaternion(s);
q.set(x, y, z, w);
q = q.Euler(q.eulerAngles);
translate(x, y);
rotateX(q.eulerAngles.x);
//rotateY(q.eulerAngles.y);
//rotateZ(q.eulerAngles.z);
shape(model);
rotateX(-q.eulerAngles.x);
translate(-x, -y);
This is part of the Quaternion class:
public class Quaternion {
PApplet s;
public float w,x,y,z;
public PVector eulerAngles;
public Quaternion(PApplet s, float x, float y, float z, float w){
this.s = s;
this.x = x;
this.y = y;
this.z = z;
this.w = w;
normalize();
}
public Quaternion(Quaternion q){
this.s = q.s;
this.w = q.w;
this.x = q.x;
this.y = q.y;
this.z = q.z;
}
public Quaternion normalize() {
float magnitude = w*w + x*x + y*y + z*z;
if(magnitude != 0.0 && magnitude != 1.0){
magnitude = 1.0f / s.sqrt(magnitude);
w *= magnitude;
x *= magnitude;
y *= magnitude;
z *= magnitude;
}
eulerAngles = setEulerAngles();
return this;
}
public Quaternion set(float x, float y, float z, float w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return normalize();
}
// Returns a rotation that rotates z degrees around
// the z axis, x degrees around the x axis, and y
// degrees around the y axis.
public Quaternion Euler(){
float roll = eulerAngles.x;
float pitch = eulerAngles.y;
float yaw = eulerAngles.z;
float cr = (float)Math.cos(roll * 0.5);
float sr = (float)Math.sin(roll * 0.5);
float cp = (float)Math.cos(pitch * 0.5);
float sp = (float)Math.sin(pitch * 0.5);
float cy = (float)Math.cos(yaw * 0.5);
float sy = (float)Math.sin(yaw * 0.5);
w = cy*cr*cp + sy*sr*sp;
x = cy*sr*cp - sy*cr*sp;
y = cy*cr*sp + sy*sr*cp;
z = sy*cr*cp - cy*sr*sp;
return normalize();
}
// set euler angle representation of
// the rotation in 3-dim PVector
private PVector setEulerAngles(){
// roll: x-axis rotation
float sinr = 2.0f * (w*x + y*z);
float cosr = 1.0f - 2.0f * (x*x + y*y);
float roll = (float)Math.atan2(sinr, cosr);
// pitch: y-axis rotation
float sinp = 2.0f * (w*y - z*x);
float pitch = 0.0f;
if(Math.abs(sinp) >= 1){
pitch = (float)Math.copySign(Math.PI/2, sinp);
} else {
pitch = (float)Math.asin(sinp);
}
// yaw: z-axis rotation
float siny = 2.0f * (w*z + x*y);
float cosy = 1.0f - 2.0f * (y*y + z*z);
float yaw = (float)Math.atan2(siny, cosy);
return new PVector(roll, pitch, yaw);
}
}
As far as I can tell, the Euler angles that you get from your method should be applied in ZYX order rather than XYZ. But anyway, do not mess around with Euler angles unless you really have to. And in this case you don't.
Instead, convert the quaternion to a rotation matrix and apply this transform using applyMatrix(). There will be no ambiguity here.
To revert a transform, do not apply the inverse transform (like you did with rotateX(-q.eulerAngles.x) and translate(-x, -y)). It is very easy to confuse the order or forget a transform during development. Instead, use pushMatrix() / popMatrix() or resetMatrix.
Btw, I find the definition of your quaternion class very confusing. Some methods return values that I would not expect to return anything (e.g. normalize()). Furthermore, I do not think that having an Euler angle representation stored with the quaternion is a good idea. And even if you think it is, I don't understand the purpose of the method Euler() since it neither has parameters, nor can you set the Euler angles from outside.
I want to change the color of the strokes but I can't figure out how to do this correctly I looked up a tutorial and it showed me the hu stuff in the code. That it changes the hu in the for loop but it just remains one color.. In my case yellowish
void setup(){
size(500,500);
colorMode(HSB);
}
float t = 0;
float tn = 0;
void draw(){
background(0);
translate(width / 2, height / 2);
noFill();
stroke(255);
strokeWeight(2);
float hu = 0;
beginShape();
//add vertices...
for(float theta = 0; theta <= 8 * PI; theta += 0.001){
float rad = r(theta,
1, //a
1, //b
sin(tn) * 0.1 + 5, //m
cos(tn) / 2, //n1
sin(t) * 0.5 + 0.5, //n2
cos(t) * 0.5 + 0.5 //n3
);
float x = rad * cos(theta) * 50;
float y = rad * sin(theta) * 50;
stroke(hu, 255, 255);
vertex(x,y);
hu += 1;
if(hu > 255){
hu = 0;
}
}
endShape();
t += 0.1;
tn += 0.1;
}
float r(float theta, float a, float b, float m, float n1, float n2, float n3){
return pow(pow(abs(cos(m * theta / 4.0) / a), n2) +
pow(abs(sin(m * theta / 4.0) / b), n3), -1.0 / n1) ;
}
Please consult the Processing reference for the beginShape function:
The P2D and P3D renderers allow stroke() and fill() to be altered on a per-vertex basis, but the default renderer does not.
In other words, you can't change the stroke color like this with the default renderer. You could just use the P2D renderer instead:
size(500, 500, P2D);
If you need to use the default renderer for some reason, then you're going to have to draw the lines yourself instead of relying on the vertex function.
Ok, we are not talking about OpenGL with this question, but this will be used with OpenGL ES 2.0.
Question: How do create and rotate a Quaternion with the following code?
I have been reading up and studying about this and still can't quite gasp the concepts. I thought I understood it, but once I started making some calculations to rotate the quaternion I realized I can't even get back to where I started.
So let us say that we have a cube, and the center of it is at (0, 0, 0). We want to rotate it on the x-axis by 45 degrees. What would I do? (Only the Quaternion)
Assuming success, how would you get the amount of rotation from 'W'? I know that '1' indicates that there is no rotation, but what if it was rotated 173 degrees?
Trying to rotate to a given direction, 45 degrees, and then get that value from W. I feel like I need to convert the angle to rads or something, but not exactly sure. Tutorials online vary from one to the next.
Here is my code:
import java.util.Scanner;
import Quaternion;
public class Main {
public static void main(String[] args) {
Quaternion q1 = new Quaternion(0, 0, 0, 1);
Quaternion q2 = new Quaternion(0, 0, 0, (float) Math.cos(toRAD(45.0f) / 2));
q1 = q2.mul(q1);
System.out.println("q1: " + q1.getX() + ", " + q1.getY() + ", " + q1.getZ() + " with " + toANGLE(2.0f * Math.acos(q1.getW())));
}
private static double toRAD(float angle) {
return angle * (Math.PI / 180.0f);
}
private static float toANGLE(double rad) {
return (float) (rad * (180.0f / Math.PI));
}
}
Here is the code for a Quaternion:
public class Quaternion // Credit goes to 'thebennybox' (http://www.youtube.com/user/thebennybox)
{
private float x;
private float y;
private float z;
private float w;
public Quaternion(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public float length()
{
return (float)Math.sqrt(x * x + y * y + z * z + w * w);
}
public Quaternion normalize()
{
float length = length();
return new Quaternion(x / length, y / length, z / length, w / length);
}
public Quaternion conjugate()
{
return new Quaternion(-x, -y, -z, w);
}
public Quaternion mul(Quaternion r)
{
float w_ = w * r.getW() - x * r.getX() - y * r.getY() - z * r.getZ();
float x_ = x * r.getW() + w * r.getX() + y * r.getZ() - z * r.getY();
float y_ = y * r.getW() + w * r.getY() + z * r.getX() - x * r.getZ();
float z_ = z * r.getW() + w * r.getZ() + x * r.getY() - y * r.getX();
return new Quaternion(x_, y_, z_, w_);
}
public Quaternion mul(Vector3f r)
{
float w_ = -x * r.getX() - y * r.getY() - z * r.getZ();
float x_ = w * r.getX() + y * r.getZ() - z * r.getY();
float y_ = w * r.getY() + z * r.getX() - x * r.getZ();
float z_ = w * r.getZ() + x * r.getY() - y * r.getX();
return new Quaternion(x_, y_, z_, w_);
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getZ()
{
return z;
}
public void setZ(float z)
{
this.z = z;
}
public float getW()
{
return w;
}
public void setW(float w)
{
this.w = w;
}
}
I'm still not 100% sure what your question is asking, but I'll give it a shot.
Problem: Given a quaternion representing a 0 degree rotation about x, y, z, generate a new quaternion representing a 45 degree rotation about the x axis
Start with a quaternion representing no rotation, call it q1
q1 = (w1, x1, y1, z1)
q1.w1 = cos(0/2) = 1
q1.x1 = 0 * sin(0/2) = 0
q1.y1 = 0 * sin(0/2) = 0
q1.z1 = 0 * sin(0/2) = 0
So q1 = (1, 0, 0, 0)
Generate a new rotation that is 45 degrees (PI/4 radians) about the X axis
We need a temporary quaternion to modify q1. Let's call it q2.
q2 = (w2, x2, y2, z2)
q2.w2 = cos(PI/4 / 2) = cos(PI/8)
q2.x2 = 1.0 * sin(PI/4 / 2) = 1.0 * sin(PI/8) = sin(PI/8)
q2.y2 = 0.0 * sin(PI/4 / 2) = 0.0
q2.z2 = 0.0 * sin(PI/4 / 2) = 0.0
so q2 = (cos(PI/8), sin(PI/8), 0, 0)
Now this last step is important, you modify your original quaternion by a left-hand multiplication of the temporary quaternion
What I mean is this:
q1 = q2 * q1
Your multiplication function is written correctly, so the problem is not there. Remember that quaternion multiplications are not commutative. That is q2 * q1 is NOT the same as q1*q2!
At this point q1 is modified to represent a 45 degree rotation about the X axis.
To print out the angle in degrees, you need to compute 2.0 * acos(q1.w) / PI * 180
Your code is incorrectly computing q1.w/PI * 180 to get the angle in degrees.
More specifically, change
toANGLE(resQuat.getW())
to
toANGLE(2.0f * Math.acos(resQuat.getW()))
I haven't looked at your code beyond that, but try applying these concepts and see if that fixes your problem.
I created a class with a method drawSphere to replace glutDrawSolidSphere. See code below.
But I wonder, how do I wrap a texture around it without tiling? For example, if I want to draw a mouth, eyes and a nose on it, then I want it to have only one mouth, two eyes and one nose, and not 100 tiled all over the sphere.
I'm using Jogl with some libraries.
class Shape {
public void drawSphere(double radius, int slices, int stacks) {
gl.glEnable(GL_TEXTURE_2D);
head.bind(gl); //This method is a shorthand equivalent of gl.glBindTexture(texture.getTarget(), texture.getTextureObject());
gl.glBegin(GL_QUADS);
double stack = (2*PI)/stacks;
double slice = (2*PI)/slices;
for (double theta = 0; theta < 2 * PI; theta += stack) {
for (double phi = 0; phi < 2 * PI; phi += slice) {
Vector p1 = getPoints(phi, theta, radius);
Vector p2 = getPoints(phi + slice, theta, radius);
Vector p3 = getPoints(phi + slice, theta + stack, radius);
Vector p4 = getPoints(phi, theta + stack, radius);
gl.glTexCoord2d(0, 0);
gl.glVertex3d(p1.x(), p1.y(), p1.z());
gl.glTexCoord2d(1, 0);
gl.glVertex3d(p2.x(), p2.y(), p2.z());
gl.glTexCoord2d(1, 1);
gl.glVertex3d(p3.x(), p3.y(), p3.z());
gl.glTexCoord2d(0, 1);
gl.glVertex3d(p4.x(), p4.y(), p4.z());
}
}
gl.glEnd();
gl.glDisable(GL_TEXTURE_2D);
}
Vector getPoints(double phi, double theta, double radius) {
double x = radius * cos(theta) * sin(phi);
double y = radius * sin(theta) * sin(phi);
double z = radius * cos(phi);
return new Vector(x, y, z);
}
}
You could just map latitude and longitude directly to the texture co-ordinates.
for (double theta = 0; theta < 2 * PI; theta += stack) {
for (double phi = 0; phi < 2 * PI; phi += slice) {
Just scale theta and phi to be between 0 and 1.
double s0 = theta / (2 * PI);
double s1 = (theta + stack) / (2 * PI);
double t0 = phi / (2 * PI);
double t1 = (phi + slice) / (2 * PI);
And use s0,s1,t0,t1 in place of 0 and 1 in your texCoord() calls.
I am having an issue with my program; currently it rotates around a set point, and can rotate models around it. Of course, this is a problem as I want it to be a first-person perspective, and currently, it rotates around a point in front of the viewer, instead of the perspective of the viewer. Here is the trigonometric calculations:
protected void drawWireframe(Graphics g) {
double theta = Math.PI * -azimuth / 180.0D;
double phi = Math.PI * elevation / 180.0D;
float cosT = (float) Math.cos(theta);
float sinT = (float) Math.sin(theta);
float cosP = (float) Math.cos(phi);
float sinP = (float) Math.sin(phi);
float cosTcosP = cosT * cosP;
float cosTsinP = cosT * sinP;
float sinTcosP = sinT * cosP;
float sinTsinP = sinT * sinP;
float near = 6.0F;
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < tiles.size(); i++) {
Point[] points = new Point[vertices.length];
for (int j = 0; j < points.length; j++) {
float x0 = -(tiles.get(i).getX() + xmod + vertices[j]
.getX());
float y0 = (tiles.get(i).getY() + ymod + vertices[j].getY());
float z0 = -(tiles.get(i).getZ() + zmod + vertices[j]
.getZ());
float x1 = cosT * x0 + sinT * z0;
float y1 = -sinTsinP * x0 + cosP * y0 + cosTsinP * z0;
float z1 = cosTcosP * z0 - sinTcosP * x0 - sinP * y0;
if (z1 + near > 0) {
x1 = x1 * near / (z1 + near);
y1 = y1 * near / (z1 + near);
points[j] = new Point((int) (Math.max(getWidth(),
getHeight()) / 2 - (Math.max(getWidth(),
getHeight()) / near) * x1), (int) (Math.max(
getWidth(), getHeight()) / 2 - (Math.max(
getWidth(), getHeight()) / near) * y1));
}
}
}
}
How would I go about moving the rotational point without actually modifying the xmod, ymod and zmod (these are used for movements like jumping, walking, running, crouching... etc)
I know how to figure out how to get the new x, y and z positions, I just don't know how to apply them; if I add them to the mods, it creates a weird loop-d-loop. If I add them to the x1, y1, z1's it doesn't cover the z not rotating from the perspective.
To change the rotation point, you effectively need three transforms:
Translate the coordinate system so that the rotation point becomes the origin.
Perform a rotation around the origin
Translate the coordinate system back again.
This can be factored a number of ways, but that's the basic priniciple: translate->rotate->translate.
The way you "move the rotation point" of an object is by translating the object so that the rotation point is at the origin; do the rotation; then translate the object back. All of this is done in memory, between frames - the user never actually sees the object moving to the origin and back.
By the way, all this stuff is significantly easier if you understand vectors and matrix transformations - as you've seen yourself, without them the code can get out of hand.
Using vectors/matrices, all your code above could be reduced to only a few lines.