Vector3 Matrix Multiplication - c++

Lets say we have:
Vector3 vec;//has some random values
Matrix4x4 mat;//has some random values as well
Now I want to multiply them to get a Vector3 out of it.
Vector3 mult = vec * mat;
Would this function fulfill that:
void Matrix4x4::Transform( Vector3 &oPoint )
{
float fX = oPoint.GetX();
float fY = oPoint.GetY();
float fZ = oPoint.GetZ();
oPoint.SetX( fX * this->matrix[0][0] + fY * this->matrix[0][1] + fZ * this->matrix[0][2] + this->matrix[0][3]);
oPoint.SetY( fX * this->matrix[1][0] + fY * this->matrix[1][1] + fZ * this->matrix[1][2] + this->matrix[1][3]);
oPoint.SetZ( fX * this->matrix[2][0] + fY * this->matrix[2][1] + fZ * this->matrix[2][2] + this->matrix[2][3]);
}
If not, can you lead me in the right direction.

Your code seems ok. I assume that, as you are using OpenGL, your matrix is in column-major order, and that when you want to multiply vector of size 3 on 4x4 matrix, your fourth vector coord is 1.
But, as you are obviously doing computer graphics, you should use vectors of size 4 because of homogenous coordinates. Also I don't really understand, why you want to multiply vector on matrix, and not vice versa.

Related

How to rotate a line around one of its vertexes

I am making my first raycasting engine, and would like to rotate a line over an angle θ
How does one do this? Would it be possible to show me some basic C++ code or some pseudocode?
This image describes my problem:
Optional question
I decided to make all of this in graphics.h, because it is the simplest graphics header for C/C++.
You want:
B = P + M * (A - P)
Where M is a 2D rotation matrix:
M = | cos(ϴ) -sin(ϴ) |
| sin(ϴ) cos(ϴ) |
In C++ it could be written as:
float c = cos(theta), s = sin(theta);
float dx = ax - px, dy = ay - py;
float bx = px + c * dx - s * dy;
float by = py + s * dx + c * dy;
One simple algorithm:
Move the circle -P, so that P is at (0, 0).
Rotate A by the angle by multiplying it by the rotation matrix.
Move the circle P to restore its original position.
All these three steps can be done using one 3x3 matrix multiplication.
The scalar product of two vectors have the following property:
vec(PA) . vec(PB) = rho cos theta
Taking the definition of our two vectors:
vec(PA) = (x_a-x_p, y_a-y_p)
vec(PB) = (x_b-x_p, y_b-y_p)
We can get:
(x_a-x_p)(x_b-x_p) + (y_a-y_p)(y_b-y_p) = rho cos theta (1)
Since PA=PB, we also have:
(x_a-x_p)^2 + (y_a-y_p)^2 = (x_b-x_p)^2 + (y_b-y_p)^2 (2)
From (1) and (2) you can derive x_band y_b with some arithmetic autopilot.

How to check if vectors are facing same direction

I am working on a question where I am working within a right-handed coordinate system where the y-axis is straight up. I am supplied a structure that represents a 3-dimensional vector that looks like this:
struct vec{float x; float y; float z; };
I need to write a function that takes in a unit vector representing north and a unit vector represent a player's forward vector, and return if they are facing more north than south. Unfortunatly I have no idea where to go from here, I believe I have to do something like:
PlayerDirection = sqrt((PlayerVector.x *= PlayerVector.x)
+ (PlayerVector.y *= PlayerVector.y)
+ (PlayerVector.z *= PlayerVector.z));
But I do not know where to go from here. Any help/explanation would help, thanks.
apply a dot product to both vectors. The dot product will be positive if the angle between both vectors is smaller than 90 degrees, and negative otherwise.
The formula you include (and which contains an error - the product is "*", not "*=") gives you the entity of the movement - the length of the vector. Which you already know, since it is a unit vector and therefore has length 1.
You need instead to perform the dot product between the two vectors. You get 1 if the two unit vectors are completely aligned (parallel), -1 if they're antiparallel, and zero if they're normal to each other.
"More north than south" means that the scalar product is positive, so:
return if they are facing more north than south
Alignment = ((PlayerVector.x * NorthVector.x)
+ (PlayerVector.y * NorthVector.y)
+ (PlayerVector.z * NorthVector.z));
return (Alignment > 0);
Questions
What if I wanted to tell if it was facing east/west?
The dot product tells you how much two vectors are aligned. It is the same formula shown by Kevin Glasson, without the unit vectors norms since they are 1, and dividing by one doesn't change anything.
So, you can't use it to tell where a vector is facing except in terms of another vector. And that's why you're given the north vector; with the player vector alone you can't run a dot product. To tell whether the player is facing east, you need the east vector (or the west vector, and then take the opposite sign).
So if the number comes back as say 35 it means it is facing more north than south but why?
Why it is so: you can find it explained on the Wikipedia page, the dot product is equal to the product of the two lengths by the cosine of their angle. Since the lengths are both 1, it is just the cosine. The cosine varies between 1 and -1 (so you can't ever get 35). When the cosine is 1, it means that the angle is zero and the vectors are aligned; when it is -1, they're opposite. A cosine of zero means the vectors are normal to each other, i.e. form an angle of 90°, and in this case it means that the player is facing either East, West, or Up, or Down - but it doesn't tell you which.
I think what would work is taking the 'dot product' of the vectors. Using the following form:
Where you would re-arrange for theta. This would give you the angle between the two vectors.
In my mind at least if the angle was 0 then you are pointing exactly north and if the angle were greater than 90 then you were facing more south.
I don't know exactly how you intend to use this, but this should work for being able to tell North from South in 3D space.
Well you can use the dot-product to get the angle between 2 vectors. The formula is the following:
cos(phi) = (a * b) / (|a|*|b|)
Which converts to this:
phi = acos((ax*bx + ay*by + az * bz) / (sqrt(ax^2 + ay^2 + az^2)+sqrt(bx^2 + by^2 + bz^2)))
Now the dot product is symmetric, meaning: (1,1,1)(2,2,2) gives you the same result as (2,2,2)(1,1,1). Hence you have to add a further step. By adding a third vector, whose angle to the first given vector you do know, you can verify the true angle between the two vectors. Optaining this reference vector can be done by turning the first vector arround an axis, now to ensure it is a valid reference it has to be in the same plane as the vectors a and b.
This means that your axis to turn your first vector arround has to be orthogonal to vector 1 and 2, the vectorproduct of 2 vectors gives a vector orthogonal to both, so we will use the so obtained vector as axis.
axis = a x b
This is equal to:
axis = (aybz-azby, azbx-axbz, axby-aybx)
To turn a vector by a given amount arround an axis the following has to be done:
double R[3][3] = {};
Vector axis = Axis.getUnitVector();
double deg = degrees / 180 * M_PI;
R[0][0] = axis.X * axis.X * (1 - cos(deg)) + cos(deg); R[0][1] = axis.X * axis.Y * (1 - cos(deg)) - axis.Z * sin(deg); R[0][2] = axis.X * axis.Z * (1 - cos(deg)) + axis.Y * sin(deg);
R[1][0] = axis.Y * axis.X * (1 - cos(deg)) + axis.Z * sin(deg); R[1][1] = axis.Y * axis.Y * (1 - cos(deg)) + cos(deg); R[1][2] = axis.Y * axis.Z * (1 - cos(deg)) - axis.X * sin(deg);
R[2][0] = axis.Z * axis.X * (1 - cos(deg)) - axis.Y * sin(deg); R[2][1] = axis.Z * axis.Y * (1 - cos(deg)) + axis.X * sin(deg); R[2][2] = axis.Z * axis.Z * (1 - cos(deg)) + cos(deg);
double x = this->X * R[0][0] + this->Y * R[0][1] + this->Z * R[0][2];
double y = this->X * R[1][0] + this->Y * R[1][1] + this->Z * R[1][2];
double z = this->X * R[2][0] + this->Y * R[2][1] + this->Z * R[2][2];
The unitvector is defined as following:
e_a = (ax / sqrt(ax^2+ay^2+az^2),ay / sqrt(ax^2+ay^2+az^2),az / sqrt(ax^2+ay^2+az^2))
Using this new axis we can rotate our first vector a 90°. By calculating the angle between our reference and our second vector we can now evaluate the real angle between the first and second vector. If the angle to the reference is bigger than 90° the 2nd vector is in the 3rd or 4th sector or a cartesian coordinate system, meaning to get the real angle we'll have to substract our aquired angle between the first and the 2nd vector from 360°. If the angle to the reference it is smaller than 90° the calculated angle is the real angle.
Now there is another issue, what/where is north? If we know north we could just calculate the angle between north and the two vectors and the one with the smaller angle would be more north. This means there is no reason to evaluate a reference vector or build and apply the rotation matrix.
In case of a fixed north you could also project your vectors on a plane containing north, and simplify the calculations required.
Provide more information and I will edit this.
edit:/ since you provide the north and a player vector, just calculate the angle between them.

Composing quaternions

I'm currently working on a 3D scene which has to be rotated by dragging the mouse. To do this, I'm utilizing a ArcBall implementation.
The used framework, immediately transformed the resulting quaternion into a rotation matrix. Any subsequent rotations would also be converted into a matrix. These matrices in turn are multiplied.
I wanted to optimize this by simply multiplying the rotation quaternions. Now the scene seems to be spinning uncontrollably while the original version worked okay(ish).
My code for multiplying quaternions:
public static Quat4f mul(Quat4f q1, Quat4f q2) {
float x = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;
float y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
float z = q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;
float w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;
return new Quat4f(x,y,z,w);
}
The multiplication is done as (the result is normalized for good measure):
currentRotation = Quat4f.mul(newRotation, currentRotation);
An finally, the rotation is applied as:
glMultMatrixf(currentRotation.toMatrix());
Does anyone know what I'm doing wrong?
glMultMatrixf (...) multiplies the current matrix by the one you pass it. Now here is where things get kind of funny, you have a Quaternion that also seems to be cumulative (named currentRotation). You are accumulating the rotation in two different places.
Chances are, what you actually want is glLoadMatrixf (...). Rather than multiplying the current matrix by your rotation matrix, that will replace the matrix. You could achieve the same result by calling glLoadIdentity (...) immediately before glMultMatrixf (...); the bottom line is you need to discard any old rotation in your matrix before applying this.
It is not clear whether the behavior you are after is such that GL's matrix is reset every time it is updated or if the quaternion is reset, but you really need to reset one of the two or you will spin endlessly.

Making a 3D graphics engine, my Translation matrix doesn't work for positions equal to 0

Hi I'm making a 3D graphics engine for an assignment that is due later tonight, it's going smoothly at the moment except I'm loading a cube model from an .obj file, the positions start at 0.
My transformation matrix works for numbers that don't = 0. I mean if X = 0 and I try to translate it by 10 on the X Axis, it returns 0.
Matrix * Vector:
Vec4 Mat4::operator*(const Vec4& v) const
{
Vec4 tmp(0, 0, 0, 0, 255, 255, 255, 255);
tmp.x = (this->data[0] * v.x) + (this->data[4] * v.y) + (this->data[8] * v.z) + (this->data[12] * v.w);
tmp.y = (this->data[1] * v.x) + (this->data[5] * v.y) + (this->data[9] * v.z) + (this->data[13] * v.w);
tmp.z = (this->data[2] * v.x) + (this->data[6] * v.y) + (this->data[10] * v.z) + (this->data[14] * v.w);
tmp.w = (this->data[3] * v.x) + (this->data[7] * v.y) + (this->data[11] * v.z) + (this->data[15] * v.w);
return tmp;
}
Translate Matrix:
Mat4 Mat4::translate(float x, float y, float z)
{
Mat4 tmp;
tmp.data[12] = x;
tmp.data[13] = y;
tmp.data[14] = z;
return tmp;
}
A Mat4 class by default is an identity matrix.
It is too late now, but... it might be helpful to know the following:
A vector strictly equal to 0.0 (e.g. <0,0,0,0>) cannot be translated using matrix multiplication and technically should not be considered a position in this context. In fact, such a vector is not even representative of a direction because it has 0 length. It is simply zero; there are not a whole lot of uses for a vector that cannot be rotated or translated.
You can rotate vectors with 0.0 for the W coordinate, but the value 0.0 for W prevents translation.
Generally you want a W coordinate of 1.0 for spatial (e.g. position) vectors and 0.0 for directional (e.g. normal).
If you want to understand this better, you need to consider how your 4x4 matrix is setup. The first 3 rows or columns (depending on which convention you use) store rotation, and the 4th stores translation.
Consider how translation is applied when you multiply your matrix and vector:
x = ... + (this->data[12] * v.w);
y = ... + (this->data[13] * v.w);
z = ... + (this->data[14] * v.w);
w = ... + (this->data[15] * v.w);
If v.w is 0.0, then translation evaluates to 0.0 for all coordinates.

Rotate a vector about another vector

I am writing a 3d vector class for OpenGL. How do I rotate a vector v1 about another vector v2 by an angle A?
You may find quaternions to be a more elegant and efficient solution.
After seeing this answer bumped recently, I though I'd provide a more robust answer. One that can be used without necessarily understanding the full mathematical implications of quaternions. I'm going to assume (given the C++ tag) that you have something like a Vector3 class with 'obvious' functions like inner, cross, and *= scalar operators, etc...
#include <cfloat>
#include <cmath>
...
void make_quat (float quat[4], const Vector3 & v2, float angle)
{
// BTW: there's no reason you can't use 'doubles' for angle, etc.
// there's not much point in applying a rotation outside of [-PI, +PI];
// as that covers the practical 2.PI range.
// any time graphics / floating point overlap, we have to think hard
// about degenerate cases that can arise quite naturally (think of
// pathological cancellation errors that are *possible* in seemingly
// benign operations like inner products - and other running sums).
Vector3 axis (v2);
float rl = sqrt(inner(axis, axis));
if (rl < FLT_EPSILON) // we'll handle this as no rotation:
{
quat[0] = 0.0, quat[1] = 0.0, quat[2] = 0.0, quat[3] = 1.0;
return; // the 'identity' unit quaternion.
}
float ca = cos(angle);
// we know a maths library is never going to yield a value outside
// of [-1.0, +1.0] right? Well, maybe we're using something else -
// like an approximating polynomial, or a faster hack that's a little
// rough 'around the edge' cases? let's *ensure* a clamped range:
ca = (ca < -1.0f) ? -1.0f : ((ca > +1.0f) ? +1.0f : ca);
// now we find cos / sin of a half-angle. we can use a faster identity
// for this, secure in the knowledge that 'sqrt' will be valid....
float cq = sqrt((1.0f + ca) / 2.0f); // cos(acos(ca) / 2.0);
float sq = sqrt((1.0f - ca) / 2.0f); // sin(acos(ca) / 2.0);
axis *= sq / rl; // i.e., scaling each element, and finally:
quat[0] = axis[0], quat[1] = axis[1], quat[2] = axis[2], quat[3] = cq;
}
Thus float quat[4] holds a unit quaternion that represents the axis and angle of rotation, given the original arguments (, v2, A).
Here's a routine for quaternion multiplication. SSE/SIMD can probably speed this up, but complicated transform & lighting are typically GPU-driven in most scenarios. If you remember complex number multiplication as a little weird, quaternion multiplication is more so. Complex number multiplication is a commutative operation: a*b = b*a. Quaternions don't even preserve this property, i.e., q*p != p*q :
static inline void
qmul (float r[4], const float q[4], const float p[4])
{
// quaternion multiplication: r = q * p
float w0 = q[3], w1 = p[3];
float x0 = q[0], x1 = p[0];
float y0 = q[1], y1 = p[1];
float z0 = q[2], z1 = p[2];
r[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
r[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
r[1] = w0 * y1 + y0 * w1 + z0 * x1 - x0 * z1;
r[2] = w0 * z1 + z0 * w1 + x0 * y1 - y0 * x1;
}
Finally, rotating a 3D 'vector' v (or if you prefer, the 'point' v that the question has named v1, represented as a vector), using the quaternion: float q[4] has a somewhat strange formula: v' = q * v * conjugate(q). Quaternions have conjugates, similar to complex numbers. Here's the routine:
static inline void
qrot (float v[3], const float q[4])
{
// 3D vector rotation: v = q * v * conj(q)
float r[4], p[4];
r[0] = + v[0], r[1] = + v[1], r[2] = + v[2], r[3] = +0.0;
glView__qmul(r, q, r);
p[0] = - q[0], p[1] = - q[1], p[2] = - q[2], p[3] = q[3];
glView__qmul(r, r, p);
v[0] = r[0], v[1] = r[1], v[2] = r[2];
}
Putting it all together. Obviously you can make use of the static keyword where appropriate. Modern optimising compilers may ignore the inline hint depending on their own code generation heuristics. But let's just concentrate on correctness for now:
How do I rotate a vector v1 about another vector v2 by an angle A?
Assuming some sort of Vector3 class, and (A) in radians, we want the quaternion representing the rotation by the angle (A) about the axis v2, and we want to apply that quaternion rotation to v1 for the result:
float q[4]; // we want to find the unit quaternion for `v2` and `A`...
make_quat(q, v2, A);
// what about `v1`? can we access elements with `operator [] (int)` (?)
// if so, let's assume the memory: `v1[0] .. v1[2]` is contiguous.
// you can figure out how you want to store and manage your Vector3 class.
qrot(& v1[0], q);
// `v1` has been rotated by `(A)` radians about the direction vector `v2` ...
Is this the sort of thing that folks would like to see expanded upon in the Beta Documentation site? I'm not altogether clear on its requirements, expected rigour, etc.
This may prove useful:
double c = cos(A);
double s = sin(A);
double C = 1.0 - c;
double Q[3][3];
Q[0][0] = v2[0] * v2[0] * C + c;
Q[0][1] = v2[1] * v2[0] * C + v2[2] * s;
Q[0][2] = v2[2] * v2[0] * C - v2[1] * s;
Q[1][0] = v2[1] * v2[0] * C - v2[2] * s;
Q[1][1] = v2[1] * v2[1] * C + c;
Q[1][2] = v2[2] * v2[1] * C + v2[0] * s;
Q[2][0] = v2[0] * v2[2] * C + v2[1] * s;
Q[2][1] = v2[2] * v2[1] * C - v2[0] * s;
Q[2][2] = v2[2] * v2[2] * C + c;
v1[0] = v1[0] * Q[0][0] + v1[0] * Q[0][1] + v1[0] * Q[0][2];
v1[1] = v1[1] * Q[1][0] + v1[1] * Q[1][1] + v1[1] * Q[1][2];
v1[2] = v1[2] * Q[2][0] + v1[2] * Q[2][1] + v1[2] * Q[2][2];
Use a 3D rotation matrix.
The easiest-to-understand way would be rotating the coordinate axis so that vector v2 aligns with the Z axis, then rotate by A around the Z axis, and rotate back so that the Z axis aligns with v2.
When you have written down the rotation matrices for the three operations, you'll probably notice that you apply three matrices after each other. To reach the same effect, you can multiply the three matrices.
I found this here:
http://steve.hollasch.net/cgindex/math/rotvec.html
let
[v] = [vx, vy, vz] the vector to be rotated.
[l] = [lx, ly, lz] the vector about rotation
| 1 0 0|
[i] = | 0 1 0| the identity matrix
| 0 0 1|
| 0 lz -ly |
[L] = | -lz 0 lx |
| ly -lx 0 |
d = sqrt(lx*lx + ly*ly + lz*lz)
a the angle of rotation
then
matrix operations gives:
[v] = [v]x{[i] + sin(a)/d*[L] + ((1 - cos(a))/(d*d)*([L]x[L]))}
I wrote my own Matrix3 class and Vector3Library that implemented this vector rotation. It works absolutely perfectly. I use it to avoid drawing models outside the field of view of the camera.
I suppose this is the "use a 3d rotation matrix" approach. I took a quick look at quaternions, but have never used them, so stuck to something I could wrap my head around.