- Home /
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...
can you share the code that you apply hitting force to ball and ball's rigidbody setting? this way we can give more precise suggestions.
Answer by Eno-Khaon · Nov 21, 2017 at 06:01 AM
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.
is there a way to find the ball trajectory at each bounce on the ground?
I'm afraid I don't know all the formulas for Unity's PhysX implementation off hand, so I can't offer any sound advice in that regard.
I do know that Physic $$anonymous$$aterials influence how high it would bounce, but current rotation speed (Angular Velocity) is a factor in the direction it would bounce, as well as any potential irregularities or flaws in the physics interaction that could occur (which would just throw logic out the window by that point). I'm sorry to say I'm not fully familiar with the collision interactions.
you can add OnCollisionExit code.. which unity calls after your collision... then just read the currect velocity vector from the RigidBody.... and re-run your predictive code again for where it will land again.
Answer by Grish_tad · Nov 20, 2017 at 12:10 PM
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.
Trajectory formula is given by
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, Where, Vo is the initial Velocity, sin θ is the y-axis vertical component , cos θ is the x-axis horizontal component.
Answer by JonPQ · Nov 20, 2017 at 05:20 PM
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.
Answer by sachinkhanna786 · Dec 27, 2017 at 08:00 AM
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.