Currently in the process of developing a basketball game using three.js and ammo.js. The positions of the hoop/rim and ball are constantly changing, making the shots dynamic and relative.
I am tasked with determining the correct force vector needed for a successful shot. As part of the gameplay, users will swipe to shoot, influencing the ideal force required for the "perfect shot."
I have come across various examples of code and equations for trajectory calculations, ballistics, etc., which I have been converting from C# to JavaScript as most of the resources I find are on Unity forums.
For this project, I require a function that can calculate the initial force vector3 to be applied to the ball based on factors such as the ball's position, the hoop's position, the mass of the ball, and gravity. Additionally, the function will need to receive the initial angle or maximum height (terminal velocity y) as parameters, as observed in existing equations and calculators.
UPDATE:
In my exploration, I adapted a script sourced from Unity forums into JavaScript/Three.js format:
insert converted JavaScript code here
The function is being called in this manner:
const velocity = getBallVelocity( ball.position, rim.position, 45 );
ball.body.applyForce( velocity.x, velocity.y, velocity.z )
However, the shot is incorrect in direction and lacks power. It appears there may be an issue with the rotation at the end of the function, while the weak shot could be due to a lack of appropriate mass multiplication. The ball has a mass of 2, so it might be necessary to multiply certain Y values by 2? Any guidance would be appreciated!
Below is the original C# script that served as the basis for my conversion attempt:
insert original C# script here
Thank you!