mob/Archer/proc/Volley(obj/Target/trg)
var/mob/Arrow/arrow=new(src.loc)//creates an arrow on archers loc
walk_towards(arrow,trg,0,1)
Problem description:
In my game I want some archers to fire a volley of arrows to a target. The problem is that the arrows change direction to reach it. Is there a lib on 360 degrees movement? Or is there something that can help me fix this issue? btw I use pixel movement
What you want is for the arrow to move along the vector from the archer to the target. In DM, a vector could be represented by a pair of variables containing the x and y components.
The vector from the archer to the target is simply the target's position - the archer's position. This is called the displacement vector. In DM, to subtract positions, you first have to get the positions. It might be most natural to use the centers of each object's bounding box, so our displacement is:
Note: If you add this vector to the archer's position, then you end up at the target's position. In other words, if you start at the archer's position, go east by dx pixels, and go north by dy pixels, you end up at your target. The arrow could do that, but you want to slow it down so it's not an instant thing. (Bullets might do that, though.)
The arrow needs to move by its velocity vector every frame. The velocity vector is calculated by scaling the displacement vector by a number, called a scalar, such that the magnitude of the result is a desired speed.
The magnitude of a vector (dx, dy)
has been provenfor our purposes is defined to be:var magnitude = sqrt(dx*dx + dy*dy)
The scalar ends up being:
I think of it this way: dividing a vector by its magnitude will make its magnitude 1, but it will still point in the same direction (since scaling a vector only changes its length). When you multiply 1 by any number S, you end up with a result of S. Therefore, if you divide a vector by its magnitude and then multiply it by S, the resulting magnitude of the vector will be S.
So the arrow's velocity vector will be:
With that, you can move the arrow every frame with my library's Translate() method.
A good read for using vectors in games is the Unity manual's Vector Cookbook. DM doesn't currently support Unity's vector syntax, but its explanations and figures can still help with understanding.