I’m trying to shoot a cannonball out of a cannon in a 3D space, but the cannonball lands significantly beyond the target. I’m using physics calculations to determine the initial velocity based on the launch angle and time to land.
The problem is not so complex because the only force applied in this scenario is gravity, the object has no drag. And yet for the life of me I can’t figure out what’s wrong.
For my X and Z axes I’m using Uniform Rectilinear Motion with the equation:
x = v0x * cos(alpha) * t + x0*
where x
is the position in the x axis, v0x
the initial velocity (what I would like to figure out), alpha
the angle, t
the time to land and x0
the initial position.
For my Y axis I’m using uniformly accelerated (by gravity only).
Mainly,
y = y0 + v0y * sin(alpha) * t - 1/2 gt^2
where y
is the position in the y axis, y0
the initial position, v0y
the initial velocity (also want to figure it out), and g
is gravity force.
And yet, applying all of this results in a very wacky cannonball launch…
I’ve looked in many other posts, but either their requirements weren’t the same as mine, or they used a different equation I could barely understand
Here is the code:
void ShootCannonball(Vector3 targetPoint, Vector3 originPoint, float T)
{
float angle = initialAngle * Mathf.Deg2Rad; // Convert to radians
GameObject newCannonball = Instantiate(cannonballPrefab, originPoint, Quaternion.identity);
Rigidbody rb = newCannonball.GetComponent<Rigidbody>();
float vox = UniformMotionX(targetPoint.x, originPoint.x, angle, T);
float voy = AcceleratedY(targetPoint.y, originPoint.y, angle, T);
float voz = UniformMotionZ(targetPoint.z, originPoint.z, angle, T);
Vector3 push = new Vector3(vox, voy, voz);
// Fire!
rb.AddForce(push, ForceMode.VelocityChange);
}
float UniformMotionX(float target, float origin, float angle, float T)
{
return (target - origin) / (T * Mathf.Cos(angle));
}
float AcceleratedY(float target, float origin, float angle, float T)
{
return (target - origin + 0.5f * currentGravity * Mathf.Pow(T,2)) / (T * Mathf.Sin(angle));
}
float UniformMotionZ(float target, float origin, float angle, float T)
{
return (target - origin) / (T * Mathf.Cos(angle));
}
A link to the problem in video format: https://imgur.com/a/8hfdvh6
Thank you for any tips you can give me, and anything else you need please ask
In the video you can see the aim is OK but it always overshoots it
1