Saturday, November 9, 2019

projectile physics - How to make an arrow land at a specific position in 3D world space


In my game when I click with the mouse on the terrain somewhere, I'd like the player to fire an arrow to that position in a parabolic fashion.



The arrow has a position, acceleration and velocity all using 3D vectors. I looked at the Trajectory of a projectile article on wikipedia, but I don't know how to apply it in my situation as it explains 2D mathematics. I was wondering if there's a solution for 3D vectors? Any good resources perhaps related to the 3D situation?



Answer



To expand on Kylotan's comment, You can use the 2D formulas in 3D. Assuming Y is up:


calculate the position of the target in X'Y'Z' space, where the X' axis is parallel to the arrow flight direction, Y' axis is up, and Z' is perpendicular to the X' and Y' axes.


diagram


Once you have X' and Y' calculated, you can convert back to real XYZ-space


Example


An archer is at (1,0,1). He wants to shoot an arrow to (4,0,5). We take X' to be the unit vector (0.6, 0, 0.8) since it points directly from the source to destination point. We then take Z' to be (-0.8, 0, 0.6) because it is a perpendicular, but since the arrow doesn't move in the Z' axis, we will ignore it. Your problem is now figuring out how to shoot an arrow from (0,0) to (0,5) in X'Y' space.


.. do 2D calculations here. Note that you'll probably want parametric functions of X' and Y' in terms of t, the time variable.


One way to abstract the conversion between the two coordinates is to use a transform matrix.



let archer = Vector3d(1.0,0.0,1.0)
let target = Vector3d(4.0,0.0,5.0)
let travel = target - archer
let transform = Matrix4d.CreateTranslation(-archer) *
Matrix4d.CreateRotationY(Math.Atan2(travel.Z,travel.X))
Vector3d.Transform(archer, transform) // transforms archer to (0,0,0)
Vector3d.Transform(target, transform) // transforms target to (5,0,0)

when we convert back from X'Y'Z' to XYZ, this is simply a reverse linear transformation.


let inverse = Matrix4d.Invert(transform)

Vector3d.Transform(Vector3d.Zero, transform) // transforms (0,0,0) to (1,0,1)

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...