• 13
• 16
• 27
• 9
• 9

# Two vector questions

This topic is 3716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys! My first question is about a vector with 4 values (x,y,z,w). I already have most of it implented in a math library, I just couldn't figure out how I was supposed to make a cross product of two vector4's... Could somebody show me an example of how to do something like that? My second question concerns a multiplication between a vector3 and a 4x4 matrix. If you look at it in a pure mathematicly, this multiplication wouldn't be possible. But it is possible in a game-designing way, right? Thanks in advance! -Max Henger

##### Share on other sites
Quote:
 I just couldn't figure out how I was supposed to make a cross product of two vector4's...

The cross product is only defined for 3D vectors.

Quote:
 My second question concerns a multiplication between a vector3 and a 4x4 matrix. If you look at it in a pure mathematicly, this multiplication wouldn't be possible. But it is possible in a game-designing way, right?

Yes. If you assume that the 4th element is 1, it will be transformed like a point (i.e., with translation). If you assume that the 4th element is 0, it will not be translated, which is appropriate for transforming vectors that represent directions and not positions (like normals).

You'll probably want your vector-matrix multiplication functions to do the formar, and have special functions (like transformNormal()) to do the latter.

##### Share on other sites

Now I was wondering, let's say I want to implent this function in SSE. The matrix4 is already 16-byte aligned, so I can move it in an SSE register with movups. However, when I move the (also 16-byte aligned) vector3 in the register... I don't have the 1, or 0 yet.

There are two options (I can think of):
1. Move the 1, or 0 to the 12th place of the SSE register
mov esi, vector3mov edi, floatWithValueZeromov [esi + 12], [edi]

2. Make a new vector4 (and a new constructor, so more time spent)
ShtrVector4(ShtrVector3.x, ShtrVector3.y, ShtrVector3.z, 0.0f)__asm {   //use code in here}

What would be the best option? Or do you advise me just to profile both of them? (and compare them to one without SSE code)

##### Share on other sites
Quote:
 Original post by MadMax1992My first question is about a vector with 4 values (x,y,z,w). I already have most of it implented in a math library, I just couldn't figure out how I was supposed to make a cross product of two vector4's... Could somebody show me an example of how to do something like that?

Four Space Visualization
Here is a link showing how to do a four dimensional cross product. Keep in mind that you need three vectors in order to get the result. I really can't see an application to gaming for it. It is what you asked for but I don't really know if it is what you are looking for. I went ahead and put it up just for the heck of it.

##### Share on other sites
I think you've already got the idea, but I'll just give you an idea of how my matrix and vector classes handle this issue.

In my library, I support 4x4, 3x3, and 2x2 matrices, and 4, 3, and 2 element vectors. My 4x4 matrix has 3 methods for multiplying vectors: overloaded multiply with Vec4 yielding a Vec4, a TransformVertex() method which takes a Vec3 and yields a Vec4, and a TransformVector() method which takes a Vec3 and yields a Vec3. This is repeated in my 3x3 matrix for 3/2 vectors (for 2D vector games), and my 2x2 matrix for 2-vectors and scalers (for um... experimental 1D gameplay? and consistency?) There are also respective TransformVertices() and TransformVectors() methods for transforming swaths of inputs in one go, to amortize some of the setup costs.