I am working on a 2D game that involves square creatures and projectiles.
Each projectile moves several creature-lengths per game tick, so I can't just check for any creatures at the projectile's location every tick- this skips any creatures in-between the projectile's starting and ending positions.
Each tick, I have the location projectile_x and projectile_y, delta_time and speed of the projectile, which result in the distance_moved. I also have the direction of the projectile in radians. This allows me to get the trajectory lines starting and ending coordinates, (projectile_start_x,projectile_start_y) and (projectile_end_x,projectile_end_y).
For each creature with center (creature_x,creature_y) and square width creature_size. How can I find out whether a projectile's trajectory has intersected a creature?
Edit: I don't mind approximating to assume that creatures are in fact circles.
Edit Edit: Actually, squares seem simpler from these answers.
Basically, each frame, we have a line segment from $(p_x,p_y)$ (where those denote
projectile_xandprojectile_y) in direction $(\cos(\theta),\sin(\theta))$ (where $\theta$ is short fordirection. $\theta=0$ refers to the right, horizontally) with length $d$ (which is, you guessed it,distance_moved).If we are to make this a line opposed to only a line segment, then the following equality would describe its path:
$$(y-p_y)\cos(\theta)=(x-p_x)\sin(\theta)\tag{$*$}$$
We can view creatures as circles instead of squares if we know the direction of the line; a line with direction $\theta$ intersects the circle with center $(c_x,c_y)$ and radius
$$r=\frac s2\cdot ((\sqrt2-1)|\sin(2\theta)|+1)$$
if and only if it intersects a square with side lengths $s$ and center $(c_x,c_y)$.
So first, we find the candidates. What circles intersect the line described at $(*)$? Well, it only intersects a circle if the distance from the line to the center of the circle is less than or equal to the radius. The distance from $(c_x,c_y)$ to $(y-p_y)\cos(\theta)=(x-p_x)\sin(\theta)$ is exactly
$$\left|(c_x-p_x)\sin(\theta)-(c_y-p_y)\cos(\theta)\right|$$
So if this is less than or equal to $\frac{s}{\cos(\theta)}$ (where $s$ is
creature_size), then the creature is on the line describing the projectile, but not necessarily on the line segment.This is the next step. Checking whether or not it reaches the square representing the creature is luckily not very hard. First, note that the end points of the segment are at $(p_x,p_y)$ and $(p_x+d\cos(\theta),p_y+d\sin(\theta))$. Now we need to check:
\begin{align} \min(p_x,p_x+d\cos(\theta))&<c_x+\tfrac s2\\ \max(p_x,p_x+d\cos(\theta))&>c_x-\tfrac s2 \end{align} and \begin{align} \min(p_y,p_y+d\sin(\theta))&<c_y+\tfrac s2\\ \max(p_y,p_y+d\sin(\theta))&>c_y-\tfrac s2 \end{align}
If those are all true, then the square overlaps the line segment at some point.
You could do these three checks in any order you like, however, since the first seems less computationally heavy, I'd go with my suggested order. A different order would be more efficient if you expect many of the creatures to be in the line the projectile is moving but not necessarily in range of the projectile.