• Create Account

## Line of sight

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1VisualChaser  Members

314
Like
0Likes
Like

Posted 26 July 2013 - 05:57 PM

I am currently working on a game in flash (with AS3), and I ran into a weird issue with the line of sight of the enemies. It's easier to explain with a picture. Blue = walls. Green = enemy. Red = area the enemy can't see the player.

This is the codes:

var playerVisible:Boolean = true;
var angleRadian = Math.atan2(_player.y - _enemy[i].y, _player.x - _enemy[i].x);
var angleDegree = angleRadian * 180 / Math.PI;

var enemyDistancex:Number = _player.x - _enemy[i].x;
var enemyDistancey:Number = _player.y - _enemy[i].y;
if(enemyDistancex < 0)
enemyDistancex *= -1;
if(enemyDistancey < 0)
enemyDistancey *= -1;
var enemyDistance:Number = Math.sqrt((enemyDistancex * enemyDistancex) + (enemyDistancey * enemyDistancey));

while(enemyDistance > 0 && playerVisible)
{
if(_walls.hitTestPoint(_enemy[i].x - (Math.sin(angleDegree * (Math.PI / 180)) * enemyDistance), _enemy[i].y - (Math.cos(angleDegree * (Math.PI / 180) * -1) * enemyDistance), true))
{
playerVisible = false;
}

enemyDistance--;
}


I tried a lot, but I can't seem to figure it out. Could someone help me out?

### #2David.M  Members

731
Like
0Likes
Like

Posted 27 July 2013 - 02:46 PM

I'm not quite understanding the picture. Is that the current behavior or the behavior you want to implement?

### #3VisualChaser  Members

314
Like
0Likes
Like

Posted 27 July 2013 - 03:28 PM

That's the current behavior the code below the picture creates.

By the way, I use the same codes to calculate the enemy rotation for targeting the player and it works fine, but somehow it is buggy when I use it for the line of sight.

5832
Like
0Likes
Like

Posted 27 July 2013 - 03:37 PM

Why aren't you using vectors? Calling atan2 and then calling sin, cos with the result is usually a sure sign you should be using 2d vectors instead of trigonometry for the problem.

Edited by Paradigm Shifter, 27 July 2013 - 03:37 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #5VisualChaser  Members

314
Like
0Likes
Like

Posted 27 July 2013 - 03:57 PM

Using vectors how? Sorry I'm kind of a newbie when it comes to maths.

5832
Like
0Likes
Like

Posted 27 July 2013 - 04:04 PM

Well you calculate the vector

player - enemy[i]

and do a load of unnecessary trig stuff with it when that gives you the 2d direction vector from the enemy to the player directly...

I'd definitely recommend reading up on vectors, you rarely need to use trig and the stuff you do need/are using involving vectors is just the very basic stuff.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #7VisualChaser  Members

314
Like
0Likes
Like

Posted 27 July 2013 - 04:24 PM

I managed to fix it, thanks for the help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.