Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do this coordinate system operation more efficiently?
    primarykey
    data
    text
    <p>I'm making a 3D game, where the player's back should always be facing the camera and he should move in that direction. I didn't come to the "back facing the camera" part yet, but I believe that it will be simple once I figure out how to move the player in the right direction...</p> <p>Though it is a 3D coordinate system, height can be ignored (z-axis) because no matter how high the camera is, the player should always be going in the same speed (the camera system is planned to function much like in the game World of Warcraft).</p> <p>Now, I have summarized my problem to this...</p> <ul> <li>Point (0, 0) is the players position. </li> <li>Point (x, y) is the camera's position.</li> <li>The camera is (dx, dy) units away from the player (and because player is at (0, 0), it is also (x, y) units away, although this is a position vector, not a translation one)</li> </ul> <p>Problem: how do I get a point (a, b) in this 2D space that lies on a circle r = 1 but is on the same line as (0, 0) and (x, y)?</p> <p>Visualization:</p> <p><img src="https://i.stack.imgur.com/Ptqsj.png" alt="enter image description here"></p> <p>By doing this, I should have a 2D vector (a, b), which would, when multiplied by -30, act as the speed for the player.</p> <p>I know how to do this, but in a very expensive and inefficient way, using the Pythagora's theorem, square roots, and all those out-of-the-question tools (working in Javascript).</p> <p>Basically, something like this:</p> <pre><code>c = sqrt(dx*dx + dy*dy); //Get the length of the line rat = 1/c; //How many times is the desired length (1) bigger than the actual length a = x*rat; b = y*rat; </code></pre> <p>There must be something better!</p> <p>For reference, I'm making the game in Javascript, using the Three.js engine.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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