samedi 26 septembre 2015

Error in physics as3

Help me find the error in physics as3. After the addition of gravity errors. hero enters the platform How to create the correct physics, physics?

mc - Sprite (Circle) kv - Sprite (Platform irregular shape)

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);
}

Aucun commentaire:

Enregistrer un commentaire