Search Unity

Find Angle and Speed

Discussion in 'Scripting' started by gregmax17, Sep 17, 2012.

  1. gregmax17

    gregmax17

    Joined:
    Jan 23, 2011
    Posts:
    186
    I am trying to find out the end speed and angle result of a sphere I have colliding with a swinging rectangle (or the pinballs flipper in this case). I attached an image to better help what I am trying to figure out right here.

    So far I have my ball doing a raycast towards the flipper. If the ball detects the flipper within a certain distance, I need it to act accordingly (the ball moving at the appropriate angle and speed, etc).

    I did try having the colliders and rigidbodies do the work, but every once in a while the ball will move too fast and zoom right by the flipper. Also, if the flipper is swinging late, the ball just "dies"... as in stops moving... I don't know why.

    I appreciate any help and advance on this, thanks!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    By what standard do you want it to react?

    Do you expect it to react by newtonian physics? Well you'll need a lot more information, like friction and mass.

    Or you probably don't care about that, and would rather it be near newtonian and forgoe the friction and mass and assume all objects are of equal mass.

    Or you expect to act to your own custom specifications...

    Or maybe to some standard specification (like simple vector reflection)...
     
  3. gregmax17

    gregmax17

    Joined:
    Jan 23, 2011
    Posts:
    186
    I don't want it to get too detailed into physics, because I plan on putting this game onto mobile. Also, I am not too good with the Math, so I am stuck somewhere in the numbers, I am done for :( So I would like to keep it simple, like the vector reflection you mentioned. However, the flipper would be larger in mass and the ball would be of smaller mass. So that much physics would be fine.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    See, your flip floppiness on this that and the other.

    I didn't ask those questions for my benefit, I asked them for YOUR benefit.

    I'm not here to solve your problem, I'm here to help you solve YOUR problem.

    And the first step in solving a problem is identifying the problem and outline what you expect to happen!
     
  5. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    For a pinball game, you should rely on the physics engine. To decrease the chance of colliders passing through each other, you can increase the physics Solver Iteration Count (try 20 or more steps) and reduce the Min Penetration Penalty value (usually around 0.001 is good). You will find these in the Edit -> Project Settings -> Physics menu.

    Modifying these values does reduce performance. But, if your game is a typical pinball game, then there will never be more than a few physical objects being controlled, and no system that Unity runs on will have a problem handling that small of a load.
     
  6. CrazySi

    CrazySi

    Joined:
    Jun 23, 2011
    Posts:
    538
    If it looks right it is right. This would be my approach.

    What I would do is have the ball cast a ray in fixedupdate. Lets assume, 1) the ball has hit the paddle, and 2) the ball is always casting a ray out in its forward direction vector, and storing the result in RaycastHit struct.

    I could simply measure the distance of the ray hit (in RayCastHit) to my centre if thats less or equal to my radius then contact has been made. I could also test for collisions using the OnCollisionEnter methods as well. If a collision has taken place, then I need to calculate the new force to apply to the ball.

    If I look at RayCastHit and it will tell me the normal of the collision. Invert this normal and multiply this by some force constant and apply this force to the rigidbody of the ball. Experiment using Impulse and Acceleration force modes. I suspect impulse is better.