Search Unity

3D Ball physics in a top down 2D world

Discussion in 'Physics' started by hjenkinz, Feb 14, 2020.

  1. hjenkinz

    hjenkinz

    Joined:
    Sep 11, 2012
    Posts:
    5
    Hey all. This is a maths, physics and 2D question, probably a very basic one for those who know.

    I'm creating a top down golf game, in the vain of Mario Golf Advance Tour:

    I have a Vector2 called ballDirection which determines the direction the ball is going in. Thanks to a mathematician friend of a friend, who wrote the code for a bouncing ball in Matlab, I've got the trajectory code, not that I understand it. Now I need to apply this to a top down world. The direction works fine, but the speed of the ball travelling is not working as I thought it might.

    I thought that I would take it's 'UH' var and multiply that by the direction vector to get the distance and position each update, and then use the height of the arc to change the size of the sprite to simulate an arc. From my research online, I think I found out something about squaring some numbers, multiplying them and then finding the square root of that sum, but I'm not sure if I'm even looking at the right branch of mathematics.

    Could anyone take a look at this, or point me in the right direction for the Maths I need to learn? It'd be much appreciated.

    Code (CSharp):
    1. public class Golfball : ButtonActivated
    2. {
    3.  
    4.     public Transform golfingPosition;
    5.     public GameObject arrow;
    6.  
    7.     [Header("Ball Physics")]
    8.     [SerializeField]
    9.     bool ballMoving;
    10.  
    11.     [SerializeField]
    12.     float initialSpeed;
    13.     [SerializeField]
    14.     float theta; //initial angle
    15.     [SerializeField]
    16.     float groundCoefficient; //the amount of force carried through onto subsequent bounces
    17.     [SerializeField]
    18.     float friction; //the friction of the surface the ball is colliding with
    19.  
    20.     float gravity = -9.8f; //initial angle
    21.  
    22.     [SerializeField]
    23.     float sh = 0; //initial distance
    24.     [SerializeField]
    25.     float sv = 0; //initial height
    26.  
    27.     [SerializeField]
    28.     float uh = 0; //amount of speed going forward
    29.     float uv = 0; //amount of speed going upwards
    30.  
    31.     Vector2 ballDirection;
    32.  
    33.  
    34.     public void HitBall()
    35.     {
    36.         sh = 0;
    37.         sv = 0;
    38.  
    39.         uh = Mathf.Pow(Mathf.Cos(theta), 2) * initialSpeed;
    40.         uv = Mathf.Pow(Mathf.Sin(theta), 2) * initialSpeed;
    41.  
    42.         //the vector we're sending the ball on is the arrow.transform.rotation vector!
    43.         ballDirection = arrow.transform.rotation * Vector2.right;      
    44.         ballMoving = true;
    45.     }
    46.  
    47.     protected virtual void Update()
    48.     {
    49.         if (ballMoving)
    50.         {
    51.             VelocityCalculations();
    52.         }
    53.     }
    54.  
    55.     private void VelocityCalculations()
    56.     {
    57.         sh = sh + (uh * (Time.deltaTime * travellingSpeed));
    58.         sv = sv + (uv * (Time.deltaTime * travellingSpeed));
    59.  
    60.         uv = uv + (gravity * (Time.deltaTime * travellingSpeed));
    61.  
    62.         //handle bouncing!
    63.         if (sv <= 0)
    64.         {
    65.             ballMoving = false;
    66.            
    67.             /* commented out for now
    68.  
    69.             print("the ball travelled " + sh + "before bouncing");
    70.             sv = 0;
    71.             uv = Mathf.Abs(uv) * groundCoefficient;
    72.             uh = uh * friction;
    73.             */
    74.         }
    75.  
    76.         MoveBall();
    77.     }
    78.  
    79.     private void MoveBall()
    80.     {
    81.         //my attempt to calculate the distance by direction of the ball...
    82.  
    83.         transform.position = new Vector2(transform.position.x + (sh * ballDirection.x), transform.position.y + (sh * ballDirection.y));
    84.        
    85.         //will do calculations to change size of sprite here
    86.     }
    87. }