Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using Euler angles is a very inefficient way to do this. It is much easier to use vectors.</p> <p>Let P be the position of the player and M the position of the missile. You need to calculate two vectors from these: MP and Mr where MP = vector from missile to player and Mr is the vector pointing to the missile's right hand side. Now calculate the dot product between the vectors MP and Mr. The sign of the result tells you which way to turn the missile. If it's positive then turn the missile clockwise, if it's negative then turn the missile anticlockwise.</p> <p>Why does this work?</p> <p>The dot product of two vectors A and B can be written as:</p> <pre><code>Ax.Bx + Ay.By </code></pre> <p>and also:</p> <pre><code>|A|.|B|.cos (theta) </code></pre> <p>where theta is the angle between the two vectors and |x| is the magnitude of vector x. This gives us:</p> <pre><code>cos (theta) = (Ax.Bx + Ay.By) / (|A|.|B|) </code></pre> <p>Since we only want to know the sign of the value cos (theta), positive values being angles between +- 90 degrees and negative values being angles between 90 and 270 degrees, we only need to work out the sign of the numerator, the denominator is always positive! Thus the sign of the dot product will tell us which side the target is on.</p> <p>But, you're thinking, aren't vectors harder to use than angles? Won't I need to do some trig functions to calculate the missile's right vector? </p> <p>Ideally, you shouldn't be using Euler angles at all for anything. You should be using matrices to store the position and rotation of objects in your world. The missile's right vector is just the top row of the missile's position/orientation matrix (or first column, depends how you set up your system). And matrices are what the rendering hardware uses to draw objects so you're not adding any overhead by using them (in fact, using angles adds overhead as the angle notation needs to be converted to matrices for the rendering).</p> <p>EDIT: You code should end up looking like this:</p> <pre><code>if (VectorDotProduct (missile.right, player.pos - missil.pos) &gt; 0) { missile.rotate clockwise } else { missile.rotate anti-clockwise } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload