升级obi
This commit is contained in:
@@ -67,17 +67,29 @@ namespace Obi
|
||||
int optimizationIterations,
|
||||
float optimizationTolerance)
|
||||
{
|
||||
var co = new BurstQueryResult() { simplexIndex = simplexIndex, queryIndex = shapeIndex };
|
||||
var co = new BurstQueryResult { simplexIndex = simplexIndex, queryIndex = shapeIndex };
|
||||
float4 simplexBary = BurstMath.BarycenterForSimplexOfSize(simplexSize);
|
||||
|
||||
var colliderPoint = BurstLocalOptimization.Optimize<BurstBoxQuery>(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out float4 convexPoint, optimizationIterations, optimizationTolerance);
|
||||
var colliderPoint = BurstLocalOptimization.Optimize(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out float4 convexPoint, optimizationIterations, optimizationTolerance);
|
||||
|
||||
float4 simplexPrevPosition = float4.zero;
|
||||
float simplexRadius = 0;
|
||||
|
||||
for (int j = 0; j < simplexSize; ++j)
|
||||
{
|
||||
int particleIndex = simplices[simplexStart + j];
|
||||
simplexPrevPosition += positions[particleIndex] * simplexBary[j];
|
||||
simplexRadius += BurstMath.EllipsoidRadius(colliderPoint.normal, orientations[particleIndex], radii[particleIndex].xyz) * simplexBary[j];
|
||||
}
|
||||
|
||||
co.queryPoint = colliderPoint.point;
|
||||
co.normal = colliderPoint.normal;
|
||||
co.simplexBary = simplexBary;
|
||||
co.distance = math.dot(simplexPrevPosition - colliderPoint.point, colliderPoint.normal) - simplexRadius;
|
||||
|
||||
results.Enqueue(co);
|
||||
if (co.distance <= shape.maxDistance)
|
||||
results.Enqueue(co);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#if (OBI_BURST && OBI_MATHEMATICS && OBI_COLLECTIONS)
|
||||
using Unity.Collections;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Obi
|
||||
{
|
||||
@@ -17,7 +18,7 @@ namespace Obi
|
||||
|
||||
// express ray in simplex space (ellipsoid == scaled sphere)
|
||||
float4 rayOrigin = math.mul(colliderToSimplex, new float4(shape.center.xyz,1));
|
||||
float4 rayDirection = math.normalizesafe(math.mul(colliderToSimplex, new float4((shape.size - shape.center).xyz,0)));
|
||||
float4 rayDirection = math.normalizesafe(math.mul(colliderToSimplex, new float4(shape.size.xyz,0)));
|
||||
|
||||
float rayDistance = ObiUtils.RaySphereIntersection(rayOrigin.xyz, rayDirection.xyz, float3.zero, 1);
|
||||
|
||||
@@ -25,7 +26,7 @@ namespace Obi
|
||||
{
|
||||
point = colliderToSolver.InverseTransformPointUnscaled(point);
|
||||
|
||||
float4 centerLine = BurstMath.NearestPointOnEdge(shape.center * colliderToSolver.scale, shape.size * colliderToSolver.scale, point, out float mu);
|
||||
float4 centerLine = BurstMath.NearestPointOnEdge(shape.center * colliderToSolver.scale, (shape.center + shape.size) * colliderToSolver.scale, point, out float mu);
|
||||
float4 centerToPoint = point - centerLine;
|
||||
float distanceToCenter = math.length(centerToPoint);
|
||||
|
||||
@@ -57,17 +58,33 @@ namespace Obi
|
||||
int optimizationIterations,
|
||||
float optimizationTolerance)
|
||||
{
|
||||
var co = new BurstQueryResult() { simplexIndex = simplexIndex, queryIndex = shapeIndex };
|
||||
var co = new BurstQueryResult { simplexIndex = simplexIndex, queryIndex = shapeIndex };
|
||||
float4 simplexBary = BurstMath.BarycenterForSimplexOfSize(simplexSize);
|
||||
|
||||
var colliderPoint = BurstLocalOptimization.Optimize<BurstRay>(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out float4 convexPoint, optimizationIterations, optimizationTolerance);
|
||||
var colliderPoint = BurstLocalOptimization.Optimize(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out float4 convexPoint, optimizationIterations, optimizationTolerance);
|
||||
|
||||
co.queryPoint = colliderPoint.point;
|
||||
float4 simplexPrevPosition = float4.zero;
|
||||
float simplexRadius = 0;
|
||||
|
||||
for (int j = 0; j < simplexSize; ++j)
|
||||
{
|
||||
int particleIndex = simplices[simplexStart + j];
|
||||
simplexPrevPosition += positions[particleIndex] * simplexBary[j];
|
||||
simplexRadius += BurstMath.EllipsoidRadius(colliderPoint.normal, orientations[particleIndex], radii[particleIndex].xyz) * simplexBary[j];
|
||||
}
|
||||
|
||||
co.queryPoint = colliderPoint.point;
|
||||
co.normal = colliderPoint.normal;
|
||||
co.simplexBary = simplexBary;
|
||||
co.distance = math.dot(simplexPrevPosition - colliderPoint.point, colliderPoint.normal) - simplexRadius;
|
||||
|
||||
results.Enqueue(co);
|
||||
if (co.distance <= shape.maxDistance)
|
||||
{
|
||||
float4 pointOnRay = colliderPoint.point + colliderPoint.normal * co.distance;
|
||||
co.distanceAlongRay = math.dot(pointOnRay.xyz - shape.center.xyz, math.normalizesafe(shape.size.xyz));
|
||||
results.Enqueue(co);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,17 +39,29 @@ namespace Obi
|
||||
int optimizationIterations,
|
||||
float optimizationTolerance)
|
||||
{
|
||||
var co = new BurstQueryResult() { simplexIndex = simplexIndex, queryIndex = shapeIndex };
|
||||
var co = new BurstQueryResult { simplexIndex = simplexIndex, queryIndex = shapeIndex };
|
||||
float4 simplexBary = BurstMath.BarycenterForSimplexOfSize(simplexSize);
|
||||
|
||||
var colliderPoint = BurstLocalOptimization.Optimize<BurstSphereQuery>(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out float4 convexPoint, optimizationIterations, optimizationTolerance);
|
||||
var colliderPoint = BurstLocalOptimization.Optimize(ref this, positions, orientations, radii, simplices, simplexStart, simplexSize,
|
||||
ref simplexBary, out float4 convexPoint, optimizationIterations, optimizationTolerance);
|
||||
|
||||
float4 simplexPrevPosition = float4.zero;
|
||||
float simplexRadius = 0;
|
||||
|
||||
for (int j = 0; j < simplexSize; ++j)
|
||||
{
|
||||
int particleIndex = simplices[simplexStart + j];
|
||||
simplexPrevPosition += positions[particleIndex] * simplexBary[j];
|
||||
simplexRadius += BurstMath.EllipsoidRadius(colliderPoint.normal, orientations[particleIndex], radii[particleIndex].xyz) * simplexBary[j];
|
||||
}
|
||||
|
||||
co.queryPoint = colliderPoint.point;
|
||||
co.normal = colliderPoint.normal;
|
||||
co.simplexBary = simplexBary;
|
||||
co.distance = math.dot(simplexPrevPosition - colliderPoint.point, colliderPoint.normal) - simplexRadius;
|
||||
|
||||
results.Enqueue(co);
|
||||
if (co.distance <= shape.maxDistance)
|
||||
results.Enqueue(co);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace Obi
|
||||
{
|
||||
var cell = grid.usedCells[c];
|
||||
|
||||
// calculate thickedned grid bounds:
|
||||
// calculate thickened grid bounds:
|
||||
float size = NativeMultilevelGrid<int>.CellSizeOfLevel(cell.Coords.w);
|
||||
float4 cellPos = (float4)cell.Coords * size;
|
||||
BurstAabb cellBounds = new BurstAabb(cellPos - new float4(size), cellPos + new float4(2 * size));
|
||||
@@ -85,7 +85,7 @@ namespace Obi
|
||||
|
||||
private BurstAabb CalculateShapeAABB(in BurstQueryShape shape)
|
||||
{
|
||||
float offset = shape.contactOffset + shape.distance;
|
||||
float offset = shape.contactOffset + shape.maxDistance;
|
||||
switch (shape.type)
|
||||
{
|
||||
case QueryShape.QueryType.Sphere:
|
||||
@@ -93,7 +93,7 @@ namespace Obi
|
||||
case QueryShape.QueryType.Box:
|
||||
return new BurstAabb(shape.center - shape.size*0.5f - offset, shape.center + shape.size * 0.5f + offset);
|
||||
case QueryShape.QueryType.Ray:
|
||||
return new BurstAabb(shape.center, shape.size, offset);
|
||||
return new BurstAabb(shape.center, shape.center + shape.size, offset);
|
||||
}
|
||||
return new BurstAabb();
|
||||
}
|
||||
@@ -108,57 +108,22 @@ namespace Obi
|
||||
switch (shape.type)
|
||||
{
|
||||
case QueryShape.QueryType.Sphere:
|
||||
BurstSphereQuery sphereShape = new BurstSphereQuery() { colliderToSolver = shapeToSolver, shape = shape};
|
||||
BurstSphereQuery sphereShape = new BurstSphereQuery { colliderToSolver = shapeToSolver, shape = shape};
|
||||
sphereShape.Query(shapeIndex, positions, orientations, radii, simplices,
|
||||
simplexIndex, simplexStart, simplexSize, results, parameters.surfaceCollisionIterations, parameters.surfaceCollisionTolerance);
|
||||
break;
|
||||
case QueryShape.QueryType.Box:
|
||||
BurstBoxQuery boxShape = new BurstBoxQuery() { colliderToSolver = shapeToSolver, shape = shape};
|
||||
BurstBoxQuery boxShape = new BurstBoxQuery { colliderToSolver = shapeToSolver, shape = shape};
|
||||
boxShape.Query(shapeIndex, positions, orientations, radii, simplices,
|
||||
simplexIndex, simplexStart, simplexSize, results, parameters.surfaceCollisionIterations, parameters.surfaceCollisionTolerance);
|
||||
break;
|
||||
case QueryShape.QueryType.Ray:
|
||||
BurstRay rayShape = new BurstRay() { colliderToSolver = shapeToSolver, shape = shape };
|
||||
BurstRay rayShape = new BurstRay { colliderToSolver = shapeToSolver, shape = shape };
|
||||
rayShape.Query(shapeIndex, positions, orientations, radii, simplices,
|
||||
simplexIndex, simplexStart, simplexSize, results, parameters.surfaceCollisionIterations, parameters.surfaceCollisionTolerance);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public struct CalculateQueryDistances : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] public NativeArray<float4> prevPositions;
|
||||
[ReadOnly] public NativeArray<quaternion> prevOrientations;
|
||||
[ReadOnly] public NativeArray<float4> radii;
|
||||
|
||||
// simplex arrays:
|
||||
[ReadOnly] public NativeArray<int> simplices;
|
||||
[ReadOnly] public SimplexCounts simplexCounts;
|
||||
|
||||
public NativeArray<BurstQueryResult> queryResults;
|
||||
|
||||
public void Execute(int i)
|
||||
{
|
||||
var result = queryResults[i];
|
||||
|
||||
int simplexStart = simplexCounts.GetSimplexStartAndSize(result.simplexIndex, out int simplexSize);
|
||||
|
||||
float4 simplexPrevPosition = float4.zero;
|
||||
float simplexRadius = 0;
|
||||
|
||||
for (int j = 0; j < simplexSize; ++j)
|
||||
{
|
||||
int particleIndex = simplices[simplexStart + j];
|
||||
simplexPrevPosition += prevPositions[particleIndex] * result.simplexBary[j];
|
||||
simplexRadius += BurstMath.EllipsoidRadius(result.normal, prevOrientations[particleIndex], radii[particleIndex].xyz) * result.simplexBary[j];
|
||||
}
|
||||
|
||||
// update contact distance
|
||||
result.distance = math.dot(simplexPrevPosition - result.queryPoint, result.normal) - simplexRadius;
|
||||
queryResults[i] = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user