# Dot Product Vs Cross Product Turning a Turret Towards A Enemy and firing

http://pastebin.com/iw9DHf7s is code demonstrating dot product turning a turretto a target and firing. It works and chooses best angle to spin.

A problem can occur when game goes faster (better computer) and that threshold no longer applies. Making the threshold bigger obviously stops the turret early.

My friend mentions cross product is the way to fix it. Then he disappears for long periods of time. Doesn't dot product NOT get a inner angle between two vectors? Ok lets just say I agree and dot product doesn't work ever.

So tell me how does vector product do the trick? I'm going from a single scalar that I can get degrees out of to a third orthogonal vector. I've read about cross product and the function and watched a you tube video and I don't get (given the same code pasted) how to use cross product here.

Update: Would this work?

//towerFacing is the at/forward vector of the cannon
Vector v1 = cannonPos - targetPos ;
Vector v2 = towerFacing - cannonPos ;

//The z plane
double cross = v1.x*v2.y - v1.y*v2.x ;
if (cross > 0) { return Direction.Left ; }
if (cross < 0) { return Direction.Right ; }


Obviously the zero is not what we want to use in games. However The difference is now our zero means our at vector and target is perpendicular and on each other which a x b = 0. The dot product since it uses cosine I used the right vector which is orthogonal with the facing( a . b = 0).

Am I wrong on how the cross product works by what has listed for answers still?

The dot method you have on the pastebin is fine if all objects are on the same plane and it is essentially a 2d problem (regardless of if the game is 2d or 3d). If, however, you are in a 3d environment where you need to adjust cannon yaw & pitch, then the cross method would be better because it would give both the direction to turn(rotate towards target) and the 3d axis to rotate about.

If you crossed the cannon's barrel direction with the direction to the target, it gives the proper axis. If you normalized both before crossing, it would even give the arc-sine of the angle between them if you needed (in the same way a dot product could give the arc-cosine). When crossing, the axis created will give the shortest rotational direction to the target... so does your dot method... but it fails you when the barrel has to change in two dimensions.

• So you mean: Get CannonAtVector instead of right and normalize. Cross VicNorm with CannonAtVector. Then rotate barrel about this axis? Normalize At Cross VicNorm with At this is my 3D axis to.... rotate about? As in rotate about this axis? – user12309 Jan 4 '12 at 13:14
• Not sure what you mean by ConnonAtVector. If that means what direction the cannon is currently pointing at then yes. it should be something like (m_cannonMatrix._31, ._32, ._33). Then, if you are going to rotate at a constant rate again, no need to normalize anything but the result of the cross (the axis). then create a rotationMatrix (D3DXMatrixRotationAxis) that takes in an arbitrary axis(your cross result) & angle(how much to rotate this frame only), combine that to m_CannonMatrix, & your done. – Steve H Jan 4 '12 at 20:21
• What you pulled from the matrix was the forward/at(Z Axis), where as Y is up, and X being right. Also I have not looked at this while. What is the math behind rotation axis with matrices so If I wanted to perform this on my own I could. I'm making the assumption that it is this kwon3d.com/theory/transform/rot.html. – user12309 Jan 4 '12 at 21:44
• Correct, local fwd. I'm assuming that your barrel is pointing along m_CannonMatrix' forward axis. with a Y-up system it may need to be the negate of that axis (-Z). It is like the ones you linked but instead of a matrix that represents a rotation around a world axis, it makes one that is a rotation around an arbitrary axis of your choosing. when you combine that with any other matrix, it's like you simply rotated that other one by whatever axis and angle you plugged in. – Steve H Jan 4 '12 at 23:31
• You don't really need to undestand the math behind if you don't want to... but it's a good idea. As long as you learn how to use it and combine matrices properly, the function will do all the math behind the scenes. msdn.microsoft.com/en-us/library/windows/desktop/… – Steve H Jan 4 '12 at 23:34

Blockquote A problem can occur when game goes faster (better computer) and that threshold no longer applies.

For starters this is a huge mistake to make and you will find your life much more difficult if you rely on the CPU processing speed to determine your game's frame rate. At a minimum I would update your game based on the actual progression of time rather than CPU cycles. You can still run into problems where different machines will have different amounts of time to process each frame, but your physics and other gameplay systems should advance time strictly on how much real world time passes each frame.

• This is correct, but does not have to do with the math question. Also I should note if it concerns, that game actually does operate off of time. The block you quoted: yeah, I should reword that and drop CPU speed(it was actually something else I'm just not recalling at the moment). – user12309 Jan 17 '12 at 23:14