-4

So here's a link for the same question but the best answer doesn't explain it fully:

Rotate Sprite to Mouse Position

It's the cross product that I'm stuck with, since the formula in that link can only be applied in mathematics outside of computing.

What is the actual formula to calculate the cross product in computing form? If you can post it as C++ code that would be great.

Keep in mind I'm looking for the cross product between 2 2D vectors, not 3D.

Community
  • 1
  • 1
StallMar
  • 31
  • 1
  • 2
  • 7
  • http://rosettacode.org/wiki/Vector_products#C.2B.2B for the threedimensional case. – Captain Giraffe Nov 20 '14 at 22:08
  • `It's the cross product that I'm stuck with, since the formula in that link can only be applied in mathematics outside of computing.` Not true. @Captain They said sprite so I'm assuming it's 2D, and the link they provided should be enough for their purposes. –  Nov 20 '14 at 22:10
  • That formula as it is cannot be applied from what I see. The first instance of sin theta will be 0 because we don't even know what theta is in the first place. – StallMar Nov 20 '14 at 22:12

1 Answers1

3

The title says you are interested in computing the angle between two 2D vectors, so thats what I'm going with.

If you look at for instance http://mathworld.wolfram.com/DotProduct.html, It is fairly straightforward to implement in code.

There is however the atan2 function which makes this a cinch:

double angle = atan2(p2y, p2x) - atan2(p1y, p1x);
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • Thanks, I wasn't aware dot product could be used to find the angle. Just to be sure, do any of the two vectors need to be unit vectors when using dot product? – StallMar Nov 20 '14 at 22:39
  • @StallMar No, it is assumed to be a normal XY coordinate system. – Captain Giraffe Nov 20 '14 at 22:40
  • Oh, that makes me a little confused. Does this mean I just plug in the x, y position of the mouse in the first atan2() and x, y position of the player in the 2nd atan2()? Or do I still need 2 vectors? Because previously I had a vector going from the player to the mouse position, and a unit vector of the player. Without the unit vector I don't have a clue where I'm supposed to make this other vector pop out from. Would it just be a velocity vector of 0,0 or something? – StallMar Nov 20 '14 at 22:46
  • @StallThe unit vectors are x and y. On a computer screen they are the pixels. In maths they might be odd creatures. Yes just plug them in. If you get odd results from your code, please post a new question. Hope that helps. – Captain Giraffe Nov 20 '14 at 23:53