Skip to content

Commit 8f5acdd

Browse files
committed
Fixed Collision!
got the collision stuff working, check out the pond scene for some examples!
1 parent 5123742 commit 8f5acdd

File tree

14 files changed

+808
-640
lines changed

14 files changed

+808
-640
lines changed

Assets/Scenes/pond_testing.unity

Lines changed: 395 additions & 45 deletions
Large diffs are not rendered by default.

Assets/Scripts/FlexStuff/FlexCollider.cs

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44
using FlexSharp;
5+
using UnityEngine.Events;
6+
using Unity.Jobs;
7+
using Unity.Collections;
8+
using Unity.Collections.LowLevel.Unsafe;
9+
using Unity.Burst;
10+
11+
[System.Serializable]
12+
public class ParticleIdEvent : UnityEvent<int>
13+
{
14+
15+
}
516

617
public class FlexCollider : MonoBehaviour
718
{
@@ -17,6 +28,16 @@ public class FlexCollider : MonoBehaviour
1728

1829
public Mesh TriMesh;
1930

31+
public bool DetectCollision;
32+
33+
public ParticleIdEvent MethodToRunOnDetectCollision;
34+
35+
//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!
36+
37+
//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
38+
39+
public bool debug;
40+
2041
[System.NonSerialized]
2142
public uint MeshId;
2243

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

2950
int ShapeIndex;
3051

52+
public enum WhenToRun
53+
{
54+
Update,
55+
FixedUpdate
56+
}
57+
58+
bool RunMethod;
59+
60+
[System.NonSerialized]
61+
public int ParticleId; // try to avoid using, this is heavilly deprecated
62+
63+
[System.NonSerialized]
64+
public NativeList<int> ParticleIds;
65+
3166
// Start is called before the first frame update
3267
void Start()
3368
{
3469
ShapeIndex = Container.SBuf.AddShape();
3570

71+
if (DetectCollision)
72+
{
73+
Container.InbetweenQueue += BurstDealWithCollisions;
74+
}
75+
3676
Container.InbetweenQueue += DealWithShape;
3777
}
3878

3979
// Update is called once per frame
4080
void Update()
4181
{
42-
82+
if (RunMethod)
83+
{
84+
//MethodToRunOnDetectCollision?.Invoke(); deprecated...
85+
RunMethod = false;
86+
}
4387
}
4488

4589
void DealWithShape()
@@ -126,4 +170,139 @@ void DealWithShape()
126170
}
127171
}
128172
}
173+
174+
unsafe void DealWithCollisions()
175+
{
176+
if (debug)
177+
{
178+
Debug.Log("run");
179+
}
180+
181+
for (int i = 0; i < Container.SlotsUsed; i++)
182+
{
183+
int ContactIndex = Container.SBuf.ContactIndices.data[i];
184+
uint Count = Container.SBuf.ContactCounts.data[ContactIndex];
185+
186+
if (debug)
187+
{
188+
Debug.Log(ContactIndex);
189+
Debug.Log(Count);
190+
}
191+
192+
for (uint c = 0; c < Count; c++)
193+
{
194+
Vector4 Velocity = Container.SBuf.ContactVelocities.data[ContactIndex * 6 + c];
195+
196+
int ContactShapeId = (int)Velocity.w;
197+
198+
if (ContactShapeId == ShapeIndex)
199+
{
200+
ParticleId = i;
201+
202+
if (debug)
203+
{
204+
Debug.Log("Collision!");
205+
}
206+
207+
//if (WhenToRunMethodOnCollision == WhenToRun.FixedUpdate)
208+
//{
209+
//MethodToRunOnDetectCollision?.Invoke(); deprecate
210+
//}
211+
//else
212+
//{
213+
//RunMethod = true;
214+
//}
215+
}
216+
else if (debug)
217+
{
218+
Debug.Log(ContactShapeId);
219+
}
220+
}
221+
}
222+
}
223+
224+
[BurstCompile]
225+
public unsafe struct CollisionsJob : IJob
226+
{
227+
//public FlexContainer FContainer;
228+
public int _SlotsUsed;
229+
[NativeDisableUnsafePtrRestriction]
230+
public int* ContactIndicesData;
231+
[NativeDisableUnsafePtrRestriction]
232+
public uint* ContactCountsData;
233+
[NativeDisableUnsafePtrRestriction]
234+
public Vector4* ContactVelocitiesData;
235+
public int _ShapeIndex;
236+
public NativeList<int> _ParticleIds;
237+
//public WhenToRun _WhenToRunMethodOnCollision;
238+
//public UnityEvent _MethodToRunOnDetectCollision;
239+
//public NativeArray<bool> _RunMethod;
240+
241+
public unsafe void Execute()
242+
{
243+
for (int i = 0; i < _SlotsUsed; i++)
244+
{
245+
int ContactIndex = ContactIndicesData[i];
246+
uint Count = ContactCountsData[ContactIndex];
247+
248+
for (uint c = 0; c < Count; c++)
249+
{
250+
Vector4 Velocity = ContactVelocitiesData[ContactIndex * 6 + c];
251+
252+
int ContactShapeId = (int)Velocity.w;
253+
254+
if (ContactShapeId == _ShapeIndex)
255+
{
256+
_ParticleIds.Add(i);
257+
258+
//Debug.Log("Collision");
259+
260+
//if (_WhenToRunMethodOnCollision == WhenToRun.FixedUpdate)
261+
//{
262+
// _MethodToRunOnDetectCollision?.Invoke();
263+
//}
264+
//else
265+
//{
266+
// _RunMethod[0] = true;
267+
//}
268+
// icky, dont like, so goodbye you garbage code, guess you will have to run fixed update, oh well
269+
}
270+
}
271+
}
272+
}
273+
}
274+
275+
//[BurstCompile]
276+
public unsafe void BurstDealWithCollisions()
277+
{
278+
ParticleIds = new NativeList<int>(100, Allocator.TempJob);
279+
280+
CollisionsJob CollisionsJobData = new CollisionsJob();
281+
282+
//CollisionsJobData.FContainer = Container;
283+
CollisionsJobData.ContactIndicesData = Container.SBuf.ContactIndices.data;
284+
CollisionsJobData.ContactCountsData = Container.SBuf.ContactCounts.data;
285+
CollisionsJobData.ContactVelocitiesData = Container.SBuf.ContactVelocities.data;
286+
CollisionsJobData._ShapeIndex = ShapeIndex;
287+
CollisionsJobData._ParticleIds = ParticleIds;
288+
CollisionsJobData._SlotsUsed = Container.SlotsUsed;
289+
//CollisionsJobData._MethodToRunOnDetectCollision = MethodToRunOnDetectCollision;
290+
291+
JobHandle handle = CollisionsJobData.Schedule();
292+
handle.Complete();
293+
294+
foreach (int PI in ParticleIds)
295+
{
296+
MethodToRunOnDetectCollision?.Invoke(PI);
297+
}
298+
299+
if (debug)
300+
{
301+
Debug.Log(ParticleIds.Length);
302+
}
303+
304+
ParticleIds.Dispose();
305+
306+
307+
}
129308
}

Assets/Scripts/FlexStuff/FlexContainer.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ public struct ShapeBuffers
195195
public FVector<XQuat<float>> Rotations;
196196
public FVector<int> Flags;
197197

198+
public FVector<Vector4> ContactPlanes;
199+
public FVector<Vector4> ContactVelocities;
200+
public FVector<int> ContactIndices;
201+
public FVector<uint> ContactCounts;
202+
198203
public bool ShapesChanged;
199204
public int NumShapes;
200205

@@ -204,6 +209,11 @@ public void InitVectors()
204209
Positions.InitVec();
205210
Rotations.InitVec();
206211
Flags.InitVec();
212+
213+
ContactPlanes.InitVec();
214+
ContactVelocities.InitVec();
215+
ContactIndices.InitVec();
216+
ContactCounts.InitVec();
207217
}
208218

209219
public void MapVectors()
@@ -212,6 +222,11 @@ public void MapVectors()
212222
Positions.MapVec();
213223
Rotations.MapVec();
214224
Flags.MapVec();
225+
226+
ContactPlanes.MapVec();
227+
ContactVelocities.MapVec();
228+
ContactIndices.MapVec();
229+
ContactCounts.MapVec();
215230
}
216231

217232
public void UnmapVectors()
@@ -220,6 +235,11 @@ public void UnmapVectors()
220235
Positions.UnmapVec();
221236
Rotations.UnmapVec();
222237
Flags.UnmapVec();
238+
239+
ContactPlanes.UnmapVec();
240+
ContactVelocities.UnmapVec();
241+
ContactIndices.UnmapVec();
242+
ContactCounts.UnmapVec();
223243
}
224244

225245
unsafe public void SendBuffers()
@@ -233,7 +253,8 @@ unsafe public void SendBuffers()
233253

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

239260
public void DestroyVectors()
@@ -242,6 +263,11 @@ public void DestroyVectors()
242263
Positions.Destroy();
243264
Rotations.Destroy();
244265
Flags.Destroy();
266+
267+
ContactPlanes.Destroy();
268+
ContactVelocities.Destroy();
269+
ContactIndices.Destroy();
270+
ContactCounts.Destroy();
245271
}
246272

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

330+
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
331+
SBuf.ContactVelocities = new FVector<Vector4>(Library, MaxParticles * 6);
332+
SBuf.ContactIndices = new FVector<int>(Library, MaxParticles);
333+
SBuf.ContactCounts = new FVector<uint>(Library, MaxParticles);
334+
304335
SBuf.InitVectors();
305336
}
306337

@@ -333,6 +364,7 @@ void FixedUpdate()
333364
AfterSolverTickQueue?.Invoke();
334365

335366
PBuf.GetBuffers();
367+
SBuf.GetBuffers();
336368
}
337369
}
338370

Assets/Scripts/FlexStuff/FlexTrigger.cs renamed to Assets/Scripts/FlexStuff/NothingYet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using UnityEngine;
44
using UnityEditor;
55

6-
public class FlexTrigger : MonoBehaviour
6+
public class NothingYet : MonoBehaviour
77
{
88
public Vector3 Size;
99
public FlexContainer Container;
@@ -26,12 +26,12 @@ private void OnDrawGizmosSelected()
2626
}
2727
}
2828

29-
[CustomEditor(typeof(FlexTrigger))]
29+
[CustomEditor(typeof(NothingYet))]
3030
public class FlexTriggerEditor : Editor
3131
{
3232
public void OnSceneGUI()
3333
{
34-
var LinkedObject = target as FlexTrigger;
34+
var LinkedObject = target as NothingYet;
3535

3636
Handles.color = Color.blue;
3737
LinkedObject.Size = Handles.ScaleHandle(LinkedObject.Size, LinkedObject.transform.position, LinkedObject.transform.rotation, HandleUtility.GetHandleSize(LinkedObject.transform.position)*1.5f);

0 commit comments

Comments
 (0)