How to predict where the ball will land after being hit by bat?

I’m working on a cricket game which uses the unity’s own PhysX. I need to predict where the ball will land after it is hit by the bat. The fielder has to run toward the ball judging where the ball will land. The ball has some drag. I have searched a lot of solutions but nothing works…

Projectile motion is something that a body undergoes when it is thrown in air. The path taken by the projectile motion is called trajectory. A bomb released from an aeroplane in level flight, bullet getting fired from gun, an athlete throws a javelin illustrates the trajectory motion.

alt text
Trajectory formula is given by
alt text

Where,
y is the horizontal component,
x is the vertical component,
g= gravity value,
v= initial velocity,
θ = angle of inclination of the initial velocity from horizontal axis,
alt text
Where,
Vo is the initial Velocity,
sin θ is the y-axis vertical component ,
cos θ is the x-axis horizontal component.

This works well with Vectors… for motion.
S = ut + .5 a t^2
where
s= distance.
u= starting velocity
a = acceleration / gravity
t = time

Use that, just for vertical motion, where U = vertical component velocity of the ball being fired up.
S(distance) = - height above floor. a is gravity.
re-arrange formula to get ‘t’
t^2= (s-ut) / .5a
t = sqr( (s-ut) / .5a)
This will give you time the ball is in the air… (without drag)
Its not trivial to take your ball move vector (without Y) to get final landing point.
end point = start point + vectorMove(xz only).normalized * time;

Your problem… is the drag. much harder to calculate with variable speed.
Suggestion… do it more like wind… a small vector added to position every time slice.
That way you can again calculate deviation in final position, based on that time you calculated.
deviationDeltaVector (drag or wind, in xz planes only) * time. and add that into final landing position.

Have done this before in golf game, grenades, and hunting (bullets) tricky, but you can get something convincing using this method.

Sorry, but I’m going to be a little lazy in this response…

I previously reviewed this subject (the most recent was here). I outline the general basis of the physics interactions per physics timestep, along with a basic idea of how to trace a path for them. Because Drag modifies the current force at any and every given point in time, it becomes more reasonable to simply run a quick calculation of the physics ahead of time and see how things match up.

So, to reiterate what I went over there:

rigidbodyDrag = Mathf.Clamp01(1.0f - (rb.drag * Time.fixedDeltaTime)); // Per FixedUpdate()
// This means that if your drag is equal to the framerate of FixedUpdate(), your object will lose 100% of its speed every frame

velocityPerFrame = lastFrameVelocity + (Physics.gravity * Time.fixedDeltaTime);
velocityPerFrame *= rigidbodyDrag;
positionPerFrame += (velocityPerFrame * Time.fixedDeltaTime);

Then, to implement that in a simple example:

// C#
List<Vector3> positionList;
// ...

positionList = new List<Vector3>();
// ...

// Example maximum of 5 seconds
int maxIterations = Mathf.RoundToInt(5.0f / Time.fixedDeltaTime);
Vector3 pos = ball.position;
Vector3 vel = ballRigidbody.velocity;
float drag = ballRigidbody.drag;
positionList.Add(pos);
float elapsedTime = 0.0f;

for(int i = 0; i < maxIterations; i++)
{
	vel = vel + (Physics.gravity * Time.fixedDeltaTime)
	vel *= drag;
	pos += vel * Time.fixedDeltaTime;
	elapsedTime += Time.fixedDeltaTime;
	positionList.Add(pos);
}

The moment the bat makes contact with the ball, you should have a new velocity value available for the ball, so this can be handled at that moment in order to determine where the ball will travel to. Every point at a height reachable by a player can then technically be considered valid and you’ll know how long it will take the ball to reach that point based on how far into the List<> the position value is located. By default, FixedUpdate() runs 50 times per second, so the time to reach any given position can be easily tracked with this in mind.

To provide further accuracy (and, depending on your defined timeout, efficiency), either raycasts in the predicted direction of movement at each timestep or a hard height cutoff can be checked for contact with the ground and stop the calculations sooner.

You can extract files from cricket 07 using biggui to check this or can extract files from don bradman cricket 17.

,you can take clues from don bradman cricket 17 game.