# Creating line trigger!

This topic is 4062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The plan: Draw a line (represents the trigger) any direction. Scenario: Player walks over the trigger Looking for suggestion on how I should check which direction the line was crossed. example: if the player crosses the right side of the line to the left side of the line, the trigger has different responds then from the other direction. Pseudo is okay, but keep in mind my math is horrible.

##### Share on other sites
Do you understand how to check whether the trigger has been crossed at all, in either direction?

The directional thing is easy. Two approaches.

One is to explicitly store a second vector that points in the direction of right...if the player's motion is in the same direction (dot product is positive), then the player moved from left to right. If the dot product is negative, the player moved from right to left.

Another approach is really the same, but doesn't require storing the second vector. The trigger line is defined by a vector, or by two points...a start point and an end point. On the fly, assume that if you look in the direction the trigger line is pointing, then if you glance to the right you are seeing the right side of the line, if you glance to the left you are seeing the left side of the line. The trigger is itself directional. In this case, you can use the perpendicular dot product (perpdot)...if its positive, you're moving in one direction, if its negative, you're moving in the other direction. The following presentation by math coder extraordinaire, Jim Van Verth, defines the perpdot function, on page 32:

Dot Product Fun

##### Share on other sites
Some basic knowledge of linear algebra is required for you to get the trigger working properly. You have an equation for a line in standard form, Ax + By = C. A line cuts your space into two halves, as though you drew a line down a piece of paper to split it into two sides. If we treat (A,B) as a vector, we can say it points in the direction considered to be the "front" side of the space. (-A,-B) points toward the "back" side. This vector is also perpendicular to (i.e. forms a right angle with) the line itself. If you plug in a point (x,y) into the left side of the line equation (Ax + By) and the result is greater than C, then the point is on the front side. If the result is less than C, the point is on the back side. If the result of the dot product is equal to C, then the point is actually on the line. The operation "plugging in the point (x,y)" is the same as taking the dot product between the vector (x,y) and the vector (A,B). So if on frame N we do the dot product and the result is greater than C, and on frame N+1 we do the dot product and the result is less than C, then we know the point started on the front side and went to the back side between frames.

So now the question becomes, how do we arrive at the values A, B, and C? As grhodes mentioned, a line is defined by a start point and an end point. If you subtract the two points, you get a vector. But this vector is in the same direction as the line, i.e. is doesn't point toward the front or the back sides. We fix this by taking something known as the "perpdot", which rotates the vector counter-clockwise until it points toward one of the sides (which we consider the front side), and forms a right angle with the original line. To summarize, you subtract the start point from the end point, and do a perpdot on that to get (A,B). If you actually want the front to be the other side, then you use (-A,-B)1. How do we get C? This is where our friend the dot product comes in again. You take the dot product of the start point (or end point), with the vector (A,B). That is, you find the result of Ax + By, where (x,y) is either your start or end point, and that's C. You're then good to go.

It would be a good idea to check out other resources on the net until you find something that makes the most sense to you. I've done a fair amount of simplification since you said you're not good at math, but you never know what you're capable of understanding until you try!

1 After finding the perpdot, most people like to normalize the vector (A,B). You do this by dividing (A,B) by its length. This gives the dot product the more intuitive geometric meaning of perpendicular distance.

##### Share on other sites
I want to draw the third point that is 90degrees from the trigger, Im not sure how to figure that out? So that the trigger has an "L" shape.

"A" is always the first point drawn then "B" (any direction)

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633734
• Total Posts
3013590
×