Sabha B
Sabha B

Reputation: 2089

line of sight 2d

Can anyone explain how the line of sight works in 2d? Which will be really help full for my 2d experiments. The experiment am working is a simple 2d simulation. Player move in the world from one place to other , my world exactly looks like this. I did the character movement successfully from one way point to other (A to G) , my goal is - when the character passes each point it has to perform some search in that area before it leaves to next point. To achieve I felt way point is better solution , can anyone help me on this.Thanks!

Edit : As soon as the player enters a room/checkpoint I will take user to next scene like this
enter image description here

where the pickups are place some where on the canvas and my player have to collect them all and leave the area - Back to Map scene.

Upvotes: 2

Views: 1706

Answers (2)

I didn't understand your paragraph, but to answer your question (assuming we want to know if an enemy can see the player, in a top-down game)

  1. Check that the player is in the enemy's cone of vision. We do this by computing the (absolute value of the) angle between the enemy's sight-vector and the vector spanning between the enemy and the player. If it is 0°, the enemy is facing the player. If it is 180°, the enemy is looking away from the player. Check that it is < 30°, say, to give the enemy a cone-of-vision of 60° (or <45° to give the enemy a cone-of-vision of 90°).

  2. Check that there is nothing between the enemy and the player. This boils down to checking for a collision between the walls (or anything else) and the line segment spanning the enemy and the player.

    Note that if there are many walls, checking for collision with all of them can be very expensive. Narrowing the search down to only a few objects is called pruning, and outside the scope of this answer (see here for more information)

Upvotes: 7

corsiKa
corsiKa

Reputation: 82579

Line of sight can be a very expensive algorithm. The objective is to determine whether or not an object exists between two points (the eye and the object, you could say.)

To do this, you have to get a list of all potential objects in the way (a QuadTree might be useful for this, http://en.wikipedia.org/wiki/Quadtree) and test each of them to see if they intersect the line between the eye and the object.

You may want to research Pathfinding: http://en.wikipedia.org/wiki/Pathfinding

Upvotes: 1

Related Questions