物理学中的错误as3

时间:2015-09-26 15:44:56

标签: actionscript-3 flash physics

帮我找到物理中的错误as3。 增加重力误差后。 英雄进入平台 如何创建正确的物理,物理?

mc - 精灵(圆圈) kv - Sprite(平台不规则形状)

import flash.events.Event;

var gradus: Number ;
var speed: Number = 1;
var direction: Point;
const gravity: Point = new Point(0, 0.001);
var i: int;

direction = getStartDirection(speed, 90);
stage.addEventListener(Event.ENTER_FRAME, ef);

function addVector(v1: Point, v2: Point): Point
{
    return new Point(v1.x + v2.x, v1.y + v2.y);
}

function getStartDirection(length: Number, angle: Number): Point
{
    var aRad: Number = (angle * Math.PI) / 180;
    var xpos: Number = length * Math.cos(aRad);
    var ypos: Number = length * Math.sin(aRad);
    return new Point(xpos, ypos);
}
function ef(e: Event): void
{
    for (i = 0; i < 10; i++)
    {
        mc.x += direction.x;
        mc.y += direction.y;
        var test: Number = hitTestAngle(kv, mc.x, mc.y, 10);
        if (test)
        {
            var a1: Number = (Math.atan2(direction.y, direction.x)) * (180 / Math.PI);
            gradus = test - (360 - 90 - test - (90 - a1));

            if (speed > 0.01)
            {
                speed /= 1.05;
            }
            else
            {
                speed = 0;
            }
            direction = getStartDirection(speed, gradus);
        }
        else
        {
            direction = addVector(direction, gravity);
        }
    }
}

function hitTestAngle(shape: Sprite, xPos: Number, yPos: Number, radius: Number): Number
{
    var samples: uint = Math.abs(radius * Math.PI);
    const PI2: Number = Math.PI * 2,
    SAMPLE: Number = 1 / samples;
    var dx: Number, dy: Number, a: Number, tx: Number = 0,
        ty: Number = 0,
        hits: int = 0;
    var i: int = samples;
    while (i--)
    {
        a = PI2 * (i * SAMPLE);
        dx = radius * Math.cos(a);
        dy = radius * Math.sin(a);
        if (shape.hitTestPoint(xPos + dx, yPos + dy, true))
        {
            hits++;
            tx += dx;
            ty += dy;
        }
    }
    if (!hits)
        return NaN;
    return Math.atan2(ty, tx) * (180 / Math.PI);
}

0 个答案:

没有答案