Skip to content

Commit

Permalink
Fixed Collision!
Browse files Browse the repository at this point in the history
got the collision stuff working, check out the pond scene for some examples!
  • Loading branch information
coolcatcoder committed Oct 2, 2022
1 parent 5123742 commit 8f5acdd
Show file tree
Hide file tree
Showing 14 changed files with 808 additions and 640 deletions.
440 changes: 395 additions & 45 deletions Assets/Scenes/pond_testing.unity

Large diffs are not rendered by default.

181 changes: 180 additions & 1 deletion Assets/Scripts/FlexStuff/FlexCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
using System.Collections.Generic;
using UnityEngine;
using FlexSharp;
using UnityEngine.Events;
using Unity.Jobs;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Burst;

[System.Serializable]
public class ParticleIdEvent : UnityEvent<int>
{

}

public class FlexCollider : MonoBehaviour
{
Expand All @@ -17,6 +28,16 @@ public class FlexCollider : MonoBehaviour

public Mesh TriMesh;

public bool DetectCollision;

public ParticleIdEvent MethodToRunOnDetectCollision;

//public WhenToRun WhenToRunMethodOnCollision; //I hate this name, please someone figure out a better naming scheme for everything in this file just so that this variable can have a better name, please!

//bool Burst = true; //force on, because burst is better, and trying to have support for both burst and non burst is getting tiresome, feel free to add your own support for non burst if you need to

public bool debug;

[System.NonSerialized]
public uint MeshId;

Expand All @@ -28,18 +49,41 @@ public class FlexCollider : MonoBehaviour

int ShapeIndex;

public enum WhenToRun
{
Update,
FixedUpdate
}

bool RunMethod;

[System.NonSerialized]
public int ParticleId; // try to avoid using, this is heavilly deprecated

[System.NonSerialized]
public NativeList<int> ParticleIds;

// Start is called before the first frame update
void Start()
{
ShapeIndex = Container.SBuf.AddShape();

if (DetectCollision)
{
Container.InbetweenQueue += BurstDealWithCollisions;
}

Container.InbetweenQueue += DealWithShape;
}

// Update is called once per frame
void Update()
{

if (RunMethod)
{
//MethodToRunOnDetectCollision?.Invoke(); deprecated...
RunMethod = false;
}
}

void DealWithShape()
Expand Down Expand Up @@ -126,4 +170,139 @@ void DealWithShape()
}
}
}

unsafe void DealWithCollisions()
{
if (debug)
{
Debug.Log("run");
}

for (int i = 0; i < Container.SlotsUsed; i++)
{
int ContactIndex = Container.SBuf.ContactIndices.data[i];
uint Count = Container.SBuf.ContactCounts.data[ContactIndex];

if (debug)
{
Debug.Log(ContactIndex);
Debug.Log(Count);
}

for (uint c = 0; c < Count; c++)
{
Vector4 Velocity = Container.SBuf.ContactVelocities.data[ContactIndex * 6 + c];

int ContactShapeId = (int)Velocity.w;

if (ContactShapeId == ShapeIndex)
{
ParticleId = i;

if (debug)
{
Debug.Log("Collision!");
}

//if (WhenToRunMethodOnCollision == WhenToRun.FixedUpdate)
//{
//MethodToRunOnDetectCollision?.Invoke(); deprecate
//}
//else
//{
//RunMethod = true;
//}
}
else if (debug)
{
Debug.Log(ContactShapeId);
}
}
}
}

[BurstCompile]
public unsafe struct CollisionsJob : IJob
{
//public FlexContainer FContainer;
public int _SlotsUsed;
[NativeDisableUnsafePtrRestriction]
public int* ContactIndicesData;
[NativeDisableUnsafePtrRestriction]
public uint* ContactCountsData;
[NativeDisableUnsafePtrRestriction]
public Vector4* ContactVelocitiesData;
public int _ShapeIndex;
public NativeList<int> _ParticleIds;
//public WhenToRun _WhenToRunMethodOnCollision;
//public UnityEvent _MethodToRunOnDetectCollision;
//public NativeArray<bool> _RunMethod;

public unsafe void Execute()
{
for (int i = 0; i < _SlotsUsed; i++)
{
int ContactIndex = ContactIndicesData[i];
uint Count = ContactCountsData[ContactIndex];

for (uint c = 0; c < Count; c++)
{
Vector4 Velocity = ContactVelocitiesData[ContactIndex * 6 + c];

int ContactShapeId = (int)Velocity.w;

if (ContactShapeId == _ShapeIndex)
{
_ParticleIds.Add(i);

//Debug.Log("Collision");

//if (_WhenToRunMethodOnCollision == WhenToRun.FixedUpdate)
//{
// _MethodToRunOnDetectCollision?.Invoke();
//}
//else
//{
// _RunMethod[0] = true;
//}
// icky, dont like, so goodbye you garbage code, guess you will have to run fixed update, oh well
}
}
}
}
}

//[BurstCompile]
public unsafe void BurstDealWithCollisions()
{
ParticleIds = new NativeList<int>(100, Allocator.TempJob);

CollisionsJob CollisionsJobData = new CollisionsJob();

//CollisionsJobData.FContainer = Container;
CollisionsJobData.ContactIndicesData = Container.SBuf.ContactIndices.data;
CollisionsJobData.ContactCountsData = Container.SBuf.ContactCounts.data;
CollisionsJobData.ContactVelocitiesData = Container.SBuf.ContactVelocities.data;
CollisionsJobData._ShapeIndex = ShapeIndex;
CollisionsJobData._ParticleIds = ParticleIds;
CollisionsJobData._SlotsUsed = Container.SlotsUsed;
//CollisionsJobData._MethodToRunOnDetectCollision = MethodToRunOnDetectCollision;

JobHandle handle = CollisionsJobData.Schedule();
handle.Complete();

foreach (int PI in ParticleIds)
{
MethodToRunOnDetectCollision?.Invoke(PI);
}

if (debug)
{
Debug.Log(ParticleIds.Length);
}

ParticleIds.Dispose();


}
}
34 changes: 33 additions & 1 deletion Assets/Scripts/FlexStuff/FlexContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public struct ShapeBuffers
public FVector<XQuat<float>> Rotations;
public FVector<int> Flags;

public FVector<Vector4> ContactPlanes;
public FVector<Vector4> ContactVelocities;
public FVector<int> ContactIndices;
public FVector<uint> ContactCounts;

public bool ShapesChanged;
public int NumShapes;

Expand All @@ -204,6 +209,11 @@ public void InitVectors()
Positions.InitVec();
Rotations.InitVec();
Flags.InitVec();

ContactPlanes.InitVec();
ContactVelocities.InitVec();
ContactIndices.InitVec();
ContactCounts.InitVec();
}

public void MapVectors()
Expand All @@ -212,6 +222,11 @@ public void MapVectors()
Positions.MapVec();
Rotations.MapVec();
Flags.MapVec();

ContactPlanes.MapVec();
ContactVelocities.MapVec();
ContactIndices.MapVec();
ContactCounts.MapVec();
}

public void UnmapVectors()
Expand All @@ -220,6 +235,11 @@ public void UnmapVectors()
Positions.UnmapVec();
Rotations.UnmapVec();
Flags.UnmapVec();

ContactPlanes.UnmapVec();
ContactVelocities.UnmapVec();
ContactIndices.UnmapVec();
ContactCounts.UnmapVec();
}

unsafe public void SendBuffers()
Expand All @@ -233,7 +253,8 @@ unsafe public void SendBuffers()

unsafe public void GetBuffers()
{
Debug.Log("?????? YOU CANT GET SHAPES, THIS FUNCTION SHOULD NEVER RUN!!!!!!!!!!!!!!!!!!!");
//Debug.Log("?????? YOU CANT GET SHAPES, THIS FUNCTION SHOULD NEVER RUN!!!!!!!!!!!!!!!!!!!"); Removed cause you can get contacts, so GetBuffers has a purpose now...
Methods.NvFlexGetContacts(Solver, ContactPlanes.buffer, ContactVelocities.buffer, ContactIndices.buffer, ContactCounts.buffer);
}

public void DestroyVectors()
Expand All @@ -242,6 +263,11 @@ public void DestroyVectors()
Positions.Destroy();
Rotations.Destroy();
Flags.Destroy();

ContactPlanes.Destroy();
ContactVelocities.Destroy();
ContactIndices.Destroy();
ContactCounts.Destroy();
}

public int AddShape()
Expand Down Expand Up @@ -301,6 +327,11 @@ void FixedUpdate()
SBuf.Rotations = new FVector<XQuat<float>>(Library, SBuf.NumShapes);
SBuf.Flags = new FVector<int>(Library, SBuf.NumShapes);

SBuf.ContactPlanes = new FVector<Vector4>(Library, MaxParticles * 6); // 6 appears to be the max amount of contacts a particle is allowed to have according to triggervolume.h in the demo app
SBuf.ContactVelocities = new FVector<Vector4>(Library, MaxParticles * 6);
SBuf.ContactIndices = new FVector<int>(Library, MaxParticles);
SBuf.ContactCounts = new FVector<uint>(Library, MaxParticles);

SBuf.InitVectors();
}

Expand Down Expand Up @@ -333,6 +364,7 @@ void FixedUpdate()
AfterSolverTickQueue?.Invoke();

PBuf.GetBuffers();
SBuf.GetBuffers();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;
using UnityEditor;

public class FlexTrigger : MonoBehaviour
public class NothingYet : MonoBehaviour
{
public Vector3 Size;
public FlexContainer Container;
Expand All @@ -26,12 +26,12 @@ private void OnDrawGizmosSelected()
}
}

[CustomEditor(typeof(FlexTrigger))]
[CustomEditor(typeof(NothingYet))]
public class FlexTriggerEditor : Editor
{
public void OnSceneGUI()
{
var LinkedObject = target as FlexTrigger;
var LinkedObject = target as NothingYet;

Handles.color = Color.blue;
LinkedObject.Size = Handles.ScaleHandle(LinkedObject.Size, LinkedObject.transform.position, LinkedObject.transform.rotation, HandleUtility.GetHandleSize(LinkedObject.transform.position)*1.5f);
Expand Down
File renamed without changes.
Loading

0 comments on commit 8f5acdd

Please sign in to comment.