Hi, I’m really having trouble figuring out how to throw a ball to a point (hoop). I’ve read a ton on motion trajectory, but not figuring it out. I attempted to convert a C# Unity script that supposedly does what I need, but it’s not working for me, perhaps i didn’t convert it correctly. The function should recieve the ball and rim’s positions, and angle, and return force vector (velocity?). Then I will apply it to the ball to make a perfect shot:
getBallVelocity = function( ballPos, rimPos, angleDegrees, gravity ){
const Vector3 = {
forward: new THREE.Vector3( 0, 0, 1 ),
up: new THREE.Vector3( 0, 1, 0 )
};
// Get angle in radians, from angleDegrees argument
const angle = THREE.Math.degToRad( angleDegrees );
gravity = gravity || 9.81;
// Positions of this object and the target on the same plane
const planarRimPos = new THREE.Vector3( rimPos.x, 0, rimPos.z ),
planarBallPos = new THREE.Vector3( ballPos.x, 0, ballPos.z );
// Planar distance between objects
const distance = planarRimPos.distanceTo( planarBallPos );
// Distance along the y axis between objects
const yOffset = rimPos.y - ballPos.y;
// Calculate velocity
const initialVelocity = ( 1 / Math.cos( angle ) ) * Math.sqrt( ( 0.5 * gravity * Math.pow( distance, 2 ) ) / ( distance * Math.tan( angle ) + yOffset ) ),
velocity = new THREE.Quaternion( 0, initialVelocity * Math.sin( angle ), initialVelocity * Math.cos( angle ) );
// Rotate our velocity to match the direction between the two objects
const planarPosDifferenceBetweenObjects = planarRimPos.sub( planarBallPos ),
angleBetweenObjects = Vector3.forward.angleTo( planarPosDifferenceBetweenObjects ),
angleUpRotated = new THREE.Quaternion().setFromAxisAngle( Vector3.up, angleBetweenObjects ),
finalVelocity = angleUpRotated.multiply( velocity );
return finalVelocity;
}
The code I converted from is here: https://forum.unity.com/threads/how-to-calculate-force-needed-to-jump-towards-target-point.372288/#post-2415612
Also, doesn’t need mass?
Please help, I’m running out of time, and if someone wants me to hire them to solve this, I’m willing to pay. Just contact me.
NOTE: I’ve also posted this question on stack overflow: https://stackoverflow.com/questions/65713249/ammo-js-three-js-calculate-force-vector-needed-to-make-a-basketball-shot
Thanks!
-Joe