mdonati
mdonati

Reputation: 1099

Calculate initial velocity to set to rigid body so it reaches a target position with angle of launch, start position and target position as given

I need to shoot a ball from any height and make it bounce on a target position defined by the user. The angle of launch is also given. I've tried a couple of solutions so far:

Vector3 calcBallisticVelocityVector(Vector3 source, Vector3 target, float angle) {
         Vector3 direction = target - source;                            
         float h = direction.y;                                           
         direction.y = 0;                                               
         float distance = direction.magnitude;                           
         float a = angle * Mathf.Deg2Rad;                                
         direction.y = distance * Mathf.Tan(a);                            
         distance += h/Mathf.Tan(a);                                      

         // calculate velocity
         float velocity = Mathf.Sqrt(distance * Physics.gravity.magnitude / Mathf.Sin(2*a));
         return velocity * direction.normalized;    
     }
Vector3 calcBallisticVelocityVector2(Vector3 source, Vector3 target, float angle) {
        float distance = (target.Planar() - source.Planar()).magnitude;
        float a = target.y - source.y - distance;
        float halfGravity = -Physics.gravity.magnitude * 0.5f;
        float distanceSquared = distance * distance;
        float theta = Mathf.Deg2Rad * angle;
        float cosSquared = Mathf.Cos(theta) * Mathf.Cos(theta);
        float b = distanceSquared / cosSquared;
        float speed = Mathf.Sqrt((halfGravity * b) / a);
        Vector3 velocity = (target.Planar() - source.Planar()).normalized * Mathf.Cos(theta);
        velocity.y = Mathf.Sin(theta);
        return velocity * speed;
    }

The results I'm getting is that even the ball does go into the direction is expected, it falls earlier than it should be so the speed calculated by these methods seems to be lower than what is actually required to hit the target position. Rigidbody's mass is set to 1, Gravity is (0, -98, 0), rigid body's drag and angular drag is set to 0. What other variables could be affecting this behavior?

EDIT: One thing I forgot to mention is that I'm setting the resulting vector as rigid body's velocity, so I'm not using via the apply force method.

Upvotes: 2

Views: 1321

Answers (1)

mdonati
mdonati

Reputation: 1099

I adapted code gotten from here: https://answers.unity.com/questions/1131176/projectile-motion.html and now I'm getting the results I was expecting. I can always hit the target position at whatever angle I input.

private Vector3 calcBallisticVelocityVector(Vector3 initialPos, Vector3 finalPos, float angle)
{
    var toPos = initialPos - finalPos;

    var h = toPos.y;

    toPos.y = 0;
    var r = toPos.magnitude;

    var g = -Physics.gravity.y;

    var a = Mathf.Deg2Rad * angle;

    var vI = Mathf.Sqrt (((Mathf.Pow (r, 2f) * g)) / (r * Mathf.Sin (2f * a) + 2f * h * Mathf.Pow (Mathf.Cos (a), 2f)));

    Vector3 velocity = (finalPos.Planar() - initialPos.Planar()).normalized * Mathf.Cos(a);
    velocity.y = Mathf.Sin(a);

    return velocity * vI;
 }

Upvotes: 1

Related Questions