Cross / Dot Product question

Blitz3D Forums/Blitz3D Beginners Area/Cross / Dot Product question

Stevie G(Posted 2005) [#1]
I have 2 vectors

vx1 = 10
vy1 = 0
vz1 = 0

vx2 = 20
vy2 = 0
vz2 = -10

Like so ....


(1)

------v1
     / \
    /   \
   /     \
  v3      v2

(2) Needs to work for this also

-----v1--------v2
      !
      !
      v3




What I need is to find the normal vector between the two points v3. How do I do this again ?


sswift(Posted 2005) [#2]
You need the cross product for that. The cross product takes two vectors, and makes a new one at a 90 degree angle to both.

It might make it easier to remember if you imagine that it CROSSES the plane that the two vectors form.

The dot product takes two vectors and returns a single number that tells you how much they point towards eachother.

The dot product is used with normals for lighting vertices, which are dots. :-)

Cross product:
x3 = y1*z2 - z1*y2
y3 = z1*x2 - x1*z2
z3 = x1*y2 - y1*x2


Stevie G(Posted 2005) [#3]
Thanks Sswift. I only need the angle on the xz plane really so this won't hold. In the pictures above the y component would not be 0 if it was at right angles to both vectors.

In example (1) the new vector can't be at 90 degrees to both vectors . I should have explained better I guess - can this be done?


sswift(Posted 2005) [#4]
I mistook your drawings for being 3D representations because you said you wanted to use the cross product.

But if your example is 2D, and V3 represents a 2D normal realtive to those two lines, then your sketches are incomplete, because you're missing a point.

(1)

v4----v1
     / \
    /   \
   /     \
  v3      v2

(2) Needs to work for this also

v4----v1--------v2
      !
      !
      v3


Now this, you can calculate.

If you have three points, V4, V1, and V2, and they form two lines, V4->V1, and V1->V2, and you want to calculate the normal V1->V3, then what you need is not a cross product.

A cross product takes two lines in a plane, and tells you the normal of the plane.

But you have two lines, and you want to determine a line which is at an angle that is half the angle between them.

I'll get back to you on this... I think I know how to do it without using any sin or cosine or arctan.


sswift(Posted 2005) [#5]
Oh, I know what you have to do. This is the same problem as generating vertex normals over a surface.

What you need to do is generate the normal for lines V4V1 and V1V2. I put code in the code archives to calculate the normal of a line a while back...

Ah here we go:
http://www.blitzbasic.com/codearcs/codearcs.php?code=450

Now that you have the normal of each of the lines... And do note that the order you put the points into the equation determines if the normals will point towards the inside of your angle (correct) or the outside of your angle in the diagrams above...

You can now simply add the normals of the two lines, Nx1+Nx2, Ny1+Ny2, and normalize the result:

; Normalize the new vertex normal.
			; (Normalizing is scaling the vertex normal down so that it's length = 1)

				Nl# = Sqr(Nx#^2 + Ny#^2 + Nz#^2)

				; Avoid a divide by zero error if by some freak accident, the vectors add up to 0.
				; If Nl# = 0 Then Nl# = 0.1

				Nx# = Nx# / Nl#
				Ny# = Ny# / Nl#
				Nz# = Nz# / Nl#


Just delete all references to Nz in the above, as it is 0.

And voila, you are finished.

In example 2 above, where V4 V1 and V2 lie on a line, the normals of both lines would point downard. You would then add them together, and renormalize. That would result in a normal pointing downward as well, line V1V3.

So that's it. I think that's what you wanted to do, right?


Stevie G(Posted 2005) [#6]
Perfect - cheers sswift!