我的Javascript游戏的坐标 - 基于角度,我什么时候使用Sin Cos和Tan? [英] Coordinates for my Javascript game - based on an angle, when do I use Sin Cos and Tan?

查看:248
本文介绍了我的Javascript游戏的坐标 - 基于角度,我什么时候使用Sin Cos和Tan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript协调Sin,Cos或Tan?
我试图在web浏览器中学习一些基本的三角学游戏开发。我知道soh cah toa规则等。我知道我们也在-1和1之间工作。



我很困惑,但我需要计算出x和y坐标



这里是我必须在x和y中确定我的角度的方向,这是工作(感谢Loktar)。

p>

  velY = -Math.cos(angle * Math.PI / 180) 
velX = Math.sin(angle * Math.PI / 180)* thrust;

我的理解是,我发现x和y的cos基于一个小公式将我的角度变量转换为弧度。



但是,为什么cos需要用于x和sin为y? tan在哪里进来?这是与一个圆的四个象限有关吗?



我怎么知道什么时候使用sin cos或tan,当我只给一个角度,我需要工作





任何简单的图表或解释都会非常有帮助!

感谢

解决方案

基本定义(来自您的三角书):

  cosΘ= x / h,sinΘ= y / h 


$ b b

用于描述x,y和H的无关ASCII艺术:

  _ 
/ \假设我们'这样去
/
/ |
/ |
h / |
/ | Y
/ |
/Θ|
+ -------
X

分成两个向量的和。 (在图中,你可以认为这是H东北向X米,北向Y米)



H在这种情况下对应到你当前的推力。你想找到推力的X和Y分量。



由于cosΘ= X / H(余弦的基本定义),我们可以说(通过简单代数)

  X = H * cosΘ

假定为弧度测量。如果您使用度数,则必须乘以Math.PI / 180以获取正确的值。因此,您有

 Θ= angle * Math.PI / 180 
pre>

我们非常接近!现在我们经典的余弦公式定义如下:

  cos(angle * Math.PI / 180)= X / H 

H是我们的推力向量,X只是我们推力的水平部分



现在,等一下,你说。 为什么我使用余弦来计算垂直速度?你的轴似乎翻转了我。是啊。这是标准几何定向 - 角度被测量为从 --------> 直接向右旋转的逆时针旋转。您的轴被翻转,因为您将角度存储为时钟角,即0度处于12:00。为了使用标准几何方程,我们必须镜像我们的整个宇宙,使得X和Y轴被翻转。幸运的是,这样做的数学方法只是在方程中切换所有的X和Y。

 坐标系
_
/ \
/
/ |
/ |角度逆时针增加
h / |
/ | Y
/ |
/Θ|
+ -----------(零度从这里开始)
(0,0)X


您的坐标系

(零度从这里开始)
^
|角度顺时针增加
| /
| /
|Θ/
| /
+

为什么在余弦有一个负?因为这里是笛卡尔系统,我们在这里做我们的数学

  ^ y轴
|
|
+ ----> x轴

这里是你要绘制的笛卡尔坐标系>

  + ------> x轴
|
|
v y轴

因为y轴朝向另一个方向,所以y值取负数1以获得正确的方向


JavaScript coordinates Sin, Cos or Tan? I am trying to learn some basic trigonometry for game development in the web browser. I know the soh cah toa rule etc. I know we are also working between -1 and 1.

I am confused though, I need to work out x and y coordinates separately depending on an angle.

Here is what I have to work out the direction of my angle in x and y, which does work (Thanks to Loktar).

velY = -Math.cos(angle * Math.PI / 180) * thrust;
velX = Math.sin(angle * Math.PI / 180) * thrust;

What I understand from this is I am finding the cos of x and y based on a small formula to convert my angle variable to radians.

But, why does cos need to be used for x and sin for y? Where does tan come into this? Is this to do with the 4 quadrants of a circle?

How do I know when to use sin cos or tan when I am only given an angle and I need to work out where on a circle that angle places using x,y?

Any simple diagrams or explanations would be extremely helpful!

Thanks

解决方案

Basic Definitions (from your trigonometry book):

cos ϴ = x/h, sin ϴ = y/h

Gratuitous ASCII art to describe x,y,and H :

         _
         /\  assume we're going this way 
        /
       /|
      / |
  h  /  |
    /   |  Y
   /    |
  /ϴ    |
 +-------
    X

A vector can be split into a sum of two vectors. (you can think of this in the diagram as going northeast for H meters is the same as going east for X meters and north for Y meters)

H in this case corresponds to your current thrust. You want to find the X and Y components of that thrust.

since cos ϴ = X / H (basic definition of cosine), we can say (via simple algebra)

X = H * cos ϴ

however, ϴ is assumed to be a radian measure. if you're using degrees, you have to multiply by Math.PI / 180 to get the correct value. hence, you have

ϴ = angle * Math.PI / 180

We're very close! Now our classic definition-of-cosine formula (when translated to our terms) looks like

cos (angle * Math.PI / 180) = X / H

H being our Thrust vector, X being only the horizontal part of our thrust vector.

"Now, wait a minute," you say. "why am I using cosine to calculate the vertical velocity then? Your axes seem flipped to me." Ah, yes. This is standard geometric orientation -- angles are measured as a counter-clockwise rotation from --------> directly to the right. Your axes are flipped because you are storing your angle as a "clock-angle", i.e. 0 degrees is at 12:00. In order to use the standard geometric equation, we have to mirror our entire universe so that the X and Y axes are flipped. Luckily, the mathematical way to do this is simply to switch all your X's and Y's around in the equations.

standard 'math' coordinate system
            _
             /\  
            /
           /|
          / |       angles increase counterclockwise
      h  /  |
        /   |  Y
       /    |
      /ϴ    |
     +----------- (zero degrees starts here)
  (0,0)  X       


your coordinate system

  (zero degrees starts here)
       ^
       |   angles increase clockwise
       |   /
       |  /
       |ϴ/
       |/
       +

Finally, why is there a negative in the cosine? Because here is the cartesian system where we do our math in

 ^ y-axis
 |
 |
 +----> x-axis

here is the cartesian system that you are drawing to (probably a canvas)

+------> x-axis
|
|
v y-axis

since the y-axis is facing the other direction, you multiply all y-values by a negative 1 to get the correct orientation

这篇关于我的Javascript游戏的坐标 - 基于角度,我什么时候使用Sin Cos和Tan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆