Reputation: 512
I'm developing a 2d game in JS and stuck on a trigonometry problem.
Problem: Player shoots a target. The shot is blocked by an obstacle. The obstacle is between the player and the target. I need to figure out the coordinates of this obstacle.
I have 3 pieces of information:
How do I figure out the (x,y) of the obstacle in the way of the shot?
Bonus points: An efficient solution in Javascript.
Picture for reference.
Upvotes: 0
Views: 127
Reputation: 11347
E
*
/|
/ |
O / |
* |
/| |
/ | |
/ | |
S *---*---*
P Q
SQ = x2 - x1
EQ = y2 - y1
SE = sqrt(SQ^2 + EQ^2)
SO = known
SP:SQ = SO:SE => SP = SO:SE * SQ
OP:EQ = SO:SE => OP = SO:SE * EQ
Upvotes: 1