3

I'm making a simple Android tablet game and am stuck at a seemingly simple math problem.

I have a touchpad that generates the angle your finger is touching from the center. This is based on 0 degrees is at 3-oclock and is working fine.

Touchpoint

With my finger on 45 degrees, I want to fire an object off from a given point at that angle, knowing the item can travel 150 pixels in range. My math son tells me the formula but I believe it figures coordinates based on 0,0 being the bottom left where as 0,0 is top left.

His math is this:

xDirection = range(hypotenuse) * Math.cos(angle);
yDirection = range(hypotenuse) * Math.sin(angle);

This math however returns some very abnormal results. If I'm at a perfect 0 degrees the math is right, it tells me the xDirection is 100 which is my range and yDirection is 0. If I am at 90 degrees it gives me an xDirection of -44 and a yDirection of 89.

My xDirection and yDirection represent how many pixels on the x and y axis my bullet will need to travel to fulfill its range at the angle my touch pad is at.

This is probably simple math for others but I'm lost! Any help would be SUPERB!

4

1 回答 1

1

我确定了我的问题!

最重要的是,我不知道的是,Y 因子必须颠倒。

另外,因为 Math.cos 和 Math.sin 正在寻找弧度,所以它失败了,因为我正在输入从 Math.toDegrees(); 返回的文字度数值;

因此,在将数学更改为以下内容后:

xDirection = range(hypotenuse) * Math.cos(Math.toRadians(angle));
yDirection = -range(hypotenuse) * Math.sin(Math.toRadians(angle));

一切都很好。再次注意 y 坐标是颠倒的,因为一般数学将 0,0 放在左下角。我们的屏幕坐标 0,0 作为左上角。

于 2012-11-20T09:04:55.263 回答