Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Custom Physics Unity 2D

Discussion in 'Physics' started by game_dev23, Jul 25, 2020.

  1. game_dev23

    game_dev23

    Joined:
    Aug 11, 2018
    Posts:
    10
    Hi, I'm implementing a basic physics engine based on Euler integration, however, It is not working. I couldn't find any tutorials that are doing it the way I am either. Basically trying to move the object by calculating the net force at each FixedUpdate() and applying that force. However, I am having trouble stopping the object when there is a collision. Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEditor.Experimental.GraphView;
    5. using UnityEngine;
    6.  
    7. public class CustomPhysics : MonoBehaviour
    8. {
    9.     private Vector2 velocity;
    10.     private Vector2 Force;
    11.     private Rigidbody2D rb;
    12.     public float mass = 1;
    13.     public float gravityMod = 1f;
    14.     public Vector2 gravity = new Vector2(0, -9.8f);
    15.     public Vector2 forceGravity;
    16.     public ContactFilter2D cfilter;
    17.     public RaycastHit2D[] results = new RaycastHit2D[16];
    18.     public List<RaycastHit2D> hits = new List<RaycastHit2D>(16);
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.         cfilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
    24.         forceGravity = (gravity * gravityMod * mass);
    25.         Force = forceGravity;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void FixedUpdate()
    30.     {
    31.         velocity += (Force/mass) * Time.deltaTime;
    32.         Vector2 deltaPos = velocity * Time.deltaTime;
    33.         rb.position += deltaPos;
    34.         Force = CalculateForce(deltaPos);
    35.     }
    36.  
    37.  
    38.     public Vector2 CalculateForce(Vector2 deltaPos)
    39.     {
    40.         Vector2 newForce = Force;
    41.         int count = rb.Cast(deltaPos, cfilter, results, deltaPos.magnitude);
    42.         hits.Clear();
    43.         for (int i = 0; i < count; i++)
    44.         {
    45.             hits.Add(results[i]);
    46.         }
    47.  
    48.         for (int i = 0; i < hits.Count; i++)
    49.         {
    50.             Vector2 normal = hits[i].normal;
    51.             Debug.Log(normal);
    52.             Vector2 forceNormal = normal.normalized * forceGravity;
    53.             Vector2 forceGravityAngle = forceGravity;
    54.             if (Mathf.Abs(normal.x) > 0.01)
    55.             {
    56.                 Vector2 moveAlongGround = new Vector2(-normal.y, normal.x);
    57.                 forceGravityAngle = (forceGravity * Mathf.Sin(Vector2.Angle(forceGravity, -forceNormal)));
    58.             }
    59.             newForce = forceNormal + forceGravityAngle;
    60.         }
    61.  
    62.         Debug.Log(newForce);
    63.         return newForce;
    64.     }
    65.  
    66. }
    67.