# Basketball simulation needs precise physics

Hey all!

I am currently working on a basketball physics simulation and having a little trouble with the shifing and scaling of numbers.

Basically I want to (initially) have the player make the shot from anywhere on the court. I am doing this by measuring the distance between the transform that instantiates the ball and a transform at the center of the basket.

Then I record the exact amount of velocity on the y and z that it takes on the ball to make a perfect shot from a near and far distance.

I update the distance between the launch point and the basket on Update then when a shoot function is called I calculate the amount of force to apply to the insantiated ball as such:

``````driver = (distance - minDistance) / (maxDistance - minDistance);
yVelocity = driver * (maxYVelocity - minYVelocity) + minVelocity;
zVelocity = driver * (maxZVelocity - minZVelocity) + minZVelocity;

instantiatedBall.velocity = transform.TransformDirection(Vector3(0,yVelocity,zVelocity));
``````

Shots close will usually make it (except from the sides) and very far will usually make it. But about half way through the distance range most shots fall just short of the basket (front of the rim).

Any help would be appreciated… or advice for a different approach for precision physics would be great also.

Thanks,
Matt

Iterative Euler-step physics like those in PhysX are simply not accurate enough over many steps.

You could increase the number of steps per frame to improve precision, or implement a simple script that analytically solves the shot each frame.

For instance:

Solve for the time it would take for the ball to go to the net.

Solve for initial X/Z velocity.
Solve for initial Y velocity.

On each frame:
Solve for x displacement using initial position, time and x/zvel.
Solve for yvelocity using time, initial yvel and acceleration due to gravity.
Solve for y displacement using time, initial yvel, current yvel and acceleration.

set the transform on the ball.

This will give you an accurate ball position.

I would repeat this process for more distances: 1/4, 1/2, 3/4, etc. Stick the results in an array, and when the player shoots, use the two closest values with the code you posted above. If this doesn’t work, I’d try doing even more steps before attempting a different method.

If this were my game, this is what I’d try.

BTW: this gives me an idea for how I want to program a Jump function in my game. Thanks!