Expose Box3D collision profiles through Ymir
This commit is contained in:
parent
dedbc4e429
commit
e49fe42ef9
21 changed files with 540 additions and 51 deletions
|
|
@ -93,6 +93,18 @@ internal static partial class Box3DOracle
|
|||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_session_get_contact_event_count(nint session);
|
||||
|
||||
[LibraryImport(LibraryName)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_session_get_body_count(nint session);
|
||||
|
||||
[LibraryImport(LibraryName)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
internal static unsafe partial int ymir_box3d_session_copy_bodies(
|
||||
nint session,
|
||||
Box3DSessionBodyOutput* bodies,
|
||||
int capacity,
|
||||
out int written);
|
||||
|
||||
[LibraryImport(LibraryName)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
internal static unsafe partial int ymir_box3d_session_copy_contact_events(
|
||||
|
|
@ -154,6 +166,8 @@ internal readonly record struct Box3DTorqueResult(
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly record struct Box3DSessionBodyInput(
|
||||
ulong StableId,
|
||||
ulong CollisionCategoryBits,
|
||||
ulong CollisionMaskBits,
|
||||
float PositionX,
|
||||
float PositionY,
|
||||
float VelocityX,
|
||||
|
|
@ -165,7 +179,11 @@ internal readonly record struct Box3DSessionBodyInput(
|
|||
float Radius,
|
||||
float Mass,
|
||||
float Restitution,
|
||||
uint IsStatic);
|
||||
uint IsStatic,
|
||||
uint IsKinematic,
|
||||
uint IsBullet,
|
||||
uint ParticipatesInFields,
|
||||
int CollisionGroupIndex);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly record struct Box3DSessionContactEvent(
|
||||
|
|
@ -182,3 +200,14 @@ internal readonly record struct Box3DSessionContactEvent(
|
|||
float NormalY,
|
||||
float Penetration,
|
||||
float RelativeSpeed);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly record struct Box3DSessionBodyOutput(
|
||||
ulong StableId,
|
||||
float PositionX,
|
||||
float PositionY,
|
||||
float VelocityX,
|
||||
float VelocityY,
|
||||
float DirectionX,
|
||||
float DirectionY,
|
||||
float AngularVelocity);
|
||||
|
|
|
|||
|
|
@ -124,14 +124,12 @@ public sealed class Box3DParityTests
|
|||
[Fact]
|
||||
public unsafe void RetainedSessionCommandsPreserveTypedContactLifecycle()
|
||||
{
|
||||
Assert.Equal(3u, Box3DOracle.ymir_box3d_get_abi_version());
|
||||
Assert.Equal(4u, Box3DOracle.ymir_box3d_get_abi_version());
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_create(out var session));
|
||||
try
|
||||
{
|
||||
var bodyA = new Box3DSessionBodyInput(
|
||||
1, -0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0);
|
||||
var bodyB = new Box3DSessionBodyInput(
|
||||
2, 0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0);
|
||||
var bodyA = SessionBody(1, -0.9f);
|
||||
var bodyB = SessionBody(2, 0.9f);
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &bodyA));
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &bodyB));
|
||||
Assert.Equal(2, Box3DOracle.ymir_box3d_session_spawn(session, &bodyA));
|
||||
|
|
@ -155,6 +153,74 @@ public sealed class Box3DParityTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public unsafe void NegativeGroupAndCategoryMasksAreBox3DCollisionAuthority()
|
||||
{
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_create(out var session));
|
||||
try
|
||||
{
|
||||
var source = SessionBody(1, 0.0f, category: 1, mask: 2, group: -7);
|
||||
var ownPayload = SessionBody(2, 0.0f, category: 2, mask: 1, group: -7);
|
||||
var otherPayload = SessionBody(3, 0.0f, category: 2, mask: 1, group: -8);
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &source));
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &ownPayload));
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &otherPayload));
|
||||
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_step(session, 1.0f / 60.0f, 4, null, 0));
|
||||
var pairs = CopyEvents(session)
|
||||
.Where(value => value.Kind == 1)
|
||||
.Select(value => new HashSet<ulong> { value.StableIdA, value.StableIdB })
|
||||
.ToArray();
|
||||
|
||||
Assert.Single(pairs);
|
||||
Assert.Equal(new HashSet<ulong> { 1, 3 }, pairs[0]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Box3DOracle.ymir_box3d_session_destroy(session);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public unsafe void BulletUsesBox3DContinuousCollisionAgainstKinematicTarget()
|
||||
{
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_create(out var session));
|
||||
try
|
||||
{
|
||||
var bullet = SessionBody(1, -10.0f, velocityX: 100.0f, isBullet: true);
|
||||
var target = SessionBody(2, 0.0f, isKinematic: true);
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &bullet));
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_spawn(session, &target));
|
||||
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_step(session, 0.2f, 4, null, 0));
|
||||
Assert.Empty(CopyEvents(session));
|
||||
var ccdBodies = CopyBodies(session);
|
||||
Assert.True(ccdBodies.Single(value => value.StableId == 1).PositionX < 0.0f);
|
||||
Assert.Equal(0.0f, ccdBodies.Single(value => value.StableId == 2).PositionX, precision: 5);
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_step(session, 1.0f / 60.0f, 4, null, 0));
|
||||
Assert.Contains(CopyEvents(session), value =>
|
||||
(value.Kind == 1 || value.Kind == 2) &&
|
||||
new HashSet<ulong> { value.StableIdA, value.StableIdB }.SetEquals([1UL, 2UL]));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Box3DOracle.ymir_box3d_session_destroy(session);
|
||||
}
|
||||
}
|
||||
|
||||
private static Box3DSessionBodyInput SessionBody(
|
||||
ulong id,
|
||||
float positionX,
|
||||
float velocityX = 0.0f,
|
||||
ulong category = 1,
|
||||
ulong mask = ulong.MaxValue,
|
||||
int group = 0,
|
||||
bool isKinematic = false,
|
||||
bool isBullet = false) => new(
|
||||
id, category, mask, positionX, 0.0f, velocityX, 0.0f, 0.0f, 1.0f,
|
||||
0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0,
|
||||
isKinematic ? 1u : 0u, isBullet ? 1u : 0u, 1u, group);
|
||||
|
||||
private static unsafe Box3DSessionContactEvent[] CopyEvents(nint session)
|
||||
{
|
||||
var count = Box3DOracle.ymir_box3d_session_get_contact_event_count(session);
|
||||
|
|
@ -167,4 +233,17 @@ public sealed class Box3DParityTests
|
|||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
private static unsafe Box3DSessionBodyOutput[] CopyBodies(nint session)
|
||||
{
|
||||
var count = Box3DOracle.ymir_box3d_session_get_body_count(session);
|
||||
Assert.True(count >= 0);
|
||||
var bodies = new Box3DSessionBodyOutput[count];
|
||||
fixed (Box3DSessionBodyOutput* pointer = bodies)
|
||||
{
|
||||
Assert.Equal(0, Box3DOracle.ymir_box3d_session_copy_bodies(session, pointer, count, out var written));
|
||||
Assert.Equal(count, written);
|
||||
}
|
||||
return bodies;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ public sealed class YmirServicePublicationTests
|
|||
Assert.Contains("Box3D solver and collision semantics", advertisement.DoesNotOwn);
|
||||
Assert.Equal("Box3D v0.1.0 (C17)", diagnostics.NumericSubstrate);
|
||||
Assert.Contains("does not own physics algorithms", diagnostics.BatchKernel, StringComparison.Ordinal);
|
||||
Assert.Contains("world_state.v1", diagnostics.Persistence, StringComparison.Ordinal);
|
||||
Assert.Contains("world_state.v2", diagnostics.Persistence, StringComparison.Ordinal);
|
||||
Assert.Contains("v0 read migration", diagnostics.Persistence, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -190,6 +190,74 @@ public sealed class YmirSessionTests
|
|||
Assert.Equal(Vec2.Zero, session.Snapshot().Bodies.Single().Velocity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CollisionProfilesExcludeOwnGroupAndPayloadPeers()
|
||||
{
|
||||
using var session = YmirSession.Create(new YmirSessionCreateRequest(
|
||||
"collision-profile",
|
||||
[
|
||||
Body("source") with
|
||||
{
|
||||
CollisionCategoryBits = 1,
|
||||
CollisionMaskBits = 2,
|
||||
CollisionGroupIndex = -7
|
||||
},
|
||||
Body("own-payload") with
|
||||
{
|
||||
CollisionCategoryBits = 2,
|
||||
CollisionMaskBits = 1,
|
||||
CollisionGroupIndex = -7
|
||||
},
|
||||
Body("other-payload") with
|
||||
{
|
||||
CollisionCategoryBits = 2,
|
||||
CollisionMaskBits = 1,
|
||||
CollisionGroupIndex = -8
|
||||
}
|
||||
]));
|
||||
|
||||
var result = session.Step(Step("profile-step", 0, 1.0f / 60.0f));
|
||||
var begin = Assert.Single(result.ContactFacts, fact => fact.Kind == YmirContactFactKind.Begin);
|
||||
|
||||
Assert.Equal(new HashSet<string> { "source", "other-payload" }, new HashSet<string> { begin.BodyA, begin.BodyB });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BodyCanOptOutOfRadialFields()
|
||||
{
|
||||
using var session = YmirSession.Create(new YmirSessionCreateRequest(
|
||||
"field-profile",
|
||||
[Body("affected", positionX: 1.0f), Body("ignored", positionX: -5.0f) with { ParticipatesInFields = false }]));
|
||||
|
||||
var result = session.Step(new YmirStepSessionCommand(
|
||||
Header("field-step", 0),
|
||||
0.1f,
|
||||
[new RadialField("gravity", Vec2.Zero, 10.0f, 10.0f)]));
|
||||
|
||||
Assert.NotEqual(0.0f, result.World.Bodies.Single(body => body.Id == "affected").Velocity.X);
|
||||
Assert.Equal(0.0f, result.World.Bodies.Single(body => body.Id == "ignored").Velocity.X, 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KinematicTargetDoesNotAcceptBulletImpulse()
|
||||
{
|
||||
using var session = YmirSession.Create(new YmirSessionCreateRequest(
|
||||
"bullet-kinematic",
|
||||
[
|
||||
Body("bullet", positionX: -10.0f, velocityX: 100.0f) with { IsBullet = true },
|
||||
Body("target") with { IsKinematic = true }
|
||||
]));
|
||||
|
||||
var ccdStep = session.Step(Step("ccd", 0, 0.2f));
|
||||
var eventStep = session.Step(Step("event", 1, 1.0f / 60.0f));
|
||||
|
||||
Assert.Empty(ccdStep.ContactFacts);
|
||||
Assert.Contains(eventStep.ContactFacts, fact => fact.Kind is YmirContactFactKind.Begin or YmirContactFactKind.Hit);
|
||||
var target = eventStep.World.Bodies.Single(body => body.Id == "target");
|
||||
Assert.Equal(Vec2.Zero, target.Velocity);
|
||||
Assert.Equal(Vec2.Zero, target.Position);
|
||||
}
|
||||
|
||||
private static YmirCommandHeader Header(string id, long revision) => new(id, revision);
|
||||
|
||||
private static long BodyGeneration(YmirContactFact fact, string bodyId) =>
|
||||
|
|
|
|||
|
|
@ -250,6 +250,46 @@ public sealed class YmirSimulatorTests
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CultCacheV2PreservesBox3DBodyProfile()
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"ymir-v2-tests-{Guid.NewGuid():N}.cc");
|
||||
var recordsPath = $"{path}.records";
|
||||
try
|
||||
{
|
||||
var world = YmirSoAWorld.FromWorld(new YmirWorld(
|
||||
3.0f,
|
||||
[new PhysicsBody("payload", Vec2.Zero, Vec2.Zero, 0.5f, 1.0f) with
|
||||
{
|
||||
IsKinematic = true,
|
||||
IsBullet = true,
|
||||
ParticipatesInFields = false,
|
||||
CollisionCategoryBits = 4,
|
||||
CollisionMaskBits = 2,
|
||||
CollisionGroupIndex = -11
|
||||
}],
|
||||
[]));
|
||||
|
||||
await YmirCultCacheStore.SaveWorldAsync(path, "test", world);
|
||||
var loaded = await YmirCultCacheStore.LoadWorldAsync(path, "test");
|
||||
|
||||
Assert.NotNull(loaded);
|
||||
loaded!.Validate();
|
||||
var body = Assert.Single(loaded.ToWorld().Bodies);
|
||||
Assert.True(body.IsKinematic);
|
||||
Assert.True(body.IsBullet);
|
||||
Assert.False(body.ParticipatesInFields);
|
||||
Assert.Equal(4UL, body.CollisionCategoryBits);
|
||||
Assert.Equal(2UL, body.CollisionMaskBits);
|
||||
Assert.Equal(-11, body.CollisionGroupIndex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(path)) File.Delete(path);
|
||||
if (Directory.Exists(recordsPath)) Directory.Delete(recordsPath, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
private static SimulationStepResult Step(params PhysicsBody[] bodies)
|
||||
{
|
||||
var simulator = new YmirSimulator();
|
||||
|
|
|
|||
Loading…
Reference in a new issue