In this, I solve for arrow velocity given a target. Then I used a raycaster to find where the cursor intersects the ground instead of doing that math myself.
I recently saw a post where someone shared some of the work they’ve done, so here’s my (cleaned-up) work for solving arrow velocity given a target.
Hm. Do you solve it in 3D? Would 2D be simpler as the shooting point, the target and the trajectory are all in a common vertical plane, so the initial state is defined by gun altitude, distance to target and angle of shooting?
yes, it’s solved it in 3D. I think it sets up the force nicely. I have gravity as f = (fx = 0, fy = -9.8, fz = 0). You could add a little wind effect or something with
const WIND = new THREE.Vector3(0, 0, 1.5);
/// addScaledVector(WIND, dt);...
But you’re totally right, you can solve this in 2D (save some paper) and convert back to 3D.
As I think about it, the wind effect - or even a more complicated force model - doesn’t affect the hand calculations. (I just woke up with my coffee.)
2D would have been the way to go on paper. It’s pretty much 2D anyway in the code. The code uses dx and dz to define the horizontal direction, solves the arc, and then splits it back into vx and vz.