 # Thread: How to detect edge position that is directly facing Active?

1. ## How to detect edge position that is directly facing Active?

Hello,

Is there a way to calculate the edge position that is facing Active, i can get it by launching object in direction of Active starting from Active(0,0) and storing the position of the object when it leaves the area, but i think there should be a better way "using math?!", what do you think?
Untitled.jpg  Reply With Quote

2. the "using math" way of doing this would be to make a line segment for each side of your frame, then test if they intersect with the line coming from the player.

if you are lazy or don't know how to do that, just set a very long invisible object to the player's position and orientation at all times. place detectors on the edge of your stage and see which one it overlaps.  Reply With Quote

3. Thanks for replying make a line segment for each side of your frame, then test if they intersect with the line coming from the player.
Well, as a beginner i am afraid i do not understand this.
I almost finished my first game, the thing is i wanted to learn all the possible solutions while progressing especially using math.  Reply With Quote

4. Just off the top of my head (so possibly containing a few mistakes)...

Formula for a straightline...
y = (m * x) + c

Calculate x and y components of line slope...
mX = cos( anglePlayer )
mY = 0 - sin( anglePlayer )

Calculate slope...
m = mY / mX

NOTE: If the player is facing precisely up/down/left/right (0/90/180/270 degrees), these are special cases that you'd need to check for separately, as you'd get "divide by zero" errors otherwise (but the results should be obvious in these cases anyway).

Rearrange original formula and substitute in a position we know is somewhere along the line (the player's position)...
c = yPlayer - (m * xPlayer)

X coordinate where the line leaves the top of the screen...
xT = (screenTopY - c) / m

X coordinate where the line leaves the bottom of the screen...
xB = (screenBottomY - c) / m

Y coordinate where the line leaves the left side of the screen...
yL = (m * screenLeftX ) + c

Y coordinate where the line leaves the right side of the screen...
yR = (m * screenRightX ) + c

If (xT < screenRightX ) and (xT > screenLeftX ) and (mY < 0)
x = xT; y = screenTopY

If (xB < screenRightX ) and (xB > screenLeftX ) and (mY > 0)
x = xB; y = screenBottomY

If (yL < screenBottomY) and (yL > screenTopY) and (mX < 0)
x = screenLeftX; y = yL

If (yR < screenBottomY) and (yR > screenTopY) and (mX > 0)
x = screenRightX; y = yR  Reply With Quote

5. Using 'Sinus'/ 'Cosinus' fuctions easily gets you this edge point without further adue ( No extra objects or doodads involved ).
Sorry for not having time to explain further.  Reply With Quote

6. I'm also using slope intercepts all time,
here are my quick notes on the matter, just in case:

m coefficient= (Y'-Y) / (X'-X )
or tan(angle of segment)

b parameter= Y - (m * X )

you need to calculate these only for the player actually,
because as MuddyMole said, the edge lines equations are just
y= 0/or Ybottom
and
x=0/or Xright

then for the intersect point:

x=(b'-b)/(m-m')

or, if you have Y
x= (y-b) / m

y=m(x)+b Originally Posted by MuddyMole
NOTE: If the player is facing precisely up/down/left/right (0/90/180/270 degrees), these are special cases that you'd need to check for separately, as you'd get "divide by zero" errors otherwise (but the results should be obvious in these cases anyway).  Reply With Quote

7. Hello,
Thanks guys these are interesting info, i already use sin / cos in my game to detect the nearest objects, tried to do the same thing with the screen edge but couldn't figure how to do it, i only have either x or y of the edge, hopefully your formulas will help me i will try them ASAP.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•