I'm trying to solve an issue where I basically have a vector inside of a rectangle. I want to figure out if the vector continues its trajectory, what side will it strike? The vector is given as an (x, y) pair showing velocity on the two planes. I also have its position within the larger square, and the size of the square.
How would I figure out something like this?
Presumably you also have a starting position somewhere within the rectangle. Translate to put this point at the origin. If you compare the angle of the given vector to the angles of the rays from the origin out to the corners of the translated rectangle, you can easily tell which side will be hit. There’s no need to actually calculate these angles, either. You can use the slope of the rays together with some sign checks to narrow down the quadrant in which the collision will occur.