# Cross Product

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

## Recommended Posts

can anyone tell me how to calculate the cross product in this tutorial? http://www.gamedev.net/reference/articles/article1997.asp // get the axis to rotate around the x-axis. Vector Axis = CrossProduct(View - Position, Up); what is up? what does this function look like? thanks

##### Share on other sites
Up is the unit vector that points upwards in your world. Often this means in positive y direction, so Up would be [0,1,0], but you may use another convention.

And the cross-product itself ... try searching the internet. E.g. euclideanspace.com or one of the other thousands of hits explain this very well.

##### Share on other sites
i'm not very good in math, so this isn't very easy for me, and i just don'e understand how the up vector goes into the equation....

##### Share on other sites
The cross product of 2 vectors results into a single vector that is perpendicular to the plane that is formed by the first 2 vectors. So in your case, your first vector points into the direction you're viewing and the second one points up. Taking the cross product of these vectors result into a vector that is perpendicular to your 2 vectors, which in your case would be to the 'right'.

##### Share on other sites
The cross-product is written mathematically as
A x B =: C
where A and B are two 3-dimensional vectors. As the link given above shows you, the prescription of computing the resulting vector C is
Cx = Ay * Bz - Az * By
Cy = Az * Bx - Ax * Bz
Cz = Ax * By - Ay * Bx
where x, y, and z denote the 1st, 2nd, and 3rd components of the vectors, resp. (Please compare this with the linked site for the case I've made a mistake.)

Now, if you carefully investigate the above formulas (BTW, those formulas are the way you should implement the cross-product), then you can see that the the order of A and B matters. This feature is called "the cross-product is not commutative", and means mathematically, that in general
A x B != B x A

This is important when you interpret the code line
Vector Axis = CrossProduct(View - Position, Up);

Now, that line first compute the difference vector pointing from the Position to the View. Lets name this difference vector A. Then it uses the Up vector which, as already stated, is probably the global y axis vector (i.e. [0,1,0]). Then the result vector Axis is just C in the formulas above. (It _may_ be that the roles of A and B are vice-versa, but that is not likely.)

Now, one very handy thing is the so-called "right hand rule". Please search the internet for it, because there are some nice pictures out there, explaining it much better than I could here verbosely. In short terms, it says that "if you spread out the thumb, forefinger, and middle finger of the right hand so that they build a cartesian co-ordinate system, then the thumb denotes A, the forefinger denotes B, and the middle finger denotes C". That is cool becouse it allows a fast way of checking in which order A and B must be "crossed" to yield in a specific C. Try it out, it is worth it.

##### Share on other sites
Hmm, i made it work, in a way, but the camera doesn't work like it should.
downwards it works fine, but if i look up it flickers (jumps around), any ideas what is going on?

maybe something is inverted as if i use that 1 radian cap the camera moves only a few units up and down....

thanks!

[Edited by - menorian on April 17, 2009 9:25:27 AM]

##### Share on other sites
Quote:
 Hmm, i made it work, in a way, but the camera doesn't work like it should.downwards it works fine, but if i look up it flickers (jumps around), any ideas what is going on?
It would be hard to say what the problem is without seeing the code.

I will say though that that tutorial has a number of problems with it, the main ones being that:

1. The use of quaternions has no benefits to speak of in the given context, and just makes things unnecessarily complicated.

2. Relative rotations are used, which is in general the wrong choice for this type of motion.

3. The view/look-at point/vector is handled incorrectly.

Anyway, if you're still having problems, I recommend posting your code so that we can take a look at it.

##### Share on other sites
i made it work.

can anyone tell me how i can move along the view vector?
since when i press W let's say i want to move upwards if i look up (aim up) like a flight simulator. I want to fly in the direction i'm aiming.

Do i need to do a vector subtract on View and Position or something?

##### Share on other sites
Position += ViewVector

The view vector is the direction you are facing, so just add it to your current position.

Since you seem to know very little I'll draw it out:

if your character is at (10,0,0) on the x-axis and he is looking on the z-axis (0,0,1) and you want him to move, just do

(10, 0, 0) + (0, 0, 1) = (10, 0, 1)

##### Share on other sites
Quote:
 Original post by dpadam450Position += ViewVectorThe view vector is the direction you are facing, so just add it to your current position.Since you seem to know very little I'll draw it out:if your character is at (10,0,0) on the x-axis and he is looking on the z-axis (0,0,1) and you want him to move, just do(10, 0, 0) + (0, 0, 1) = (10, 0, 1)

You can also change the speed of your movement in the given direction by using:

Position += (ViewVector * delta);

You can then adjust the delta value to give you different movement speeds. Note that updating by a constant value means that your overall speed will be determined by the frame rate and will vary from machine to machine (and will slow down when you add more advanced rendering). Updating according to time elapsed is off topic but I thought I'd raise the point in case you were unaware, it may come in handy in the future :)

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633368
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×