A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Runge Kutta AS2 (I have the Verlet system working but need to integrate Runge Kutta)

  1. #1
    Junior Member
    Join Date
    Feb 2008
    Posts
    8

    Runge Kutta AS2 (I have the Verlet system working but need to integrate Runge Kutta)

    Hi,

    I must firstly apologise as I am not an experienced Flash user. As a result I have been working in AS2 and have still (much to the digust of many I'm sure) not converted to AS3.

    My problem is that I have a rough Verlet system working. However, it is rigid bodied and really need to be able to integrate Runge Kutta into it.
    I have googled thoroughly and can not find any information on Runge Kutta in AS2.

    However, I have an AS3 actionscript file for Runge Kutta 4. I will post it below. Does anyone know how I could convert this to AS2. I really need to do this and am prepared to pay someone to do it using paypal if necessary and if somebody would be willing. This is not laziness, but a strict deadline for college work.

    Thank you.

    Here is the AS3 Runge Kutta 4 code:

    package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.utils.getTimer;

    public class RK4 extends Sprite
    {
    private var _ball:Sprite;
    private var _position:Point;
    private var _velocity:Point;
    private var _gravity:Number = 32;
    private var _bounce:Number = -0.6;
    private var _oldTime:int;
    private var _pixelsPerFoot:Number = 10;


    public function RK4()
    {
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;

    _ball = new Sprite();
    _ball.graphics.beginFill(0xff0000);
    _ball.graphics.drawCircle(0, 0, 20);
    _ball.graphics.endFill();
    _ball.x = 50;
    _ball.y = 50;
    addChild(_ball);

    _velocity = new Point(10, 0);
    _position = new Point(_ball.x / _pixelsPerFoot, _ball.y / _pixelsPerFoot);

    _oldTime = getTimer();
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onEnterFrame(event:Event):void
    {
    var time:int = getTimer();
    var elapsed:Number = (time - _oldTime) / 1000;
    _oldTime = time;

    var accel1:Point = acceleration(_position, _velocity);

    var position2:Point = new Point();
    position2.x = _position.x + _velocity.x / 2 * elapsed;
    position2.y = _position.y + _velocity.y / 2 * elapsed;

    var velocity2:Point = new Point();
    velocity2.x = _velocity.x + accel1.x / 2 * elapsed;
    velocity2.y = _velocity.y + accel1.x / 2 * elapsed;

    var accel2:Point = acceleration(position2, velocity2);

    var position3:Point = new Point();
    position3.x = _position.x + velocity2.x / 2 * elapsed;
    position3.y = _position.y + velocity2.y / 2 * elapsed;

    var velocity3:Point = new Point();
    velocity3.x = _velocity.x + accel2.x / 2 * elapsed;
    velocity3.y = _velocity.y + accel2.y / 2 * elapsed;

    var accel3:Point = acceleration(position3, velocity3);

    var position4:Point = new Point();
    position4.x = _position.x + velocity3.x * elapsed;
    position4.y = _position.y + velocity3.y * elapsed;

    var velocity4:Point = new Point();
    velocity4.x = _velocity.x + accel3.x * elapsed;
    velocity4.y = _velocity.y + accel3.y * elapsed;

    var accel4:Point = acceleration(position4, velocity4);

    _position.x += (_velocity.x + 2 * velocity2.x + 2 * velocity3.x + velocity4.x) / 6 * elapsed;
    _position.y += (_velocity.y + 2 * velocity2.y + 2 * velocity3.y + velocity4.y) / 6 * elapsed;

    _velocity.x += (accel1.x + 2 * accel2.x + 2 * accel3.x + accel4.x) / 6 * elapsed;
    _velocity.y += (accel1.y + 2 * accel2.y + 2 * accel3.y + accel4.y) / 6 * elapsed;

    if(_position.y > (stage.stageHeight - 20) / _pixelsPerFoot)
    {
    _position.y = (stage.stageHeight - 20) / _pixelsPerFoot;
    _velocity.y *= _bounce;
    }
    if(_position.x > (stage.stageWidth - 20) / _pixelsPerFoot)
    {
    _position.x = (stage.stageWidth - 20) / _pixelsPerFoot;
    _velocity.x *= _bounce
    }
    else if(_position.x < 20 / _pixelsPerFoot)
    {
    _position.x = 20 / _pixelsPerFoot;
    _velocity.x *= _bounce;
    }

    _ball.x = _position.x * _pixelsPerFoot;
    _ball.y = _position.y * _pixelsPerFoot;
    }

    private function acceleration(p:Point, v:Point):Point
    {
    return new Point(0, _gravity);
    }
    }
    }

  2. #2
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    there's nothing that too solidly binds this to as3. the actual integration method would be almost identical in as2. i'm happy to help you understand the integrator computationally, but as far as porting something to as2, no thanks.

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    check out foam. it has general RK4 implementation not tied to any _balls or stage references. that might be easier to port.
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    oh look, I name foam, and woah, we have newblack in da house I know it seems other way around, but I was typing when he posted.
    Last edited by realMakc; 04-01-2009 at 05:40 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  5. #5
    Junior Member
    Join Date
    Feb 2008
    Posts
    8
    Hi newBlack and realMakc.

    Firstly, thank you for the link to Foam. I might well be confused as I thought that Runge Kutta produces soft-body behaviours and that the Verlet system that I already have working has Rigid body gravity behaviour. But let me know if I'm wrong there

    NewBlack - Any advice you can give me would be very much appreciated about how it works computationally. Also if I am on the wrong tracks here in terms of what I said above about soft-body collision then it would be great to know also.

    Thank you both.

  6. #6
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    they are both just algorithms to solve equations numerically. all these soft/rigid/body/gravity stuff are just use cases.

    p.s. check out this post (it's newblack blog)
    Last edited by realMakc; 04-01-2009 at 06:08 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  7. #7
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    if you're doing soft body stuff verlet is probably the way to go- especially if you already have it running. because in a typical verlet scheme the velocity is implied by the difference in position, the system usually loses energy quickly which makes it less prone to explode.

    but really they're just 2 different ways of solving differential equations. unless you need really stiff springs or extremely accurate results rk4 is probably overboard.

    you can also always try a simple euler integrator:
    v(t + dt) = v(t) + ( f(t) / m ) * dt
    x(t + dt) = x(t) + v(t) * dt

    anyway, the whole idea behind a good integrator is usually based on sampling. rk4 takes the state of the system at time t and advances the simulation to time t + dt. it does this by evaluating derivatives at various points over the interval dt, updating an imaginary state which is used to find a new derivative, and repeating until there's a nicely mixed interpretation of the derivative over dt. if you understand how and why rk4 does this, it makes an implementation really simple... just look at the example you posted or the link to my implementation in foam that realMakc posted.

    so what you want, computationally, is a way to evaluate the derivative, given the state. so that you can pass the state of the entire system and receive a new derivative given that state. since you're already starting with a working simulation, just a different integrator, this is probably pretty easy to accomplish. from there you should be able to copy/paste your way through it...

  8. #8
    Junior Member
    Join Date
    Feb 2008
    Posts
    8
    Hi newblack,

    Thankyou very much for the explanation. I will check out your blog too as this appears to have a lot of helpful info on the subject. Also like you said maybe RK4 is overkill.

    At the risk of sounding completely ignorant though, is RK4 not the equation for soft body movment as opposed to rigid body physics in the verlet system? If not, then how could soft body dynamics be achieved? No worries if this is too big a question. But do you know any links where I could start to look at this?

    Thank you very much again. I appreciate that this must have taken up a lot of your time!!

  9. #9
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    Quote Originally Posted by kt1984 View Post
    At the risk of sounding completely ignorant though, is RK4 not the equation for soft body movment as opposed to rigid body physics in the verlet system?
    What someone chooses to use one integrator over the other for is very application specific, but no, RK4 is not specifically "the equation for soft body movement." Maybe if you share your simulation and what you actually want to do I'll be able to help a little better...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center