2017-07-07 114 views
0

我正在从事游戏项目。我有一个场景,其中舞台将包含随机位置的圈子。当我按向上箭头键时,我想让圆圈从中心向三维轴移动。投影设定点

舞台的宽度为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度,角度较小的行进应该增加基于从中心

  • 被定位成比阶段的中心更大的圆圈的差(二分之五百五十零)应以大于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度的角度。请提供我的解决方案找到角度

回答

0

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

现在,通过一些角度旋转向量,假设A,你只需要向左乘以:

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

但由于COS(A)= X和sin(A)= Y:

|X -Y| 
|Y X| 

(I期望这如果Y = 0是在屏幕的顶部工作。) https://en.wikipedia.org/wiki/Rotation_matrix