AS3 / Flash Develop - 径向重力(行星模拟中的特征)

时间:2016-02-04 11:57:23

标签: class actionscript-3 game-physics flashdevelop gravity

我正在尝试创建一个程序,其中字符形状将不断绘制到行星形状的中心。我已经采用了先前的建议来使用物理方程来产生所需的效果,但我认为我错过了一些,因此它不起作用(我只使用v ^ 2 = u ^ 2 + 2as)。我过度复杂了吗?我只需要将玩家对象不断地拉向行星物体的中心;不需要其他行星物体发挥作用并影响玩家的引力。

编辑:为了澄清,我的问题应该是“我哪里出错?”。我现在已经解决了下面发布的答案的问题,因为我不打算尝试实现我想要的正确方法。我没有按预期计算实际移动方向,因此角色无法按预期方向移动。

这是我的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 个答案:

答案 0 :(得分: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;

这将使玩家不断被吸引到地球的中心。当然,如果没有任何行星碰撞,它将无休止地绕行星中心运行。