Labs: Line-Of-Sight

Did a quick little experiment with Line-Of-Sight Calculations in AS3.0. I’m not too happy with it at the moment, and trying to find a balance between performance and accuracy, but you can see what I have done so far in the Labs.

Experiment Focus:

  • flash.geom.Point;
  • Line of Sight

View Experiment

Share this post: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Facebook
  • Google Bookmarks

2 thoughts on “Labs: Line-Of-Sight

  1. Hmm…a slightly less laggy way to test for collision might be to use bitmapdata:
    /**
    * Pixel Perfect Collision Detection
    * ———————
    * VERSION: 1.0.1
    * DATE: 8/23/2011
    * AS3
    * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
    **/
    package utils
    {
    import flash.display.BitmapData;
    import flash.display.DisplayObjectContainer;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class CollisionTest
    {
    // vars
    private var _returnValue:Boolean;

    private var _onePoint:Point;
    private var _twoPoint:Point;

    private var _oneRectangle:Rectangle;
    private var _twoRectangle:Rectangle;

    private var _oneClipBmpData:BitmapData;
    private var _twoClipBmpData:BitmapData;

    private var _oneOffset:Matrix;
    private var _twoOffset:Matrix;

    /**
    * Simple collision test. Use this for objects that are not rotated.
    * @param clip1 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
    * @param clip2 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
    * @return Collision True/False
    */
    public function simple(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
    {
    _returnValue = false;

    _oneRectangle = clip1.getBounds(clip1);
    _oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);
    _oneClipBmpData.draw(clip1);

    _twoRectangle = clip2.getBounds(clip2);
    _twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
    _twoClipBmpData.draw(clip2);

    _onePoint = new Point(clip1.x, clip1.y)
    _twoPoint = new Point(clip2.x, clip2.y)

    if (_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
    {
    _returnValue = true;
    }

    return _returnValue;
    }

    /**
    * Complex collision test. Use this for objects that are rotated, scaled, skewed, etc
    * @param clip1 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
    * @param clip2 Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
    * @return Collision True/False
    */
    public function complex(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
    {
    _returnValue = false;

    _twoRectangle = clip1.getBounds(clip1);
    _oneOffset = clip1.transform.matrix;
    _oneOffset.tx = clip1.x – clip2.x;
    _oneOffset.ty = clip1.y – clip2.y;

    _twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
    _twoClipBmpData.draw(clip1, _oneOffset);

    _oneRectangle = clip2.getBounds(clip2);
    _oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);

    _twoOffset = clip2.transform.matrix;
    _twoOffset.tx = clip2.x – clip2.x;
    _twoOffset.ty = clip2.y – clip2.y;

    _oneClipBmpData.draw(clip2, _twoOffset);

    _onePoint = new Point(_oneRectangle.x, _oneRectangle.y);
    _twoPoint = new Point(_twoRectangle.x, _twoRectangle.y);

    if(_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
    {
    _returnValue = true;
    }

    _twoClipBmpData.dispose();
    _oneClipBmpData.dispose();

    return _returnValue;
    }

    }

    }
    Pardon me if I’m wrong about the lag, I’m still a noob at this.

    If you’re wondering how I knew you used hitTestPoint, I decompiled your SWF. Sorr yabout that ;) .

  2. This is an extremely old post, and I haven’t even touched Flash in the past 2 years :) I have moved on to iPhone development now.

    Thanks for your contribution regardless.
    Daryl

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>