0

I'm writing a function that takes in an object with a trajectory (including starting position, starting velocity, and acceleration, all represented as Vector3s) in 3D space and if it hits another object, returns the point of collision and time of the collision. I'm using kinematic equations with a timestep to detect possible collisions and I can get the point of collision that way, but once I have that I want to find the exact time that that collision would occur at.I thought of rearranging a kinematic equation to solve for time and plug in what I already had, but I can't figure out how I can use all three axes of motion to do this, since my other values are Vec3's and time is just scalar. I've thought about just doing the calculation on one axis, but I'm not sure if that would lead to an accurate result.

Would it be accurate to calculate just based on one axis, or is there a way to incorporate all three into the calculation? The formula I'm using to solve for time is:

t = (v_init +/- Sqrt((v_init)^2 - (accel * disp * 4 * .5)))/accel;

Where v_init is initial velocity, disp is total displacement, and accel is acceleration. I'm basing this off of the kinematic equation:

d = v*t + .5*a*t^2

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34
bad coder
  • 9
  • 3
  • You can base your calculation on just one axis if the initial velocity and acceleration are the same for all 3 axes. I think this is not the case because I immagine that on the z axis you should have also gravity acceleration. Are calculating the point of collision on the ground? – Eddymage Jul 31 '20 at 06:22
  • Do it component-wise and then create a vector of the resulting components. – molbdnilo Jul 31 '20 at 06:35
  • @Eddymage gravity is ignored but acceleration is constant and can be (and usually is) on all three axes. The initial velocity and acceleration are different for the three axes though. The point of collision is a just a simple plane – bad coder Jul 31 '20 at 06:37
  • 1
    @badcoder, why would you ignore gravitational acceleration in a projectile? And what is the acceleration you're talking about if not gravity? – Debargha Roy Jul 31 '20 at 06:58
  • 2
    Sorry, that was a bad way to put it, there is "gravity" but it can be in any direction, not just downward. That's the only acceleration though. Sorry if that sounds weird – bad coder Jul 31 '20 at 07:05
  • Actually you can include gravity in the a_z component: if the projectile has initial acceleration a' in the z direction, then a_z = a'-g. But actually this seems to me more a physics question rather than a coding question. – Eddymage Jul 31 '20 at 07:18

1 Answers1

1

Let me write in the general case. The component-wise motion law is

x(t) = x0 + v_x t + 0.5 a_x t^2

y(t) = y0 + v_y t + 0.5 a_y t^2

z(t) = z0 + v_z t + 0.5 a_z t^2

where (x0,y0,z0)^t is the initial position, (v_x, v_y, v_z)^t is the initial velocity vector, and (a_x, a_y, a_z)^t is the vector of acceleration. The 3rd component of the latter may include also the gravity acceleration.

I assume that the collision plane is horizontal, having thus equation z = k. Solve in t the equation

z(t) = k

for finding the time t_c in which the projectile hits the plane. Compute then the collision coordinates x(t_c) and y(t_c) using the above formula by substituting t with t_c.

If the plane has the general equation

a x + b y +c z + d = 0

I suggest to put the frame of reference on the plane, having the xy plane on the collision plane, and then apply the above procedure.

You may also solve the non linear system

x = x0 + v_x t + 0.5 a_x t^2

y = y0 + v_y t + 0.5 a_y t^2

z = z0 + v_z t + 0.5 a_z t^2

a x + b y +c z + d = 0

taking the solution for t>0 (I dropped the dependency on t for x, y and z).

To solve it in C++, you may search a math library, such as Eigen which has a module for non linear systems.

Eddymage
  • 1,008
  • 1
  • 7
  • 22