Coder Social home page Coder Social logo

bepuphysics-unity's Introduction

Bepuphysics-Unity

A bridge for Bepuphysics and Unity

Bepu physics source code: https://github.com/bepu/bepuphysics2

Installation

  • Require unity 2020.1a17 or higher (Previous version will crash as soon as you hit play).

Performances

Right now it's not hardware accelerated in Unity. Until they support hardware acceleration for System.Numerics.Vectors it won't run as fast as it can.

bepuphysics-unity's People

Contributors

antoinecharton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bepuphysics-unity's Issues

Try to implement BoxCast, but encounter seriously performance problem

BoxCast results are all correct, but performance is unbelievable low
comu1
400 BoxCast
Unity cost 2 ms
Bepu cost 6494 ms, and GC Alloc is 53.2 MB

comu2

bepuphysics2 source code here
GJKDistanceTester.Edge() cost 1753.64 ms, and 3.2MB GC Alloc
comu3

This looks like SIMD things.
I am not familiar with SIMD so I don't know how to modify these code to improve performance in Unity.
You looks pretty cool at these.
Can you give me some advice?
Thanks in advance!

comu4
Here's Bench MonoBehaviour, put it on PhysicsSystem gameobject and click Benchmark, it will start a Benchmark

using UnityEngine;
using System.Collections.Generic;
using BepuPhysics;
using BepuPhysics.Collidables;
using BepuPhysicsUnity;

public class Bench : MonoBehaviour
{
    public bool benchmark;

    PhysicSimulation _physicSimulation;

    List<UnityEngine.Vector3> unityRayStartPositions;
    int unityRayStartPositionsCount;
    RaycastHit unityRaycastHit;
    UnityEngine.Vector3 unityHalfExtents = new UnityEngine.Vector3(1, 1, 1);

    List<System.Numerics.Vector3> bepuRayStartPositions;
    int bepuRayStartPositionsCount;
    System.Numerics.Vector3 Vector3Down = new System.Numerics.Vector3(0, -1, 0);
    System.Numerics.Vector3 bepuHalfExtents = new System.Numerics.Vector3(1, 1, 1);

    int boxCastCount = 400;
    float detectDistance = 30;

    void Start()
    {
        _physicSimulation = GetComponent<PhysicSimulation>();

        unityRayStartPositions = new List<UnityEngine.Vector3>(boxCastCount);
        bepuRayStartPositions = new List<System.Numerics.Vector3>(boxCastCount);

        int loopCount = (int)Mathf.Sqrt(boxCastCount);
        for (int i = 0; i < loopCount; i++)
        {
            for (int j = 0; j < loopCount; j++)
            {
                unityRayStartPositions.Add(new UnityEngine.Vector3(i * 0.1f, 5, j * 0.1f));
                bepuRayStartPositions.Add(new System.Numerics.Vector3(i * 0.1f, 5, j * 0.1f));
            }
        }
        unityRayStartPositionsCount = unityRayStartPositions.Count;
        bepuRayStartPositionsCount = bepuRayStartPositions.Count;

        //Add BoxCollider so Unity Physics.BoxCast can hit it
        BoxDetection[] boxDetections = FindObjectsOfType<BoxDetection>();
        for (int i = 0; i < boxDetections.Length; i++)
        {
            BoxDetection boxDetection = boxDetections[i];
            if (boxDetection.GetComponent<BoxCollider>() == null)
            {
                boxDetection.gameObject.AddComponent<BoxCollider>();
                //BoxDetection size does not match cube mesh, correct it
                boxDetection.SetSize(boxDetection.transform.localScale);
            }
        }
    }

    void Benchmark()
    {
        System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();

        st.Start();
        BenchmarkUnityBoxCast();
        st.Stop();
        UnityEngine.Debug.LogFormat("BenchmarkUnityBoxCast: {0} ms", st.ElapsedMilliseconds);
        st.Reset();

        st.Start();
        BenchmarkBepuBoxCast();
        st.Stop();
        UnityEngine.Debug.LogFormat("BenchmarkBepuBoxCast: {0} ms", st.ElapsedMilliseconds);
        st.Reset();
    }

    void BenchmarkUnityBoxCast()
    {
        for (int i = 0; i < unityRayStartPositionsCount; i++)
        {
            if (Physics.BoxCast(unityRayStartPositions[i], unityHalfExtents, UnityEngine.Vector3.down, out unityRaycastHit, UnityEngine.Quaternion.identity, detectDistance))
            {
                //UnityEngine.Debug.DrawLine(unityRayStartPositions[i], unityRaycastHit.point, Color.green);
            }
        }
    }

    void BenchmarkBepuBoxCast()
    {
        for (int i = 0; i < bepuRayStartPositionsCount; i++)
        {
            if (BepuBoxCast(bepuRayStartPositions[i], bepuHalfExtents, Vector3Down, BepuUtilities.Quaternion.Identity, detectDistance))
            {
                /*
                UnityEngine.Vector3 start = new Vector3(bepuRayStartPositions[i].X, bepuRayStartPositions[i].Y, bepuRayStartPositions[i].Z);
                UnityEngine.Vector3 end = new Vector3(_boxCastHitHandler.HitLocation.X, _boxCastHitHandler.HitLocation.Y, _boxCastHitHandler.HitLocation.Z);
                UnityEngine.Debug.DrawLine(start, end, Color.red);
                */
            }
        }
    }

    struct SceneSweepHitHandler : ISweepHitHandler
    {
        public System.Numerics.Vector3 HitLocation;
        public System.Numerics.Vector3 HitNormal;
        public float T;

        public bool AllowTest(CollidableReference collidable)
        {
            return true;
        }

        public bool AllowTest(CollidableReference collidable, int child)
        {
            return true;
        }

        public void OnHit(ref float maximumT, float t, in System.Numerics.Vector3 hitLocation, in System.Numerics.Vector3 hitNormal, CollidableReference collidable)
        {
            //Changing the maximum T value prevents the traversal from visiting any leaf nodes more distant than that later in the traversal.
            //It is effectively an optimization that you can use if you only care about the time of first impact.
            if (t < maximumT)
                maximumT = t;
            if (t < T)
            {
                T = t;
                HitLocation = hitLocation;
                HitNormal = hitNormal;
            }
        }

        public void OnHitAtZeroT(ref float maximumT, CollidableReference collidable)
        {
            maximumT = 0;
            T = 0;
            HitLocation = new System.Numerics.Vector3();
            HitNormal = new System.Numerics.Vector3();
        }
    }

    SceneSweepHitHandler _boxCastHitHandler = new SceneSweepHitHandler();
    Box _boxShape = new Box(1, 1, 1);
    RigidPose _boxRigidPose = new RigidPose();
    BodyVelocity _boxBodyVelocity = new BodyVelocity();
    unsafe bool BepuBoxCast(System.Numerics.Vector3 origin, System.Numerics.Vector3 halfExtents, System.Numerics.Vector3 direction, BepuUtilities.Quaternion orientation, float maxDistance)
    {
        _boxCastHitHandler.T = float.MaxValue;

        _boxShape.HalfWidth = halfExtents.X;
        _boxShape.HalfHeight = halfExtents.Y;
        _boxShape.HalfLength = halfExtents.Z;

        _boxRigidPose.Position = origin;
        _boxRigidPose.Orientation = orientation;

        _boxBodyVelocity.Linear = direction;

        _physicSimulation.Simulation.Sweep(_boxShape, _boxRigidPose, _boxBodyVelocity, maxDistance, _physicSimulation.BufferPool, ref _boxCastHitHandler);

        if (_boxCastHitHandler.T < float.MaxValue && _boxCastHitHandler.T > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    void Update()
    {
        if (benchmark)
        {
            Benchmark();
            benchmark = false;
            //UnityEngine.Debug.Break();
        }
    }
}

il2cpp crash with null pointer dereference

i build a apk (arm64) and run in android device, it carsh with il2cpp crash with null pointer dereference, is this not support il2cpp? i have cancel the "strip Engine Code". have you ever build il2cpp and run in phone? thanks

Lastest version.

Hi,

Thank you for making an example available to Unity.
Do you have any intention of upgrading to the latest version of Bepu?

Regards.

BEPUPhysics vs Unity's new Havok in terms of performance

Hello :) I'm really interested in BEPUPhysics and this project :)
What comments do you have in regards to BEPUPhysics and the DOTS/ HAVOK Physics that Unity is working on and rn available to use?
I'm starting to work on a new game, and seeing the performance of Bepu really got me hyped when I saw its video yesterday.
Please let me know your thoughts in comparision to Bepu vs the new physics engine Unity is currently developing.. I really have to choose one for those massive physics requirements for my Android game :)
Kind regards.

Mesh Collider

Hello - I have this running in the current 2020 beta and it looks great. Any chance you could implement a mesh collider for models? Or give me some hints on how I would do that myself? I have not worked with Bepu before so I'm a bit lost. Oh Also a terrain collider would be good too. :)

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.