0
\$\begingroup\$

I am trying to create a method that will plot the path a rigidbody will take when simulating a magnus type effect with no gravity in Unity. I realize this approach is a drastic simplification to the magnus force but the result is working for my application. I am applying a force to the rigidbody in FixedUpdate but keeping the velocity magnitude constant. In the picture below the actual plot of the rigid body is the yellow line. The green line is currently what the is produced by the Plot method. Actual vs Simulated plot What do I need to add/change in the Plot method so that it will plot the exact same path the rigidbody will take? Thanks!

Plot without normalizing movementStep enter image description here

public class BallSimple : MonoBehaviour
{
    public Vector3 m_StartingDirection = new Vector3(0f, 0f, 1f);
    public float m_Speed = 5f;
    public Vector3 m_ForceDirection = new Vector3(1f, 0f, 0f);
    public float m_ForceMagnitude = 1f;
    public int m_Steps = 400;
    public Rigidbody m_Rigidbody;
    public LineRenderer m_SimulationPathLineRenderer;
    public LineRenderer m_RealPathLineRenderer;
    private int m_RealPathPoints = 0;

    void Start()
    {
        m_Rigidbody.AddForce(m_StartingDirection.normalized * m_Speed);
        m_Rigidbody.velocity = m_Rigidbody.velocity.normalized * m_Speed;
        m_SimulationPathLineRenderer.positionCount = m_Steps;
        m_RealPathLineRenderer.positionCount = m_Steps;
        Plot(transform.position, m_StartingDirection.normalized * m_Speed, m_Steps);
    }

    private void FixedUpdate()
    {
        m_Rigidbody.AddForce(m_ForceDirection.normalized * m_ForceMagnitude);
        m_Rigidbody.velocity = m_Rigidbody.velocity.normalized * m_Speed;

        if (m_RealPathPoints < m_Steps)
        {
            m_RealPathLineRenderer.SetPosition(m_RealPathPoints, transform.position);
            m_RealPathPoints++;
        }
    }

    private void Plot(Vector3 pos, Vector3 velocity, int steps)
    { 
        float timestep = Time.fixedDeltaTime / Physics.defaultSolverVelocityIterations;

        Vector3 magnusMovement = m_ForceDirection.normalized * m_ForceMagnitude * timestep * timestep;
        Vector3 movementStep = velocity.normalized * m_Speed * timestep;

        for (int i = 0; i < steps; ++i)
        {
            movementStep += magnusMovement;
            movementStep = movementStep.normalized * m_Speed * timestep;
            pos += movementStep;
            m_SimulationPathLineRenderer.SetPosition(i, pos);
        }
    }
}

\$\endgroup\$
3
  • \$\begingroup\$ I notice that you normalize velocity every frame in your actual physics tick, but do not apply this normalization in your plotting loop. Why is that? \$\endgroup\$
    – DMGregory
    Commented Apr 27, 2022 at 18:18
  • \$\begingroup\$ Just an oversite on my part. I normalized the movementStep in the plot loop and then multiplying it by m_Speed * timestep. It is a more accurate projection but still overshooting the actual path \$\endgroup\$
    – AAron_-51
    Commented Apr 27, 2022 at 19:03
  • \$\begingroup\$ Want to edit your post to show your more accurate projection? \$\endgroup\$
    – DMGregory
    Commented Apr 27, 2022 at 19:10

0

You must log in to answer this question.

Browse other questions tagged .