Search Unity

Question Gravity pull not taken into account in Physics2D.simulate

Discussion in 'Physics' started by portalobbqueen, May 9, 2023.

  1. portalobbqueen

    portalobbqueen

    Joined:
    Apr 28, 2023
    Posts:
    1
    Hello! I'm new to Unity and I think I'm not getting how Physics2D.simulate works.

    I'm trying to implement a trajectory prediction in a 2D outer space game with line renderer using the physics simulation method presented here: https://austin-mackrell.medium.com/unity-trajectory-prediction-simulation-method-5b441ee1604

    I implemented as in the post: a physicscene copy of the actual scene is created, clones of the planets and the player are moved there.

    Then I run physicsScene2D.simulate method X iterations in the future to make my prediction when the player aims and the line renderer draws at each iteration the simulated player's position.

    Here is the simulated launch code:

    Code (CSharp):
    1. public void SimulateLaunch(Transform player, Vector3 force)
    2.     {
    3.  
    4.         _simulatedPlayer.transform.position = player.position;
    5.         _simulatedPlayer.transform.rotation = player.rotation;
    6.         _simulatedPlayer.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
    7.  
    8.         if(_lastforce != force)
    9.         {
    10.             _simulatedPlayer.GetComponent<Rigidbody2D>().AddForce(force);
    11.             for (int i = 0; i < _maxSimulSteps; i++)
    12.             {
    13.                 _physicsScene.Simulate(Time.fixedDeltaTime);
    14.  
    15.                 points[i] = _simulatedPlayer.transform.position;
    16.                 trajectoryLine.SetPosition(i, points[i]);
    17.             }
    18.         }
    19.         _lastforce = force;
    20.  
    21.     }


    However, I have GameObjects representing planets that exert a gravity pull on my player.
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.      
    4.  
    5.         float distance = Vector2.Distance(player.transform.position, transform.position);
    6.         if (distance < influenceRange)
    7.         {
    8.             pullForce = (transform.position - player.transform.position).normalized * intensity * fixedUpdateCompensator / distance;
    9.             player.GetComponent<Rigidbody2D>().AddForce(pullForce, ForceMode2D.Force);
    10.         }
    11.  
    12.  
    13.         float distanceToSim = Vector2.Distance(playerSimulation.transform.position, transform.position);
    14.         if (distanceToSim < influenceRange)
    15.         {
    16.             playerSimulation.GetComponent<Rigidbody2D>().AddForce(((transform.position - playerSimulation.transform.position).normalized * intensity * fixedUpdateCompensator / distanceToSim), ForceMode2D.Force);
    17.         }
    18.  
    19.  
    20.     }

    Every FixedUpdate, these planets calculate the force to exert using a simplified newton's law and addForce to the player's rigidbody.

    When aiming: the line renderer is not curved when near to a planet.
    But in my actual scene the ship is gravity pulled correctly.

    upload_2023-5-9_14-21-1.png

    I think i'm maybe missing something about the physics simulate lifecycle and callbacks handling ?
    The fixedUpdate seems to be called because if I log inside:


    Code (CSharp):
    1. if (distanceToSim < influenceRange)
    2.         {
    3. //Log something
    4.         }
    It pops in the console.

    Any help appreciated, even if it's " read this part of the documentation" :)
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Physics2D.Simulate is not a special code-path. This is what Unity will call automatically when you're not simulating. It's absolutely identical no matter who calls it.
     
    portalobbqueen likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    I think you're under the assumption that physics calls FixedUpdate, it doesn't.

    FixedUpdate calls (amongst other things) physics. Calling Physics2D.Simulate doesn't call FixedUpdate. It simulates the physics scene only, not everything else in the Unity scene associated with it.
     
    Edy and portalobbqueen like this.