Ball collision time with ceiling

I know to to do collision time with the ground. Here's how I calculate that: gravityY = positive 9.81

float timeToGround = (velocity.y + Sqrt(velocity.y*velocity.y + 2f * gravityY * distanceToGround)) / gravityY;

How do I calculate the collision time to the roof/ceiling based on distance and initial velocity?

float timeToRoof = ?

I don't know where to look, thanks so much for your help

2 answers

  • answered 2018-11-08 08:54 MBo

    You need to solve quadratic equation for unknown time t

    h = y0 + Vy * t  - g*t^2 / 2
    or 
     g*t^2/2 - Vy*t + (h-y0) = 0  // a,b,c coefficient grouped
    

    for known height of starting point y0, roof height h, gravity g, y-component of initial velocity Vy

  • answered 2018-11-09 05:42 Sami

    Hehe, thanks for your help. Here's the answer:

    float timeToRoof = (-velocity.y + Sqrt(velocity.y * velocity.y + 2f * gravity.y * distanceToRoof)) / gravity.y;
    
    • gravity.y = negative -9.81
    • velocity.y = current velocity
    • distanceToRoof = current distance to roof