2016-02-04 82 views
-1

我正在嘗試創建一個程序,其中角色形狀將不斷繪製到行星形狀的中心。我採納了以前的建議,使用物理方程來產生所需的效果,但我想我錯過了一些,因此它不起作用(我只使用v^2 = u^2 + 2as)。我在複雜的事情?我需要的是讓玩家對象不斷地朝向地球對象的中心;將不需要其他行星物體進入並影響玩家的重力。AS3/Flash開發 - 徑向引力(行星模擬中的角色)

編輯:爲了澄清,我的問題應該是「我哪裏出錯了?」。我現在已經解決了下面發佈的答案,這是因爲我沒有試圖以正確的方式實現我想要的答案。我沒有按照預期計算實際移動的方向,因此角色無法按預期方向移動。

這裏是我的Main.as:

package 
{ 
import flash.display.InteractiveObject; 
import flash.display.Sprite; 
import flash.events.Event; 
import Math 
import flash.display.Bitmap; 
import flash.display.DisplayObject; 
import flash.events.KeyboardEvent; 
import flash.text.TextField; 
import flash.text.TextFormat; 
import flash.utils.*; 

/** 
* ... 
* @author Me 
*/ 
public class Main extends Sprite 
{ 
    public var planet1:Planet; 
    public var character1:Character; 
    public var initialXVelocity:Number = 0; 
    public var initialYVelocity:Number = 0; 
    public var finalXVelocity:Number; 
    public var finalYVelocity:Number; 
    public const gravitationalAcceleration:Number = 3; 
    public var xDistance:Number; 
    public var yDistance:Number; 



    public function Main() 
    { 
     if (stage) init(); 
     else addEventListener(Event.ADDED_TO_STAGE, init); 
    } 

    private function init(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 
     // entry point 
     planet1 = new Planet; 
     character1 = new Character; 

     //place entites in position to test note: planet 1 is 100*100 pixels, character 1 is 10*10 pixels 
     planet1.x = 350; 
     planet1.y = 250; 
     character1.y = 100; //295 
     character1.x = 396; //395 
     addChild(planet1); 
     addChild(character1); 

     //Make sure character has gravity applied constantly 
     setInterval(playerMove, 1000); 

    } 

    public function playerMove():void 
    { 
     //Calculate the distance between the character and the planet in terms of x and y coordinate 
     xDistance = (character1.x + 5) - (planet1.x + 50); 
     yDistance = (character1.y + 5) - (planet1.y + 50); 

     //Make sure this distance is a positive value 
     if (xDistance < 0) { 
      xDistance = -xDistance; 
     } 

     if (yDistance < 0) { 
      yDistance = -yDistance; 
     } 

     //Calculate velocity using physics equation v^2=(u^2)+2as 
     finalXVelocity = Math.sqrt((initialXVelocity * initialXVelocity) + (2 * gravitationalAcceleration) * xDistance); 
     finalYVelocity = Math.sqrt((initialYVelocity * initialYVelocity) + (2 * gravitationalAcceleration) * yDistance); 
     /*trace(initialXVelocity); 
     trace(gravitationalAcceleration); 
     trace(xDistance); 
     trace(finalXVelocity); 
     trace(initialYVelocity); 
     trace(gravitationalAcceleration); 
     trace(yDistance); 
     trace(finalYVelocity);*/ 

     //Make sure the character is moving towards the centre of the planet at all times by reversing the appropriate velocity once it passes the axis of the centre of the planet 
     if (planet1.x < character1.x) { 
      finalXVelocity = -finalXVelocity; 
     } 

     if (planet1.y < character1.y) { 
      finalYVelocity = -finalYVelocity; 
     } 

     //Update the current velocity before new velocity is calculated 
     initialXVelocity = finalXVelocity; 
     initialYVelocity = finalYVelocity; 

     //Send the character into the correct direction 
     character1.x += finalXVelocity; 
     character1.y += finalYVelocity; 
    } 

} 

} 
+1

這是什麼問題?你嘗試了什麼? 「不起作用」不是問題。你調試了你的代碼嗎?在使用調試器時變量具有哪些值?你期望什麼值? – null

回答

1

重力通常被描述成加速度恆定:

const gravity:Number = 3; 

的東西,如一個側面滾輪,您可以只需添加重力到y的速度每個剔:

vy += gravity; 

在你的情況是真的一樣,除了代替重力被純粹添加到矢量y中,它會根據朝向行星中心的方向添加到x和y中,就好像您不斷向行星中心施加推力一樣。因此,所有你需要的是找到玩家和地球中心之間的角度:

var angle:Number = Math.atan2(planet.y - player.y, planet.x - player.x); 

並與角度,你可以找到重力矢量:

var gravityX:Number = Math.cos(angle) * gravity; 
var gravityY:Number = Math.sin(angle) * gravity; 

,只需添加到您的播放器的矢量:

vx += gravityX; 
vy += gravityY; 

這將導致玩家不斷被拉到地球中心。當然,沒有任何行星碰撞,它將無休止地繞行星中心運行。