Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My customizations for consideration - Boolean Sweeps, Fast Mesh Bounds, some small API changes #150

Draft
wants to merge 44 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2da754a
Merge pull request #1 from bepu/master
Frooxius Sep 22, 2020
5e8dae1
Merge branch 'bepu:master' into master
Frooxius Jun 4, 2021
a1048c4
Set up CI with Azure Pipelines
Frooxius Jun 9, 2021
821ecfb
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
6ab960b
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
d99023d
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
78eef12
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
5654b16
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
fd9b584
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
791ca32
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
381fd64
Update azure-pipelines.yml for Azure Pipelines
Frooxius Jun 9, 2021
32e7293
Merge branch 'bepu:master' into master
Frooxius Jun 12, 2021
3e8b25e
Merge branch 'bepu:master' into neos-release
Frooxius Jun 12, 2021
df7bfa6
Some testing for dual sided triangle raycasts (WIP)
Frooxius Jun 27, 2021
90958dd
Merge branch 'neos-release' into master
Frooxius Jun 27, 2021
4f838c7
Merge pull request #2 from Neos-Metaverse/master
Frooxius Jun 27, 2021
0b5be48
Actually use the dualsided method for test
Frooxius Jun 27, 2021
b367dbc
Merge branch 'master' of https://github.com/Neos-Metaverse/bepuphysics2
Frooxius Jun 27, 2021
296a086
Merge pull request #3 from Neos-Metaverse/master
Frooxius Jun 27, 2021
891d6ff
- Initial implementation of fast boolean checks (doesn't test every s…
Frooxius Jul 25, 2021
107289d
- Added validation for fast mesh bounds
Frooxius Aug 28, 2021
46cf626
Temporarily replacing files from upstream to resolve merge conflict
Frooxius Aug 28, 2021
e7aa09f
Merge pull request #5 from bepu/master
Frooxius Aug 28, 2021
d41c90c
Reapply changes to Mesh.cs
Frooxius Aug 28, 2021
d2b0367
Reapply more changes to Mesh.cs
Frooxius Aug 28, 2021
d3f59b3
Reapply changes to project files
Frooxius Aug 28, 2021
61dbd12
- Fix readonly properties on Tree node count
Frooxius Aug 29, 2021
8f2d72f
- Reapply changes to CustomVoxelCollidableDemo
Frooxius Aug 29, 2021
600be04
Add readonly decorators to mesh
Frooxius Aug 29, 2021
d31618d
Remove the extended version of Validate (used these only during debug…
Frooxius Aug 29, 2021
0c51b7d
Few more changes to compile as .NET Standard 2.0 again
Frooxius Aug 29, 2021
092e5aa
Merge branch 'bepu:master' into master
Frooxius Aug 29, 2021
cee7490
Merge branch 'neos-release' into master
Frooxius Aug 29, 2021
9a4a1dd
Revert "Merge branch 'neos-release' into master"
Frooxius Aug 29, 2021
6b46fc1
- Backport MathF back to Math
Frooxius Aug 29, 2021
9269bd1
Re-add ilopt
Frooxius Aug 29, 2021
1b83c20
Remove DEBUG keyword from release version
Frooxius Aug 29, 2021
0325cdb
Update version to reflect that this is a fork with modifications
Frooxius Aug 29, 2021
5b4b90a
Use actually valid version string
Frooxius Aug 29, 2021
000eec7
Merge branch 'bepu:master' into master
Frooxius Aug 30, 2021
21161c6
Merge branch 'bepu:master' into master
Frooxius Sep 8, 2021
c32ffd9
Merge branch 'bepu:master' into master
Frooxius Sep 12, 2021
76d0fd5
Lower the mesh reduction limit more because we're cheap
Frooxius Sep 12, 2021
f6800d9
Merge branch 'bepu:master' into master
Frooxius Sep 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions BepuPhysics/BepuPhysics.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>2.4.0-beta6</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.4.0-neos1</Version>
<Company>Bepu Entertainment LLC</Company>
<Authors>Ross Nordby</Authors>
<Description>Speedy real time physics simulation library.</Description>
Expand All @@ -10,29 +10,40 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/bepu/bepuphysics2</RepositoryUrl>
<PackageIcon>bepuphysicslogo256.png</PackageIcon>
<Configurations>Debug;Release;ReleaseNoProfiling</Configurations>
<Configurations>Debug;Release;ReleaseStrip;ReleaseStripNoProfiling</Configurations>
<LangVersion>latest</LangVersion>
<PackageTags>physics;3d;rigid body;real time;simulation</PackageTags>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>

<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.4.0.1</AssemblyVersion>
<FileVersion>2.4.0.1</FileVersion>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Condition="'$(Configuration)'=='ReleaseStrip' Or '$(Configuration)'=='ReleaseStripNoProfiling'" Command="dotnet &quot;$(ProjectDir)..\Dependencies\ilopt\ilopt.dll&quot; &quot;$(TargetPath)&quot;" />
</Target>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;CHECKMATH;PROFILE</DefineConstants>
<DefineConstants>TRACE;DEBUG;CHECKMATH;PROFILE;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE;RELEASE;PROFILE</DefineConstants>
<DefineConstants>TRACE;RELEASE;PROFILE;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='ReleaseStrip'">
<Optimize>true</Optimize>
<DefineConstants>TRACE;RELEASE;NETSTANDARD2_0;RELEASESTRIP;FAST_MESH_BOUNDS</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='ReleaseNoProfiling'">
<PropertyGroup Condition="'$(Configuration)'=='ReleaseStripNoProfiling'">
<Optimize>true</Optimize>
<DefineConstants>TRACE;RELEASE</DefineConstants>
</PropertyGroup>
Expand Down
10 changes: 4 additions & 6 deletions BepuPhysics/Collidables/BigCompound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,11 @@ public void TestLeaf(int leafIndex, ref float maximumT)
Unsafe.AsRef<TOverlaps>(Overlaps).Allocate(Pool) = leafIndex;
}
}
public unsafe void FindLocalOverlaps<TOverlaps>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, void* overlaps)
where TOverlaps : ICollisionTaskSubpairOverlaps

public unsafe void FindLocalOverlaps<TLeafTester>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, ref TLeafTester leafTester)
where TLeafTester : ISweepLeafTester
{
SweepLeafTester<TOverlaps> enumerator;
enumerator.Pool = pool;
enumerator.Overlaps = overlaps;
Tree.Sweep(min, max, sweep, maximumT, ref enumerator);
Tree.Sweep(min, max, sweep, maximumT, ref leafTester);
}

public void Dispose(BufferPool bufferPool)
Expand Down
8 changes: 3 additions & 5 deletions BepuPhysics/Collidables/Compound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,11 @@ public unsafe void FindLocalOverlaps<TOverlaps, TSubpairOverlaps>(ref Buffer<Ove
}
}

public unsafe void FindLocalOverlaps<TOverlaps>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, void* overlapsPointer)
where TOverlaps : ICollisionTaskSubpairOverlaps
public unsafe void FindLocalOverlaps<TLeafTester>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, ref TLeafTester leafTester)
where TLeafTester : ISweepLeafTester
{
Tree.ConvertBoxToCentroidWithExtent(min, max, out var sweepOrigin, out var expansion);
TreeRay.CreateFrom(sweepOrigin, sweep, maximumT, out var ray);
ref var overlaps = ref Unsafe.AsRef<TOverlaps>(overlapsPointer);
for (int i = 0; i < Children.Length; ++i)
{
ref var child = ref Children[i];
Expand All @@ -273,10 +272,9 @@ public unsafe void FindLocalOverlaps<TOverlaps>(in Vector3 min, in Vector3 max,
childMax = childMax + child.LocalPose.Position + expansion;
if (Tree.Intersects(childMin, childMax, &ray, out _))
{
overlaps.Allocate(pool) = i;
leafTester.TestLeaf(i, ref maximumT);
}
}

}

public void Dispose(BufferPool bufferPool)
Expand Down
8 changes: 4 additions & 4 deletions BepuPhysics/Collidables/ConvexHullHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static int FindNextIndexForFaceHull(Vector2 start, Vector2 previousEdgeDirection
best = candidateY / candidateX;
bestIndex = 0;
bestDistanceSquared = candidateX * candidateX + candidateY * candidateY;
var inverseBestDistance = 1f / MathF.Sqrt(bestDistanceSquared);
var inverseBestDistance = 1f / (float)Math.Sqrt(bestDistanceSquared);
currentEdgeDirectionX = candidateX * inverseBestDistance;
currentEdgeDirectionY = candidateY * inverseBestDistance;
}
Expand All @@ -216,7 +216,7 @@ static int FindNextIndexForFaceHull(Vector2 start, Vector2 previousEdgeDirection
//1) collinear with the previous best by the plane epsilon test BUT is more distant, or
//2) has a greater angle than the previous best.
var planeOffset = -candidateX * currentEdgeDirectionY + candidateY * currentEdgeDirectionX;
if (MathF.Abs(planeOffset) < planeEpsilon)
if (Math.Abs(planeOffset) < planeEpsilon)
{
//The candidate is collinear. Only accept it if it's further away.
if (candidateX * candidateX + candidateY * candidateY <= bestDistanceSquared)
Expand All @@ -231,7 +231,7 @@ static int FindNextIndexForFaceHull(Vector2 start, Vector2 previousEdgeDirection
}
best = candidateY / candidateX;
bestDistanceSquared = candidateX * candidateX + candidateY * candidateY;
var inverseBestDistance = 1f / MathF.Sqrt(bestDistanceSquared);
var inverseBestDistance = 1f / (float)Math.Sqrt(bestDistanceSquared);
currentEdgeDirectionX = candidateX * inverseBestDistance;
currentEdgeDirectionY = candidateY * inverseBestDistance;
bestIndex = i;
Expand Down Expand Up @@ -546,7 +546,7 @@ public static void ComputeHull(Span<Vector3> points, BufferPool pool, out HullDa
Vector3Wide.Broadcast(initialVertex, out var initialVertexBundle);
pool.Take<Vector<float>>(pointBundles.Length, out var projectedOnX);
pool.Take<Vector<float>>(pointBundles.Length, out var projectedOnY);
var planeEpsilonNarrow = MathF.Sqrt(bestDistanceSquared) * 1e-6f;
var planeEpsilonNarrow = (float)Math.Sqrt(bestDistanceSquared) * 1e-6f;
var planeEpsilon = new Vector<float>(planeEpsilonNarrow);
var rawFaceVertexIndices = new QuickList<int>(pointBundles.Length * Vector<float>.Count, pool);
var initialSourceEdge = new EdgeEndpoints { A = initialIndex, B = initialIndex };
Expand Down
58 changes: 51 additions & 7 deletions BepuPhysics/Collidables/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,39 @@ public struct Mesh : IHomogeneousCompoundShape<Triangle, TriangleWide>
public Buffer<Triangle> Triangles;
internal Vector3 scale;
internal Vector3 inverseScale;
#if FAST_MESH_BOUNDS
internal Vector3 boundsCenter;
internal Vector3 boundsHalfSize;

public Vector3 BoundsCenter
{
readonly get
{
return boundsCenter;
}
set
{
boundsCenter = value;
}
}
public Vector3 BoundsSize
{
readonly get
{
return boundsHalfSize * 2;
}
set
{
boundsHalfSize = value * 0.5f;
}
}
#endif
/// <summary>
/// Gets or sets the scale of the mesh.
/// </summary>
public Vector3 Scale
{
get
readonly get
{
return scale;
}
Expand Down Expand Up @@ -167,6 +194,25 @@ public readonly unsafe void GetLocalChild(int triangleIndex, ref TriangleWide ta
Vector3Wide.WriteFirst(source.C * scale, ref target.C);
}

#if FAST_MESH_BOUNDS
public void ComputeBounds(in Quaternion orientation, out Vector3 min, out Vector3 max)
{
Matrix3x3.CreateFromQuaternion(orientation, out var basis);
var x = scale.X * boundsHalfSize.X * basis.X;
var y = scale.Y * boundsHalfSize.Y * basis.Y;
var z = scale.Z * boundsHalfSize.Z * basis.Z;
max = Vector3.Abs(x) + Vector3.Abs(y) + Vector3.Abs(z);
min = -max;

var rotatedPose = Vector3.Transform(scale * boundsCenter, orientation);

min += rotatedPose;
max += rotatedPose;

MathChecker.Validate(min);
MathChecker.Validate(max);
}
#else
public readonly void ComputeBounds(in Quaternion orientation, out Vector3 min, out Vector3 max)
{
Matrix3x3.CreateFromQuaternion(orientation, out var r);
Expand All @@ -191,6 +237,7 @@ public readonly void ComputeBounds(in Quaternion orientation, out Vector3 min, o
max = Vector3.Max(max0, max1);
}
}
#endif

public readonly ShapeBatch CreateShapeBatch(BufferPool pool, int initialCapacity, Shapes shapeBatches)
{
Expand Down Expand Up @@ -294,17 +341,14 @@ public readonly unsafe void FindLocalOverlaps<TOverlaps, TSubpairOverlaps>(ref B
}
}

public readonly unsafe void FindLocalOverlaps<TOverlaps>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, void* overlaps)
where TOverlaps : ICollisionTaskSubpairOverlaps
public unsafe void FindLocalOverlaps<TLeafTester>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, ref TLeafTester leafTester)
where TLeafTester : ISweepLeafTester
{
var scaledMin = min * inverseScale;
var scaledMax = max * inverseScale;
var scaledSweep = sweep * inverseScale;
ShapeTreeSweepLeafTester<TOverlaps> enumerator;
enumerator.Pool = pool;
enumerator.Overlaps = overlaps;
//Take a min/max to compensate for negative scales.
Tree.Sweep(Vector3.Min(scaledMin, scaledMax), Vector3.Max(scaledMin, scaledMax), scaledSweep, maximumT, ref enumerator);
Tree.Sweep(Vector3.Min(scaledMin, scaledMax), Vector3.Max(scaledMin, scaledMax), scaledSweep, maximumT, ref leafTester);
}

public struct MeshTriangleSource : ITriangleSource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BepuPhysics.Collidables;
using BepuPhysics.Trees;
using BepuUtilities;
using BepuUtilities.Memory;
using System.Numerics;
Expand All @@ -19,8 +20,8 @@ unsafe void FindLocalOverlaps<TOverlaps, TSubpairOverlaps>(ref Buffer<OverlapQue
where TOverlaps : struct, ICollisionTaskOverlaps<TSubpairOverlaps>
where TSubpairOverlaps : struct, ICollisionTaskSubpairOverlaps;

unsafe void FindLocalOverlaps<TOverlaps>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, void* overlaps)
where TOverlaps : ICollisionTaskSubpairOverlaps;
unsafe void FindLocalOverlaps<TLeafTester>(in Vector3 min, in Vector3 max, in Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, ref TLeafTester leafTester)
where TLeafTester : ISweepLeafTester;
}
public interface IOverlapTestingOptions
{
Expand Down
2 changes: 1 addition & 1 deletion BepuPhysics/CollisionDetection/MeshReduction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public unsafe static void ReduceManifolds(ref Buffer<Triangle> continuationTrian
//The current quadratic scaling behavior of this reduction can be explosively bad as the count rises into the thousands.
//Ideally we'll do https://github.com/bepu/bepuphysics2/issues/66 so this will become a nonissue.
//Until then, attempting to reduce absurdo-manifolds is likely misguided. Better to have some bumps than a multi-second hang.
if (count > 1024)
if (count > 128)
return;
//Narrow the region of interest.
continuationTriangles.Slice(start, count, out var triangles);
Expand Down
42 changes: 42 additions & 0 deletions BepuPhysics/CollisionDetection/SweepTaskRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ protected abstract unsafe bool PreorderedTypeSweep<TSweepFilter>(
bool flipRequired, ref TSweepFilter filter, Shapes shapes, SweepTaskRegistry sweepTasks, BufferPool pool, out float t0, out float t1, out Vector3 hitLocation, out Vector3 hitNormal)
where TSweepFilter : ISweepFilter;

protected virtual unsafe bool PreorderedTypeCheck<TSweepFilter>(
void* shapeDataA, in Quaternion orientationA, in BodyVelocity velocityA,
void* shapeDataB, in Vector3 offsetB, in Quaternion orientationB, in BodyVelocity velocityB, float maximumT,
float minimumProgression, float convergenceThreshold, int maximumIterationCount,
bool flipRequired, ref TSweepFilter filter, Shapes shapes, SweepTaskRegistry sweepTasks, BufferPool pool)
where TSweepFilter : ISweepFilter
{
// Use the Sweep by default, so each sweep task doesn't have to do this in case it doesn't need to optimize the Check over Sweep
return PreorderedTypeSweep(shapeDataA, orientationA, velocityA,
shapeDataB, offsetB, orientationB, velocityB,
maximumT, minimumProgression, convergenceThreshold, maximumIterationCount,
flipRequired, ref filter, shapes, sweepTasks, pool, out _, out _, out _, out _);
}

public unsafe bool Sweep<TSweepFilter>(
void* shapeDataA, int shapeTypeA, in Quaternion orientationA, in BodyVelocity velocityA,
void* shapeDataB, int shapeTypeB, in Vector3 offsetB, in Quaternion orientationB, in BodyVelocity velocityB,
Expand Down Expand Up @@ -104,6 +118,34 @@ public unsafe bool Sweep<TSweepFilter>(
out t0, out t1, out hitLocation, out hitNormal);
}
}

public unsafe bool Check<TSweepFilter>(
void* shapeDataA, int shapeTypeA, in Quaternion orientationA, in BodyVelocity velocityA,
void* shapeDataB, int shapeTypeB, in Vector3 offsetB, in Quaternion orientationB, in BodyVelocity velocityB,
float maximumT, float minimumProgression, float convergenceThreshold, int maximumIterationCount,
ref TSweepFilter filter, Shapes shapes, SweepTaskRegistry sweepTasks, BufferPool pool)
where TSweepFilter : ISweepFilter
{
Debug.Assert((shapeTypeA == ShapeTypeIndexA && shapeTypeB == ShapeTypeIndexB) || (shapeTypeA == ShapeTypeIndexB && shapeTypeB == ShapeTypeIndexA),
"Types must match expected types.");
var flipRequired = shapeTypeB == ShapeTypeIndexA;
if (flipRequired)
{
return PreorderedTypeCheck(
shapeDataB, orientationB, velocityB,
shapeDataA, -offsetB, orientationA, velocityA,
maximumT, minimumProgression, convergenceThreshold, maximumIterationCount,
flipRequired, ref filter, shapes, sweepTasks, pool);
}
else
{
return PreorderedTypeCheck(
shapeDataA, orientationA, velocityA,
shapeDataB, offsetB, orientationB, velocityB,
maximumT, minimumProgression, convergenceThreshold, maximumIterationCount,
flipRequired, ref filter, shapes, sweepTasks, pool);
}
}
}

public class SweepTaskRegistry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ protected unsafe override bool PreorderedTypeSweep<TSweepFilter>(
return t1 < float.MaxValue;
}

void TestChild()
{

}

protected override unsafe bool PreorderedTypeSweep(void* shapeDataA, in RigidPose localPoseA, in Quaternion orientationA, in BodyVelocity velocityA, void* shapeDataB, in RigidPose localPoseB, in Vector3 offsetB, in Quaternion orientationB, in BodyVelocity velocityB, float maximumT, float minimumProgression, float convergenceThreshold, int maximumIterationCount, out float t0, out float t1, out Vector3 hitLocation, out Vector3 hitNormal)
{
throw new NotImplementedException("Compounds and meshes can never be nested; this should never be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public unsafe void FindOverlaps(
offsetB, orientationB, velocityB, maximumT, out var sweep, out var min, out var max);
ref var childOverlaps = ref overlaps.GetOverlapsForChild(i);
childOverlaps.ChildIndex = i;
compoundB.FindLocalOverlaps<ChildOverlapsCollection>(min, max, sweep, maximumT, pool, shapes, Unsafe.AsPointer(ref childOverlaps));

ShapeTreeSweepLeafTester<ChildOverlapsCollection> enumerator;
enumerator.Pool = pool;
enumerator.Overlaps = Unsafe.AsPointer(ref childOverlaps);

compoundB.FindLocalOverlaps(min, max, sweep, maximumT, pool, shapes, ref enumerator);
}
}
}
Expand Down
Loading