2017-07-07 116 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