Ball Mechanics - Is this the best approach?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:20:11

问题


Good day,

I'd like to program a constantly moving ball (object3) being passed between two stationary objects (object1, object2), with the ability to set the max height Y of the pass trajectory dynamically.

What would you argue is the best way to program the ball physics for this concept?

I've looked at using addForce on a default sphere w/ a rigidbody. It seems like there should be an equation that expresses the trajectory of a pass of object3 from object1's x to object2's x... at a known, given speed, with a known, set mass, and a known gravity environment.

However, currently I have a Vector3.Lerp interpolating the ball between the two objects on each FixedUpdate() with t expressed as:

`(Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;`

It works and all, but with this approach, it seems there's no clear way to add height to the trajectory of the ball path. I've considered adding the height to the Y value in object2 until the ball is half way there, and then setting it back to the original Y position... but it just feels wrong! Thoughts?

Thanks!


回答1:


Okey so if I understand you correctly currently you are doing

privte void FixedUpdate()
{
    var factor = (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;
    object1.MovePosition(Vector3.Lerp(object2.position, object3.position, factor));
}

which moves the ball pingpong between object1 and object2 position but only planar.

Assuming for now the objects will only be moving within the XZ plane and never have different Y position in order to rather get a curve with height you could treat the separatly: - Interpolate between both positions as before - Separately calculate the Y position with sinus or any other mathematical curve function - for realistic physics probably rather a parabola actually

Could look somhow like

public class Example : MonoBehaviour
{
    public Rigidbody object1;
    public Transform object2;
    public Transform object3;

    // adjust in the Inspector
    public float speed = 1;
    public float Amplitude = 0;

    // Just for debug
    [Range(0, 1)] [SerializeField] private float linearFactor;
    [SerializeField] private float yPosition;

    private void FixedUpdate()
    {
        // This always returns a value between 0 and 1 
        // and linearly pingpongs forth and back
        linearFactor = Mathf.PingPong(Time.time * speed, 1);
        // * Mathf.PI => gives now a value 0 - PI
        // so sinus returns correctly 0 - 1 (no need for +1 and /2 anymore)
        // then simply multiply by the desired amplitude
        var sinus = Mathf.Sin(linearFactor * Mathf.PI);
        yPosition = sinus * Amplitude;

        // As before interpolate between the positions
        // later we will ignore/replace the Y component
        var position = Vector3.Lerp(object2.position, object3.position, linearFactor);

        object1.MovePosition(new Vector3(position.x, yPosition, position.z));
    }
}


You could optionally also try and add some dumping in the Y direction in order to make the vertical movement more realistic (slow down when reaching the peak). I tried a bit using inverted SmoothStep like

// just for debug
[Range(0, 1)] [SerializeField] private float dampedSinusFactor;
[Range(0, 1)] [SerializeField] private float linearFactor;
[SerializeField] private float yPosition;

private void FixedUpdate()
{
    // Use two different factros:
    // - a linear one for movement in XZ
    // - a smoothed one for movement in Y (in order to slow down when reaching the peak ;) )
    linearFactor = Mathf.PingPong(Time.time * speed, 1);
    dampedSinusFactor = InvertSmoothStep(linearFactor);

    // * Mathf.PI => gives now a value 0 - PI
    // so sinus returns correctly 0 - 1 ()
    // then simply multiply by the desired amplitude
    var sinus = Mathf.Sin(dampedSinusFactor * Mathf.PI);
    yPosition = sinus * Amplitude;

    // later we will ignore/replace the Y component
    var position = Vector3.Lerp(object2.position, object3.position, linearFactor);

    object1.position = new Vector3(position.x, yPosition, position.z);
}

// source: https://stackoverflow.com/a/34576808/7111561
private float InvertSmoothStep(float x)
{
    return x + (x - (x * x * (3.0f - 2.0f * x)));
}

However for slow movements this looks a bit strange yet. But you can come up with any other maths curve that results in the expected behavior for x=[0,1] ;)



来源:https://stackoverflow.com/questions/58073597/ball-mechanics-is-this-the-best-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!