# Determining Line of Sight in AS3 using collision detection, need help!

## Recommended Posts

raimondi1337    107

So I'm working on a project with a good friend of mine for our Programming IV class. We have all quarter to complete a project of out choice. We chose to make an arcade style survival game. Basically we want randomely generated rooms, Halo style weapons pickups and quake style power ups all with the goal of obtaining a high score, that we will record into a database and send to a web site.

We're on Christmas break for the next two weeks. We decided he was going to write the code for the room generation, and I was going to get a basi enemy AI done by the time we get back. I've been doing alright so far but I didn't consider everything else I would have to put in place to get this working
.

I generated a few NPC's on screen, a player, and a wall, and wrote the movement code for the player, now I want to have the NPC's move to the player, but only if they have a line of sight to him.

I'm thinking drawing an actual line between the two, and then doing a collision test on the line and the wall (which will eventually be an array of walls) is the easiest way. The hitTest function of AS3 uses the whole bounding box of the line, so it's useless. I found a method of using bitmap data to text each individual pixel, but its confusing me pretty well and I can't imagine it's very efficient.

Does anyone know the easiest way to determine collisions in AS3 or even an easier way to determine line of sight?

Project here if you want to look at it (the code is for the bitmap method and is buggy):

This is the Bitmap method I was trying to implement:
http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

##### Share on other sites
Kaze    948

The easiest way it to use a box2d port

http://www.sideroller.com/wck/

The built in flash collision functions are limited and ineffcent

Edited by Kaze

##### Share on other sites
raimondi1337    107
The easiest way it to use a box2d port

http://www.sideroller.com/wck/

The built in flash collision functions are limited and ineffcent

I don't think our grade would negetively be effected by using an API, but I don't think that's what our teacher had in mind when he approved our project. I'd prefer to stick to writing everything from scratch.

##### Share on other sites
eppo    4877

Ray tests would be simple if you could express a wall as a set of half-spaces (forming a convex bounding volume), like a k-DOP (2-DOP in your case).

##### Share on other sites
raimondi1337    107

I don't know what any of this means.

##### Share on other sites
EduardoMoura    298

Well, try and implement Separation Axis Algorithm  its very easy, and You can ray trace which is what you want. (There are a lot of Tutorials on SAT). I am not very familiar with flash, so I can't help you much further, but the SAT algorithm will help you in long term believe me.

##### Share on other sites
raimondi1337    107
Well, try and implement Separation Axis Algorithm  its very easy, and You can ray trace which is what you want. (There are a lot of Tutorials on SAT). I am not very familiar with flash, so I can't help you much further, but the SAT algorithm will help you in long term believe me.

I'll look this up, thanks.

##### Share on other sites
odrega    288

It's not very efficient, but something like this could work, if actual trigonometric calculations are thrown at it.

//Ray Trace - for loop
for(var i = 0; i < rayIterations; i++){

rectangle = new Rectangle(1, 1, ray.x, ray.y);
for(WallArray.length){
if(rectangle.hitTest(WallArray[whatever].rectangle)){
return true
}
}
}