设定投影点

时间:2017-07-07 02:11:40

标签: actionscript-3 math game-physics trigonometry

我正致力于游戏项目。我有一个场景,其中舞台将包含随机位置的圆圈。当我按向上箭头键时,我希望圆圈从中心向三维轴移动。

舞台的宽度为550,舞台的高度为300.

我想要的是,

  • 位于舞台中心(550/2)的圆圈应以90度行进。 In the above image the circle falls within the center so I want it to travel in 90 degree

  • 位于小于舞台中心(550/2)的圆圈应以小于90度的角度行进,与中心不同。 In the image the circles should travel in angle  lesser than 90 degree and the angle should vary based on the difference from the center  在图像中,圆圈应以小于90度的角度行进,角度应根据与中心的差异而增加

  • 位于大于舞台中心(550/2)的圆圈应以大于90度的角度行进,与中心不同。 In the image the circles should travel in angle  greater than 90 degree and the angle should vary based on the difference from the center 在图像中,圆圈应以大于90度的角度行进,角度应根据与中心的差异而增加

我所做的是

var stage_width = 550;
var stage_height = 300;
var stage_center = stage_width/2;
var speed = 5;
var default_angle = 90 * Math.PI/180;

for(i = 0; i< 1; i++)
{
     var circle = _root.attachMovie('circle','circle_mc_'+i,_root.getNextHighestDepth());
    circle._x = Math.floor(Math.random() * 551);
    circle._y = Math.floor(Math.random() * 301);
    circle.obj_x = circle._x;
    circle.onEnterFrame = function()
    {
        Key.addListener(this);
        this.onKeyDown = function()
        {
            if(Key.UP  == Key.getCode())
            {
                if(this._x == stage_center)
                {
                    this._x = this._x + Math.cos(default_angle) * speed;
                    this._y = this._y + Math.sin(default_angle) * speed;
                }   
                else if(this._x > stage_center)
                {
                    var diff = this.obj_x - stage_center;
                    // I need a solution here to find the angle
                    var angle = ((90 - diff)<0)?-(90 - diff): 90 - diff;
                    var rad = (angle) * Math.PI/180;
                    this._x = this._x + Math.cos(rad) * speed;
                    this._y = this._y + Math.sin(rad) * speed;
                }
                else if(this._x < stage_center)
                {
                    var diff = stage_center - this.obj_x;
                    // I need a solution here to find the angle
                    var angle = ((90 + diff)>180)? 180: 90 + diff;
                    var rad = (angle) * Math.PI/180;
                    this._x = this._x + Math.cos(rad) * speed;
                    this._y = this._y + Math.sin(rad) * speed;
                }
            }
        }
    }
}

我不知道如何根据与中心和指定位置的差异找到大于或小于90度的角度。请为我提供找到角度的解决方案

1 个答案:

答案 0 :(得分:0)

最简单的方法是想象一个半径为550/2的圆圈围绕你的中心。如果你这样做,那么你的球的X坐标就成了你需要的角度的余弦,而Y坐标就变成了正弦。

现在,要将矢量旋转某个角度,让我们说A,你只需要将它乘以:

|cos(A) -sin(A)|
|sin(A)  cos(A)|

但是因为cos(A)= X而sin(A)= Y:

|X -Y|
|Y  X|

(如果Y = 0位于屏幕顶部,我希望这可以工作。) https://en.wikipedia.org/wiki/Rotation_matrix