Michiel Standaert
Michiel Standaert

Reputation: 4176

How to implement line of sight restriction in actionscript?

I have a problem with a game i am programming. I am making some sort of security game and i would like to have some visual line of sight. The problem is that i can't restrict my line of sight so my cops can't see through the walls. Below you find the design, in which they can look through windows, but not walls. Further below you find an illustration of what my problem is exactly.

design

this is what it looks like now. As you can see, the cops can see through walls.

enter image description here

This is the map i would want to use to restrict the line of sight.

visual map

So the way i am programming the line of sight now is just by calculating some points and drawing the sight accordingly, as shown below. Note that i also check for a hittest using bitmapdata to check whether or not my player has been spotted by any of the cops.

private function setSight(e:Event=null):Boolean
{
    g = copCanvas.graphics;
    g.clear();

    for each(var cop:Cop in copCanvas.getChildren())
    {
        var _angle:Number = cop.angle;
        var _radians:Number = (_angle * Math.PI) / 180;
        var _radius:Number = 50;

        var _x1:Number = cop.x + (cop.width/2);
        var _y1:Number = cop.y + (cop.height/2);
        var _baseX:Number = _x1 + (Math.cos(_radians) * _radius);
        var _baseY:Number = _y1 - (Math.sin(_radians) * _radius);

        var _x2:Number = _baseX + (25 * Math.sin(_radians));
        var _y2:Number = _baseY + (25 * Math.cos(_radians));
        var _x3:Number = _baseX - (25 * Math.sin(_radians));
        var _y3:Number = _baseY - (25 * Math.cos(_radians));

        g.beginFill(0xff0000, 0.3);
        g.moveTo(_x1, _y1);
        g.lineTo(_x2, _y2);
        g.lineTo(_x3, _y3);
        g.endFill();
    }

    var _cops:BitmapData = new BitmapData(width, height, true, 0);
    _cops.draw(copCanvas);

    var _bmpd:BitmapData = new BitmapData(10, 10, true, 0);
    _bmpd.draw(me);

    if(_cops.hitTest(new Point(0, 0), 10, _bmpd, new Point(me.x, me.y), 255))
    {
        gameover.alpha = 1;
        setTimeout(function():void{ gameover.alpha = 0; }, 5000);
        stop();

        return true;
    }

    return false;
}

So now my question is: Is there someone who knows how to restrict the view so that the cops can't look through the walls? Thanks a lot in advance.

ps: i have already looked at this tutorial by emanuele feronato, but i can't use the code to restric the visual line of sight.

Upvotes: 4

Views: 547

Answers (3)

Michiel Standaert
Michiel Standaert

Reputation: 4176

I have found a way to do this. The answer was given at the gamedev.stackexchange-fourm :)

https://gamedev.stackexchange.com/questions/14001/how-to-implement-line-of-sight-restriction-in-actionscript

Upvotes: 0

Stephan van den Heuvel
Stephan van den Heuvel

Reputation: 2138

At a very high level, what about when you have a detection, try to shoot a ray from the cop that did the detection and the player. If you can shoot a ray without impacting any of the level, you have a valid detection. You could implement this in a variety of ways, but this is the standard approach.

Upvotes: 2

jhocking
jhocking

Reputation: 5577

Well first off, regardless of what you do with the visibility cones I would suggest using Feronato's line-of-sight code so that visibility to the player works correctly regardless of how the cones are drawn.* Actually chopping up the cone where it hits walls will involve a lot of complex math and probably be too processor intensive for real-time.

At this point you might want to just say "well whatever, it works" because getting the cone visuals to cut-off at the walls will be a lot of work and possibly not necessary. You should playtest things to see if players get confused.

That said, the simplest way to make the cones cut-off at the walls would be for each room's floor to be a separate graphic object that you can raise over the cone in the display stack. Check if the guard can see the room to determine if you need to raise the floor to cover the cone, or lower it to display the cone.

You could probably automate the creation of these floor objects (ie. everything is setup magically while loading the level) but for a first pass I would probably just put them in manually when drawing the level.

*Incidentally, as alxx suggested you probably want only to do the accurate check after first doing a rough distance check. Doing the accurate check too much could get costly.

Upvotes: 1

Related Questions