
Rotating one arbirary vector around another
Hi,
I''ve been trying to get this to work for a couple of hours now and I feel I''m quite lost/dizzy. I found a couple of explanations on the net, but they''re not good enough for my head
. Can someone, please, phrase the equations necessary to rotate v1 around v2 so that v1 would remain perpenticular to v2.y where v2 is an arbitrary vector...
Thanks in advance,
Crispy

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
3d
Crispy
Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Let me make sure I understand your question --
you want to rotate v1 around v2''s y axis?
you want to rotate v1 around v2''s y axis?
The correct terminology is orthogonal to v1.y - saying perpendicular implies 2D.
That''s not enough information to decide how to do it.
You could just perform the general rotation, then project the resultant vector back onto the plane orthogonal to v1.y
That will accomplish what you say, but may not be what you want.
That''s not enough information to decide how to do it.
You could just perform the general rotation, then project the resultant vector back onto the plane orthogonal to v1.y
That will accomplish what you say, but may not be what you want.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
What I mean is that I want to rotate v1 around v2, v1 being the orientation vector and v2 being the up-vector. Just think of a random directional vector v2 that points "up" in a relative space and another vector under a right angle to it, pointing in some direction depending on the rotation angle. It''s like rotating a line around the y axis in a fixed 3d space with y pointing up. I need to rotate the entire system by defining a new up-vector and then find the orientation based on the rotation angle... Hope this makes more sense.
Crispy
Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Here's a function for you. 'q' is the result, I think 'p' is the point to be rotated, theta is the angle an radians and I think 'r' is the arbitrary vector:
Here's one from the Quake2 source:
Here's something you might not know. The three floats that you pass into glRotatef is the vector that the matrix rotates around. So you can rotate the matrix and then multiply your vector against that matrix.
Here's all the code you need to rotate a vector with a 4x4 matrix:
I hope that helps.
[edited by - WhatEver on September 28, 2002 3:25:45 PM]
void ArbitraryRotate(vector3f q, vector3f p, float theta, vector3f r){ float costheta,vSintheta; costheta = (float)cos(theta); vSintheta = (float)sin(theta); q[0]=q[1]=q[2]=0.0f; q[0] += (costheta + (1 - costheta) * r[0] * r[0]) * p[0]; q[0] += ((1 - costheta) * r[0] * r[1] - r[2] * vSintheta) * p[1]; q[0] += ((1 - costheta) * r[0] * r[2] + r[1] * vSintheta) * p[2]; q[1] += ((1 - costheta) * r[0] * r[1] + r[2] * vSintheta) * p[0]; q[1] += (costheta + (1 - costheta) * r[1] * r[1]) * p[1]; q[1] += ((1 - costheta) * r[1] * r2[2] - r[0] * vSintheta) * p[2]; q[2] += ((1 - costheta) * r[0] * r[2] - r[1] * vSintheta) * p[0]; q[2] += ((1 - costheta) * r[1] * r[2] + r[0] * vSintheta) * p[1]; q[2] += (costheta + (1 - costheta) * r[2] * r[2]) * p[2];}
Here's one from the Quake2 source:
void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees ){ float m[3][3]; float im[3][3]; float zrot[3][3]; float tmpmat[3][3]; float rot[3][3]; int i; vec3_t vr, vup, vf; vf[0] = dir[0]; vf[1] = dir[1]; vf[2] = dir[2]; PerpendicularVector( vr, dir ); CrossProduct( vr, vf, vup ); m[0][0] = vr[0]; m[1][0] = vr[1]; m[2][0] = vr[2]; m[0][1] = vup[0]; m[1][1] = vup[1]; m[2][1] = vup[2]; m[0][2] = vf[0]; m[1][2] = vf[1]; m[2][2] = vf[2]; memcpy( im, m, sizeof( im ) ); im[0][1] = m[1][0]; im[0][2] = m[2][0]; im[1][0] = m[0][1]; im[1][2] = m[2][1]; im[2][0] = m[0][2]; im[2][1] = m[1][2]; memset( zrot, 0, sizeof( zrot ) ); zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F; //deg to rad is degrees*0.017453292 zrot[0][0] = cos( DEG2RAD( degrees ) ); zrot[0][1] = sin( DEG2RAD( degrees ) ); zrot[1][0] = -sin( DEG2RAD( degrees ) ); zrot[1][1] = cos( DEG2RAD( degrees ) ); R_ConcatRotations( m, zrot, tmpmat ); R_ConcatRotations( tmpmat, im, rot ); for ( i = 0; i < 3; i++ ) { dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2]; }}
Here's something you might not know. The three floats that you pass into glRotatef is the vector that the matrix rotates around. So you can rotate the matrix and then multiply your vector against that matrix.
Here's all the code you need to rotate a vector with a 4x4 matrix:
inline void MatRotate16f(float* a, float theta, float x, float y, float z){ while(theta>=360) { theta-=360; } while(theta<0) { theta+=360; } theta*=0.017453292f; float c = (float)cos(theta); float s = (float)sin(theta); mat16f Matrix; Matrix[0] = (x * x) * (1.0f - c) + c; Matrix[1] = (y * x) * (1.0f - c) + (z * s); Matrix[2] = (z * x) * (1.0f - c) - (y * s); Matrix[3] = 0.0f; Matrix[4] = (x * y) * (1.0f - c) - (z * s); Matrix[5] = (y * y) * (1.0f - c) + c; Matrix[6] = (z * y) * (1.0f - c) + (x * s); Matrix[7] = 0.0f; Matrix[8] = (x * z) * (1.0f - c) + (y * s); Matrix[9] = (y * z) * (1.0f - c) - (x * s); Matrix[10] = (z * z) * (1.0f - c) + c; Matrix[11] = 0.0f; Matrix[12] = 0.0f; Matrix[13] = 0.0f; Matrix[14] = 0.0f; Matrix[15] = 1.0f; MatMultiply16x16f(a, Matrix);}//assumes 4x4 times 4x4inline void MatMultiply16x16f(float* a, float* b){ mat16f t; //a is the matrix getting transformed, not b t[0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3]; t[1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3]; t[2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3]; t[3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3]; t[4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7]; t[5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7]; t[6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7]; t[7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7]; t[8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11]; t[9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11]; t[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11]; t[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11]; t[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15]; t[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15]; t[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15]; t[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15]; a[0] = t[0]; a[1] = t[1]; a[2] = t[2]; a[3] = t[3]; a[4] = t[4]; a[5] = t[5]; a[6] = t[6]; a[7] = t[7]; a[8] = t[8]; a[9] = t[9]; a[10] = t[10]; a[11] = t[11]; a[12] = t[12]; a[13] = t[13]; a[14] = t[14]; a[15] = t[15];}//assumes 1x4 times 4x4inline void MatMultiply4x16f(float* a, float* b){ mat4f c; c[0]=a[0]*b[0] + a[1]*b[4] + a[2]*b[8] + a[3]*b[12]; c[1]=a[0]*b[1] + a[1]*b[5] + a[2]*b[9] + a[3]*b[13]; c[2]=a[0]*b[2] + a[1]*b[6] + a[2]*b[10] + a[3]*b[14]; c[3]=a[0]*b[3] + a[1]*b[7] + a[2]*b[11] + a[3]*b[15]; a[0]=c[0]; a[1]=c[1]; a[2]=c[2]; a[3]=c[3];}
I hope that helps.
[edited by - WhatEver on September 28, 2002 3:25:45 PM]
This is fabulous! Got it working just the way I needed - lots to do still, but the rotating part looks just great. Thanks, WhatEver!
Crispy
Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement