Search code examples
mathgraphicslanguage-agnosticcollision-detectiontrigonometry

How to repositionate a bouncing ball after it passed throug a surface


I have a bouncing ball which can collide lines with random slope. The ball pass through the lines of a bit and i need to set back the ball at a "radius" distance from the line.

Ball (with variables x, y and radius) travels at speedX and speedY (obtained from vectors directionX and directionY multiplied for a variable) and i can know the distance (dist) between the center and the line so i can know how many pixels the ball passed through the line

enter image description here

Think that in the example the ball passed 10 pixels (radius - dist) after the line, i need to set back the center of the ball 10 pixels in the opposite vector (directionX directionY). My question is:

How can i calculate how to split those n pixels between x and y so i can subtract them from the center coordinates?


Solution

  • I can imagine 4 different resolutions of what you have, and it is unclear which one you want.

    Here they are, where the black arrow is the movement of the centre of the ball between the frame before collision and the frame you are asking how to draw.

    4 different options, illustrated

    A) the situation which you have now.

    • pro : simple
    • con : ball is in a physically unacceptable position

    B) you compute where the ball should be after having bounced (assuming an elastic chock)

    • pro : most accurate
    • con : you don't have a frame with the ball in contact with the surface (but do you care ?).

    C) The position of the ball is A, brought back to being tangent at the surface, with a correction that is orthogonal to said surface

    • pro : conserves accuracy in direction parallel to surface
    • con : centre of the ball not on the reflected line (i.e. we take liberties with Descartes' law)

    D) The ball is still on the incoming line, but stopped when it is tangent to the surface.

    • pro : only the speed/timing is messed with
    • con : err.... out of ideas here. Still not as precise as B.

    Well, disregarding all the drawings, it is much easier to take only the centre of the ball, and consider it hits a line that is at 'radius' of the real surface (and parallel to it), so we only have mechanics for a single point. Thus from the previous image, we get the formulation in terms of the following red objects :

    illustration with point mechanics

    So what do we need to do all this ? The undisturbed trajectory starts at point S, ends at point E (the endpoint of situation A). We will call C the collision point between both lines (red one and trajectory, thus the endpoint of trajectory D).

    I will assume we are always in the case of a collision, thus the point of intersection C between the undisturbed trajectory and the surface always exists.

    You will also need the vector u that is perpendicular to the surface. Be sure to take a unit vector that points towards the side where the ball is. Thus if your slope has an equation ax+by+c=0, start with the vector ( a/sqrt(a*a+b*b) , b/sqrt(a*a+b*b) ) and multiply both coordinates by -1 if it points to the wrong side.

    Then, to shift the line by a distance r in the direction of u, you want the equation a(x-r*u.x)+b(y-r*u.y)+c=0 thus ax+by+c-r*(a*u.x+b*u.y)=0

    So if r is the radius and ax+by+c=0 your surface, the red line's equation is ax+by+c+r*sqrt(a*a+b*b)=0 or -r if the ball is beneath the line.

    I will write PQ the vector starting at point P and ending at point Q, thus coordinates of said vector will be (Q.x - P.x, Q.y - P.y) and a . between two vectors will mean a scalar product.

    So you can express SE in terms of the variables you named directionX, directionY and dist.

    • A) Move center by SE. Yay, finished !
    • B) Get C. Move center by SE - 2 * (CE . u) * u : thus the total move, but removing twice the normal component of CE that goes beyond the surface, effectively mirroring the CE vector by that surface.
    • C) Get C. Move center by SE - (CE . u) * u : the same, but remove the normal component of CE only once, effectively projecting the CE vector on the red line.
    • D) Get C. Move center by SC.