Search Unity

Whack Ball - need to get the physics right

Discussion in 'Physics' started by BroncoBilli, Oct 15, 2018.

  1. BroncoBilli

    BroncoBilli

    Joined:
    Oct 8, 2017
    Posts:
    90
    I've seen a lot of "how do you throw an object" questions. My project needs to have a paddle that hits a ball... I want the ball to bounce off the paddle realistically. The ball and the paddle both have rigidbody on them, and colliders, and the ball has a mass of 1 and the paddle has a mass of 100. But when the paddle hits the ball, the ball MOVES, but it doesn't smack off the paddle fast. it's as if the collision is being detected but the velocity isn't transferred. Question is: why? When two balls are flying through the air (and I've reduced gravity to be very small, for testing), one ball hitting another will impart spin and bounce and it looks realistic. But the paddle hitting the ball, no such luck. Now on the paddle, I'm moving it based on the motion controller's position. Every frame. And in the rigidbody for the paddle, I'm setting the position xyz and the rotation xyz to be "constrained". I don't want the paddle moving around based on hitting the ball, I want the ball to move from being hit by the paddle.

    Given the above, what am I doing wrong?

    Do I need to handle this in my own script, look for the collision, then assign the velocity based on the collision point and the motion controller's velocity? Or, each frame, should I read the motion controller's velocity (can I even do that?) and then assign it to the paddle's rigidbody? (that sounds wrong).

    Huh.
     
  2. xorpheous

    xorpheous

    Joined:
    Mar 28, 2018
    Posts:
    19
    So basically, you want a straight reflection off of the paddle surface, not a two-body physics collision, right? If that's the case, you can flip the velocity component of the ball parallel to the normal vector of the paddle's face and add to that the parallel component of the paddle's velocity.

    vf=vi−(ε+1)(vi⋅n)n + (v_paddle⋅n)n

    Where vi and vf are the initial and final velocities of the ball, v_paddle is the velocity of the paddle, n is the unit normal vector for the paddle face, and ε is the coefficient of restitution for the collision. (play around with the number, but something close to 0.9 is likely what you want)

    You can use this result, vf-vi, to calculate a frictional force on the ball and determine the rotational impulse if you need that too.
     
  3. BroncoBilli

    BroncoBilli

    Joined:
    Oct 8, 2017
    Posts:
    90
    well... let's suppose I did want a two-body collision - how do you even approach a two-body collision when the paddle's position and rotation is set by the motion controller's position and rotation? I can imagine some kind of "elastic spring" concept, attaching the paddle's base to the motion controller, and when the motion controller moves, it drags the end of the spring with it, but the paddle is attached to the other end, and is a 'true" 2-body collision object. But this seems overkill, and I wouldn't know how to do it anyhow (does anybody know how? Detail it in a reply, if you can!)

    So let's assume it's a reflection collision and I bounce the ball's velocity off the paddle and add the paddle's velocity... I guess my main question still is: Why do I have to do this calculation myself? My paddle & ball in Unity both have mass & speed, and they do know when they collide. Why isn't Unity making the ball whack off the paddle and do this vector velocity sum for me? I thought the physics in Unity was fairly advanced, and I must be doing something wrong.



    So, xorpheous' answer is interesting, but doesn't explain what's wrong with Unity's built-in physics, which is my original question.
     
  4. BroncoBilli

    BroncoBilli

    Joined:
    Oct 8, 2017
    Posts:
    90
    I looked into this, and if the controller is setting the position of the paddle, or setting the position of the root bone of a bunch of rigged bones, then it messes with the rigidbody on what seems to be ALL the linked items. The rigidbodies don't know their velocities, and you certainly can't set them if the object is rigged. The solution seems to be to have a follower object for each thing you want to have a collider, and move the colliders onto the following object. Then, each frame, set the position of the following object, and calculate and set the rigidbody's velocity on the follower. Then, when the follower object hits the object, it will have the right velocity.

    Everything else i tried just doesn't work.
     
  5. BroncoBilli

    BroncoBilli

    Joined:
    Oct 8, 2017
    Posts:
    90
    ... tell you the truth, I don't really understand the "follower" trick. If somebody could explain it to me (us), here, I'd love that. Let's say you have a rigged hand, and you want each bone to have a collider/rb on it... If every frame, per bone, you were to calc the velocity and assign it to the rigidbody, and leave the position and rotation alone, then my guess is that each frame, your bone will drift away from its rig. I don't quite understand how rigidbodys work when they're on an object that is in the middle of a rig, and the bones are not IK'ed - how can the rigidbody move way from the rig when the rig knows where the bone should be? Just doesn't make sense. I think this is why the RB has to be on an object that follows the parent. I can't imagine how else this would be done.

    comments?