A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: steering behaviours

Hybrid View

  1. #1
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002

    steering behaviours

    Ok so I'm going nuts over this, The source I'm reading from is here:


    If you can be bothered reading that, the main concern is with the arrival behaviour. I have setup my object exactly the same as the page says

    Simple Vehicle Model:
    mass scalar
    position vector
    velocity vector
    max_force scalar
    max_speed scalar
    orientation N basis vectors

    private var p:Vector; //position
    private var v:Vector; //velocity
    private var f:Vector; //force
    private var mass:Number;
    private var maxForce:Number;
    private var maxSpeed:Number;
    *I have left out orientation and added in force which is used as steering

    now when converting this arrival code into flash:

    target_offset = target - position
    distance = length (target_offset)
    ramped_speed = max_speed * (distance / slowing_distance)
    clipped_speed = minimum (ramped_speed, max_speed)
    desired_velocity = (clipped_speed / distance) * target_offset
    steering = desired_velocity - velocity

    var d:Vector = target.subNew(p); //target offset
    var dist:Number = d.length;
    var speed:Number = maxSpeed * (dist / 100);
    speed = Math.min(speed, maxSpeed);
    var dvx = (speed / dist) * d.x;
    var dvy = (speed / dist) * d.y;
    f.x = dvx - v.x;
    f.y = dvy - v.y;
    and then I update the object exactly like the paper describes:

    steering_force = truncate (steering_direction, max_force)
    acceleration = steering_force / mass
    velocity = truncate (velocity + acceleration, max_speed)
    position = position + velocity

    clamp(f, maxForce);
    var ax = f.x / mass;
    var ay = f.y / mass;
    v.x += ax;
    v.y += ay;
    clamp(v, maxSpeed);
    p.x += v.x;
    p.y += v.y;
    the clamp functions looks like:
    function clamp(n, max){
    var len = n.length;
    if(len > max){
    n.x /= len
    n.y /= len
    n.x *= max
    n.y *= max
    But the object continues past the target, as if it were elastic

    can somebody point out what I have done wrong here, I have found several papers on this all which are exactly the same, use the same formulas, but I can't seem to re-create it in flash.
    Last edited by mr_malee; 10-08-2007 at 01:56 AM.
    lather yourself up with soap - soap arcade

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    hmm, was playing around and it seems mass is the culprit for the overshooting, I guess that makes sense since the arrive function knows nothing about the mass of the object; and assumes the force given is the force applied
    lather yourself up with soap - soap arcade

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