0
\$\begingroup\$

I am creating a game using Javascript. I am not using the HTML5 Canvas Element. The game requires both side view controlles, and Isometric controls, hence the movementMode variable.

I have got the specific angles, but I am stuck on an aspect of this.

https://chillibyte.makes.org/thimble/movement

function draw() {
            if (keyPressed) {
                if (whichKey == keys.left) {
                    move(-1,0)
                }
                if (whichKey == keys.right) {
                    move(1,0)
                }
                if (whichKey == keys.up) {

                    move(0,-1)
                }
                if (whichKey == keys.down) {

                    move(0,1)
                }
            }
        }

This gives normal up, down , left, and right. i want to refactor this so that i can plugin two variables into the move() function, which will give the movement wanted.

Now for the trig.

         /|
        / |
       /  | y
      /   |
     /a___|
        x

Take This Right angled Triangle.

given that x is 1, y must be equal to tan(a)

That Seems right. However, when I do Math.tan(45), i get a number similar to 1.601. Why?

To Sum up this question.

I have a function, and i need a function which will converts an angle to a value, which will tell me the number of pixels that i need to go up by, if i only go across 1. Is it Math.tan that i want? or is it something else?

\$\endgroup\$
3
\$\begingroup\$

If I'm not mistaken Math.tan(x) does not take angles as degrees, but rather radians. If you want to supply angles as degrees, try the following code sample:

function getTanDeg(deg) {
   var rad = deg * Math.PI/180;
   return Math.tan(rad);
}

Source - Mozilla Developer Network

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.