1
\$\begingroup\$

So I've been following along Sebastian Lague's 2D platformer tutorial, and to understand exactly how the physics of how a jump is done, I dug into the subject and noticed a lot of people, from GafferonGames and Kyle Pittman, all mention using Semi-Implicit Euler in making an accurate jump.

Now my problem is: How do you exactly implement this type of integration scheme in a game?

Specifically a platformer. A comment on the tutorial I was following said that the code below uses the Explicit Euler type of integration. Though I'm not exactly sure myself on whether or not this is true; another tutorial I found on the topic, specifically Jorge Rodriguez's Math for Game Developers uses the Semi-Implicit Euler integration to make a spaceship simulator, but the code used was C++ and I only have some limited C# and I was not able to fully understand parts of the code that went into implementing the integration.

Specifically:

          auto v = [vn, gravity](Vector x, float h) -> Vector
            {
                return vn + gravity(x) * h;
            };
            auto a = [xn, gravity](Vector v, float h) -> Vector
            {
                return gravity(xn) + gravity(xn + v*h) * h;
            };

from what I understand, this function returns a Vector2, but I don't know if the auto = a [xn, gravity] is supposed to be about declaring a variable or what, or how the -> vector bit fits in the whole code.

All in all, I need help, I'm still pretty new to programming and general and these small nuances confuse me. Any help is greatly appreciated, Thank you.

This is the code for the Platformer Character controller I followed from Sebastian Lague.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof(EntityController2D))]
public class PlayerController : MonoBehaviour
{
    EntityController2D playerController;

    #region Y-Velocity Fields
    public float jumpDisplacementDeltaTime = 0.75f;
    public float jumpHeight = 6.0f;

    float gravity;
    float jumpVelocity;

    #endregion

    float moveSpeed = 6.0f;

    Vector2 velocity;

    public void Start()
    {
        playerController = GetComponent<EntityController2D>();

        gravity = -(2 * jumpHeight) / Mathf.Pow(jumpDisplacementDeltaTime, 2);       
        jumpVelocity = Mathf.Abs(gravity) * jumpDisplacementDeltaTime;

        print ($"Gravity: {gravity}");
    }

    // Update is called once per frame

    public void Update()
    {
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); 

        if (playerController.collisions.below || playerController.collisions.above)
            {
            velocity.y = 0;
            }

        if (Input.GetKeyDown(KeyCode.Space) && playerController.collisions.below)
            {
            velocity.y = jumpVelocity;
            }

        velocity.x = input.x * moveSpeed;
        velocity.y += gravity * Time.deltaTime;
        playerController.Move(velocity * Time.deltaTime);
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You probably figured it out by now, but I'll answer since no-one else has. Basically, it's a lambda function declaration called like:

 v(aVec, aFloat); 

The -> arrow part is a late return type, (just pretend what comes after gets put where the auto is).
The [] brackets say what variables the function needs to know about, so it can use them. yea...should be right.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .