Expose Box3D collision profiles through Ymir
This commit is contained in:
parent
dedbc4e429
commit
e49fe42ef9
21 changed files with 540 additions and 51 deletions
|
|
@ -110,7 +110,8 @@ internal static partial class Box3DNative
|
|||
float radius,
|
||||
float mass,
|
||||
float restitution,
|
||||
uint isStatic);
|
||||
uint isStatic,
|
||||
uint isKinematic);
|
||||
|
||||
[LibraryImport(LibraryName)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
||||
|
|
@ -191,6 +192,8 @@ internal sealed class Box3DSessionHandle : SafeHandleZeroOrMinusOneIsInvalid
|
|||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly record struct Box3DBodyInput(
|
||||
ulong StableId,
|
||||
ulong CollisionCategoryBits,
|
||||
ulong CollisionMaskBits,
|
||||
float PositionX,
|
||||
float PositionZ,
|
||||
float VelocityX,
|
||||
|
|
@ -202,7 +205,11 @@ internal readonly record struct Box3DBodyInput(
|
|||
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 Box3DRadialField(
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ namespace Ymir.Box3D;
|
|||
|
||||
internal readonly record struct Box3DBody(
|
||||
ulong StableId,
|
||||
ulong CollisionCategoryBits,
|
||||
ulong CollisionMaskBits,
|
||||
float PositionX,
|
||||
float PositionZ,
|
||||
float VelocityX,
|
||||
|
|
@ -15,7 +17,11 @@ internal readonly record struct Box3DBody(
|
|||
float Radius,
|
||||
float Mass,
|
||||
float Restitution,
|
||||
bool IsStatic);
|
||||
bool IsStatic,
|
||||
bool IsKinematic,
|
||||
bool IsBullet,
|
||||
bool ParticipatesInFields,
|
||||
int CollisionGroupIndex);
|
||||
|
||||
internal readonly record struct Box3DField(float PositionX, float PositionZ, float Strength, float Radius);
|
||||
|
||||
|
|
@ -63,7 +69,7 @@ internal readonly record struct Box3DContactFact(
|
|||
|
||||
internal sealed class Box3DSession : IDisposable
|
||||
{
|
||||
internal const uint AbiVersion = 3;
|
||||
internal const uint AbiVersion = 4;
|
||||
internal const int DefaultSubstepCount = 4;
|
||||
|
||||
private readonly Box3DSessionHandle _handle;
|
||||
|
|
@ -90,6 +96,8 @@ internal sealed class Box3DSession : IDisposable
|
|||
var body = bodies[i];
|
||||
native[i] = new Box3DBodyInput(
|
||||
body.StableId,
|
||||
body.CollisionCategoryBits,
|
||||
body.CollisionMaskBits,
|
||||
body.PositionX,
|
||||
body.PositionZ,
|
||||
body.VelocityX,
|
||||
|
|
@ -101,7 +109,11 @@ internal sealed class Box3DSession : IDisposable
|
|||
body.Radius,
|
||||
body.Mass,
|
||||
body.Restitution,
|
||||
body.IsStatic ? 1u : 0u);
|
||||
body.IsStatic ? 1u : 0u,
|
||||
body.IsKinematic ? 1u : 0u,
|
||||
body.IsBullet ? 1u : 0u,
|
||||
body.ParticipatesInFields ? 1u : 0u,
|
||||
body.CollisionGroupIndex);
|
||||
}
|
||||
|
||||
fixed (Box3DBodyInput* pointer = native)
|
||||
|
|
@ -155,12 +167,13 @@ internal sealed class Box3DSession : IDisposable
|
|||
float radius,
|
||||
float mass,
|
||||
float restitution,
|
||||
bool isStatic)
|
||||
bool isStatic,
|
||||
bool isKinematic)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
ThrowIfMutationFailed(
|
||||
Box3DNative.ymir_box3d_session_configure(
|
||||
_handle, stableId, radius, mass, restitution, isStatic ? 1u : 0u),
|
||||
_handle, stableId, radius, mass, restitution, isStatic ? 1u : 0u, isKinematic ? 1u : 0u),
|
||||
"configure body");
|
||||
}
|
||||
|
||||
|
|
@ -368,6 +381,8 @@ internal sealed class Box3DSession : IDisposable
|
|||
|
||||
private static Box3DBodyInput ToNative(Box3DBody body) => new(
|
||||
body.StableId,
|
||||
body.CollisionCategoryBits,
|
||||
body.CollisionMaskBits,
|
||||
body.PositionX,
|
||||
body.PositionZ,
|
||||
body.VelocityX,
|
||||
|
|
@ -379,7 +394,11 @@ internal sealed class Box3DSession : IDisposable
|
|||
body.Radius,
|
||||
body.Mass,
|
||||
body.Restitution,
|
||||
body.IsStatic ? 1u : 0u);
|
||||
body.IsStatic ? 1u : 0u,
|
||||
body.IsKinematic ? 1u : 0u,
|
||||
body.IsBullet ? 1u : 0u,
|
||||
body.ParticipatesInFields ? 1u : 0u,
|
||||
body.CollisionGroupIndex);
|
||||
|
||||
private void ThrowIfDisposed() => ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
<AssemblyName>Ymir.Box3D</AssemblyName>
|
||||
<Description>Native ABI boundary between Ymir sessions and pinned Box3D.</Description>
|
||||
<PackageId>GameCult.Ymir.Box3D</PackageId>
|
||||
<Version>0.2.0</Version>
|
||||
<Version>0.3.0</Version>
|
||||
<Authors>GameCult</Authors>
|
||||
<IsPackable>true</IsPackable>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
|
|
|
|||
|
|
@ -21,7 +21,13 @@ public sealed record PhysicsBody(
|
|||
float Restitution = 0.2f,
|
||||
Vec2? Direction = null,
|
||||
float AngularVelocity = 0.0f,
|
||||
float Torque = 0.0f);
|
||||
float Torque = 0.0f,
|
||||
bool IsKinematic = false,
|
||||
bool IsBullet = false,
|
||||
bool ParticipatesInFields = true,
|
||||
ulong CollisionCategoryBits = 1UL,
|
||||
ulong CollisionMaskBits = ulong.MaxValue,
|
||||
int CollisionGroupIndex = 0);
|
||||
|
||||
public sealed record RadialField(
|
||||
string Id,
|
||||
|
|
|
|||
|
|
@ -29,7 +29,8 @@ public sealed record YmirConfigureBodyCommand(
|
|||
float Radius,
|
||||
float Mass,
|
||||
bool IsStatic,
|
||||
float Restitution);
|
||||
float Restitution,
|
||||
bool IsKinematic = false);
|
||||
|
||||
public sealed record YmirApplyForceCommand(
|
||||
YmirCommandHeader Header,
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ public sealed class YmirOperatorStateDocument
|
|||
public string BatchKernel { get; set; } = "";
|
||||
|
||||
[Key(5)]
|
||||
public string Persistence { get; set; } = "CultCache gamecult.ymir.world_state.v1; explicit v0 read migration";
|
||||
public string Persistence { get; set; } = "CultCache gamecult.ymir.world_state.v2; explicit v1/v0 read migration";
|
||||
|
||||
[Key(6)]
|
||||
public string UpdatedAtUtc { get; set; } = "";
|
||||
|
|
@ -90,7 +90,7 @@ public static class YmirServicePublication
|
|||
"stable GameCult id to transient Box3D handle projection",
|
||||
"Box3D-backed circle overlap and cast queries over submitted bodies",
|
||||
"deterministic result ordering",
|
||||
"CultCache world snapshot v1 with explicit v0 read migration",
|
||||
"CultCache world snapshot v2 with explicit v1/v0 read migration",
|
||||
"diagnostic Eve publication"
|
||||
],
|
||||
DoesNotOwn =
|
||||
|
|
@ -115,7 +115,7 @@ public static class YmirServicePublication
|
|||
StateOwner = "In-process retained Box3D sessions, isolated snapshot steps, and diagnostic projections",
|
||||
NumericSubstrate = "Box3D v0.1.0 (C17)",
|
||||
BatchKernel = "Box3D native solver; Ymir does not own physics algorithms",
|
||||
Persistence = "CultCache gamecult.ymir.world_state.v1; explicit v0 read migration",
|
||||
Persistence = "CultCache gamecult.ymir.world_state.v2; explicit v1/v0 read migration",
|
||||
UpdatedAtUtc = FormatUtc(now)
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -217,6 +217,7 @@ public sealed class YmirSession : IDisposable
|
|||
writer.Write(command.Mass);
|
||||
writer.Write(command.IsStatic);
|
||||
writer.Write(command.Restitution);
|
||||
writer.Write(command.IsKinematic);
|
||||
});
|
||||
if (Preflight(command.Header, fingerprint, "body.configure") is { } previous)
|
||||
{
|
||||
|
|
@ -228,6 +229,7 @@ public sealed class YmirSession : IDisposable
|
|||
}
|
||||
if (!float.IsFinite(command.Radius) || command.Radius <= 0.0f ||
|
||||
!float.IsFinite(command.Mass) || (!command.IsStatic && command.Mass <= 0.0f) ||
|
||||
(command.IsStatic && command.IsKinematic) ||
|
||||
!float.IsFinite(command.Restitution) || command.Restitution < 0.0f)
|
||||
{
|
||||
return RejectAndStore(command.Header, fingerprint, "body.configure", YmirCommandError.InvalidCommand);
|
||||
|
|
@ -235,12 +237,19 @@ public sealed class YmirSession : IDisposable
|
|||
|
||||
RunNative(() =>
|
||||
{
|
||||
_native.Configure(key, command.Radius, command.Mass, command.Restitution, command.IsStatic);
|
||||
_native.Configure(
|
||||
key,
|
||||
command.Radius,
|
||||
command.Mass,
|
||||
command.Restitution,
|
||||
command.IsStatic,
|
||||
command.IsKinematic);
|
||||
_bodies[command.BodyId] = body with
|
||||
{
|
||||
Radius = command.Radius,
|
||||
Mass = command.Mass,
|
||||
IsStatic = command.IsStatic,
|
||||
IsKinematic = command.IsKinematic,
|
||||
Restitution = command.Restitution
|
||||
};
|
||||
UpdateBodyProjection(_native.CopyBodies());
|
||||
|
|
@ -267,7 +276,7 @@ public sealed class YmirSession : IDisposable
|
|||
{
|
||||
return RejectAndStore(command.Header, fingerprint, "body.apply_force", YmirCommandError.UnknownBody);
|
||||
}
|
||||
if (body.IsStatic)
|
||||
if (body.IsStatic || body.IsKinematic)
|
||||
{
|
||||
return RejectAndStore(command.Header, fingerprint, "body.apply_force", YmirCommandError.StaticBody);
|
||||
}
|
||||
|
|
@ -299,7 +308,7 @@ public sealed class YmirSession : IDisposable
|
|||
{
|
||||
return RejectAndStore(command.Header, fingerprint, "body.apply_torque", YmirCommandError.UnknownBody);
|
||||
}
|
||||
if (body.IsStatic)
|
||||
if (body.IsStatic || body.IsKinematic)
|
||||
{
|
||||
return RejectAndStore(command.Header, fingerprint, "body.apply_torque", YmirCommandError.StaticBody);
|
||||
}
|
||||
|
|
@ -791,6 +800,8 @@ public sealed class YmirSession : IDisposable
|
|||
var direction = DirectionOrDefault(body.Direction);
|
||||
return new Box3DBody(
|
||||
key,
|
||||
body.CollisionCategoryBits,
|
||||
body.CollisionMaskBits,
|
||||
body.Position.X,
|
||||
body.Position.Y,
|
||||
body.Velocity.X,
|
||||
|
|
@ -802,7 +813,11 @@ public sealed class YmirSession : IDisposable
|
|||
body.Radius,
|
||||
body.Mass,
|
||||
body.Restitution,
|
||||
body.IsStatic);
|
||||
body.IsStatic,
|
||||
body.IsKinematic,
|
||||
body.IsBullet,
|
||||
body.ParticipatesInFields,
|
||||
body.CollisionGroupIndex);
|
||||
}
|
||||
|
||||
private static Vec2 DirectionOrDefault(Vec2? direction) =>
|
||||
|
|
@ -858,7 +873,9 @@ public sealed class YmirSession : IDisposable
|
|||
if (string.IsNullOrWhiteSpace(body.Id) || !Finite(body.Position) || !Finite(body.Velocity) ||
|
||||
!float.IsFinite(body.Radius) || body.Radius <= 0.0f ||
|
||||
!float.IsFinite(body.Mass) || (!body.IsStatic && body.Mass <= 0.0f) ||
|
||||
(body.IsStatic && body.IsKinematic) ||
|
||||
!float.IsFinite(body.Restitution) || body.Restitution < 0.0f ||
|
||||
body.CollisionCategoryBits == 0 ||
|
||||
(body.Direction is { } direction && !ValidDirection(direction)) ||
|
||||
!float.IsFinite(body.AngularVelocity) || !float.IsFinite(body.Torque) ||
|
||||
(!allowTorque && body.Torque != 0.0f))
|
||||
|
|
@ -918,6 +935,12 @@ public sealed class YmirSession : IDisposable
|
|||
}
|
||||
writer.Write(body.AngularVelocity);
|
||||
writer.Write(body.Torque);
|
||||
writer.Write(body.IsKinematic);
|
||||
writer.Write(body.IsBullet);
|
||||
writer.Write(body.ParticipatesInFields);
|
||||
writer.Write(body.CollisionCategoryBits);
|
||||
writer.Write(body.CollisionMaskBits);
|
||||
writer.Write(body.CollisionGroupIndex);
|
||||
}
|
||||
|
||||
private static void WriteField(BinaryWriter writer, RadialField? field)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,12 @@ public sealed class YmirSoAWorld
|
|||
public float[] Mass { get; set; } = [];
|
||||
public float[] DynamicMask { get; set; } = [];
|
||||
public float[] Restitution { get; set; } = [];
|
||||
public float[] KinematicMask { get; set; } = [];
|
||||
public float[] BulletMask { get; set; } = [];
|
||||
public float[] FieldParticipationMask { get; set; } = [];
|
||||
public ulong[] CollisionCategoryBits { get; set; } = [];
|
||||
public ulong[] CollisionMaskBits { get; set; } = [];
|
||||
public int[] CollisionGroupIndex { get; set; } = [];
|
||||
public string[] FieldIds { get; set; } = [];
|
||||
public float[] FieldPositionX { get; set; } = [];
|
||||
public float[] FieldPositionY { get; set; } = [];
|
||||
|
|
@ -49,6 +55,12 @@ public sealed class YmirSoAWorld
|
|||
Mass = bodies.Select(body => body.Mass).ToArray(),
|
||||
DynamicMask = bodies.Select(body => body.IsStatic ? 0.0f : 1.0f).ToArray(),
|
||||
Restitution = bodies.Select(body => body.Restitution).ToArray(),
|
||||
KinematicMask = bodies.Select(body => body.IsKinematic ? 1.0f : 0.0f).ToArray(),
|
||||
BulletMask = bodies.Select(body => body.IsBullet ? 1.0f : 0.0f).ToArray(),
|
||||
FieldParticipationMask = bodies.Select(body => body.ParticipatesInFields ? 1.0f : 0.0f).ToArray(),
|
||||
CollisionCategoryBits = bodies.Select(body => body.CollisionCategoryBits).ToArray(),
|
||||
CollisionMaskBits = bodies.Select(body => body.CollisionMaskBits).ToArray(),
|
||||
CollisionGroupIndex = bodies.Select(body => body.CollisionGroupIndex).ToArray(),
|
||||
FieldIds = fields.Select(field => field.Id).ToArray(),
|
||||
FieldPositionX = fields.Select(field => field.Position.X).ToArray(),
|
||||
FieldPositionY = fields.Select(field => field.Position.Y).ToArray(),
|
||||
|
|
@ -70,7 +82,13 @@ public sealed class YmirSoAWorld
|
|||
Restitution[i],
|
||||
new Vec2(DirectionX[i], DirectionY[i]),
|
||||
AngularVelocity[i],
|
||||
Torque[i]))
|
||||
Torque[i],
|
||||
KinematicMask[i] > 0.0f,
|
||||
BulletMask[i] > 0.0f,
|
||||
FieldParticipationMask[i] > 0.0f,
|
||||
CollisionCategoryBits[i],
|
||||
CollisionMaskBits[i],
|
||||
CollisionGroupIndex[i]))
|
||||
.ToArray(),
|
||||
Enumerable.Range(0, FieldCount)
|
||||
.Select(i => new RadialField(
|
||||
|
|
@ -96,6 +114,12 @@ public sealed class YmirSoAWorld
|
|||
Mass = (float[])Mass.Clone(),
|
||||
DynamicMask = (float[])DynamicMask.Clone(),
|
||||
Restitution = (float[])Restitution.Clone(),
|
||||
KinematicMask = (float[])KinematicMask.Clone(),
|
||||
BulletMask = (float[])BulletMask.Clone(),
|
||||
FieldParticipationMask = (float[])FieldParticipationMask.Clone(),
|
||||
CollisionCategoryBits = (ulong[])CollisionCategoryBits.Clone(),
|
||||
CollisionMaskBits = (ulong[])CollisionMaskBits.Clone(),
|
||||
CollisionGroupIndex = (int[])CollisionGroupIndex.Clone(),
|
||||
FieldIds = (string[])FieldIds.Clone(),
|
||||
FieldPositionX = (float[])FieldPositionX.Clone(),
|
||||
FieldPositionY = (float[])FieldPositionY.Clone(),
|
||||
|
|
@ -125,6 +149,16 @@ public sealed class YmirSoAWorld
|
|||
throw new ArgumentOutOfRangeException(nameof(Mass), "Dynamic body mass must be positive.");
|
||||
}
|
||||
|
||||
if (DynamicMask[i] <= 0.0f && KinematicMask[i] > 0.0f)
|
||||
{
|
||||
throw new ArgumentException("A body cannot be both static and kinematic.");
|
||||
}
|
||||
|
||||
if (CollisionCategoryBits[i] == 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(CollisionCategoryBits), "Collision category bits must be non-zero.");
|
||||
}
|
||||
|
||||
var directionLengthSquared = DirectionX[i] * DirectionX[i] + DirectionY[i] * DirectionY[i];
|
||||
if (!float.IsFinite(directionLengthSquared) || directionLengthSquared <= 1.0e-8f)
|
||||
{
|
||||
|
|
@ -158,6 +192,7 @@ public sealed class YmirSoAWorld
|
|||
{
|
||||
var expected = BodyIds.Length;
|
||||
FillMissingAngularState(expected);
|
||||
FillMissingCollisionState(expected);
|
||||
foreach (var length in new[]
|
||||
{
|
||||
PositionX.Length,
|
||||
|
|
@ -171,7 +206,13 @@ public sealed class YmirSoAWorld
|
|||
Radius.Length,
|
||||
Mass.Length,
|
||||
DynamicMask.Length,
|
||||
Restitution.Length
|
||||
Restitution.Length,
|
||||
KinematicMask.Length,
|
||||
BulletMask.Length,
|
||||
FieldParticipationMask.Length,
|
||||
CollisionCategoryBits.Length,
|
||||
CollisionMaskBits.Length,
|
||||
CollisionGroupIndex.Length
|
||||
})
|
||||
{
|
||||
if (length != expected)
|
||||
|
|
@ -181,6 +222,19 @@ public sealed class YmirSoAWorld
|
|||
}
|
||||
}
|
||||
|
||||
private void FillMissingCollisionState(int expected)
|
||||
{
|
||||
if (KinematicMask.Length == 0 && expected > 0) KinematicMask = new float[expected];
|
||||
if (BulletMask.Length == 0 && expected > 0) BulletMask = new float[expected];
|
||||
if (FieldParticipationMask.Length == 0 && expected > 0)
|
||||
FieldParticipationMask = Enumerable.Repeat(1.0f, expected).ToArray();
|
||||
if (CollisionCategoryBits.Length == 0 && expected > 0)
|
||||
CollisionCategoryBits = Enumerable.Repeat(1UL, expected).ToArray();
|
||||
if (CollisionMaskBits.Length == 0 && expected > 0)
|
||||
CollisionMaskBits = Enumerable.Repeat(ulong.MaxValue, expected).ToArray();
|
||||
if (CollisionGroupIndex.Length == 0 && expected > 0) CollisionGroupIndex = new int[expected];
|
||||
}
|
||||
|
||||
private void FillMissingAngularState(int expected)
|
||||
{
|
||||
if (DirectionX.Length == 0 && expected > 0)
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ internal sealed class YmirWorldSession : IDisposable
|
|||
indicesByKey.Add(key, i);
|
||||
bodies[i] = new Box3DBody(
|
||||
key,
|
||||
world.CollisionCategoryBits[i],
|
||||
world.CollisionMaskBits[i],
|
||||
world.PositionX[i],
|
||||
world.PositionY[i],
|
||||
world.VelocityX[i],
|
||||
|
|
@ -64,7 +66,11 @@ internal sealed class YmirWorldSession : IDisposable
|
|||
world.Radius[i],
|
||||
world.Mass[i],
|
||||
world.Restitution[i],
|
||||
world.DynamicMask[i] <= 0.0f);
|
||||
world.DynamicMask[i] <= 0.0f,
|
||||
world.KinematicMask[i] > 0.0f,
|
||||
world.BulletMask[i] > 0.0f,
|
||||
world.FieldParticipationMask[i] > 0.0f,
|
||||
world.CollisionGroupIndex[i]);
|
||||
}
|
||||
|
||||
Array.Sort(bodies, static (left, right) => left.StableId.CompareTo(right.StableId));
|
||||
|
|
|
|||
|
|
@ -167,6 +167,76 @@ public sealed class YmirWorldStateDocumentV1
|
|||
};
|
||||
}
|
||||
|
||||
[CultDocument("gamecult.ymir.world_state", "gamecult.ymir.world_state.v2")]
|
||||
[MessagePackObject]
|
||||
public sealed class YmirWorldStateDocumentV2
|
||||
{
|
||||
[Key(0), CultName] public string WorldId { get; set; } = "default";
|
||||
[Key(1)] public float Time { get; set; }
|
||||
[Key(2)] public string[] BodyIds { get; set; } = [];
|
||||
[Key(3)] public float[] PositionX { get; set; } = [];
|
||||
[Key(4)] public float[] PositionY { get; set; } = [];
|
||||
[Key(5)] public float[] VelocityX { get; set; } = [];
|
||||
[Key(6)] public float[] VelocityY { get; set; } = [];
|
||||
[Key(7)] public float[] Radius { get; set; } = [];
|
||||
[Key(8)] public float[] Mass { get; set; } = [];
|
||||
[Key(9)] public float[] DynamicMask { get; set; } = [];
|
||||
[Key(10)] public float[] Restitution { get; set; } = [];
|
||||
[Key(11)] public string[] FieldIds { get; set; } = [];
|
||||
[Key(12)] public float[] FieldPositionX { get; set; } = [];
|
||||
[Key(13)] public float[] FieldPositionY { get; set; } = [];
|
||||
[Key(14)] public float[] FieldStrength { get; set; } = [];
|
||||
[Key(15)] public float[] FieldRadius { get; set; } = [];
|
||||
[Key(16)] public float[] DirectionX { get; set; } = [];
|
||||
[Key(17)] public float[] DirectionY { get; set; } = [];
|
||||
[Key(18)] public float[] AngularVelocity { get; set; } = [];
|
||||
[Key(19)] public float[] KinematicMask { get; set; } = [];
|
||||
[Key(20)] public float[] BulletMask { get; set; } = [];
|
||||
[Key(21)] public float[] FieldParticipationMask { get; set; } = [];
|
||||
[Key(22)] public ulong[] CollisionCategoryBits { get; set; } = [];
|
||||
[Key(23)] public ulong[] CollisionMaskBits { get; set; } = [];
|
||||
[Key(24)] public int[] CollisionGroupIndex { get; set; } = [];
|
||||
|
||||
public static YmirWorldStateDocumentV2 FromSoA(string worldId, YmirSoAWorld world) => new()
|
||||
{
|
||||
WorldId = worldId, Time = world.Time,
|
||||
BodyIds = (string[])world.BodyIds.Clone(),
|
||||
PositionX = (float[])world.PositionX.Clone(), PositionY = (float[])world.PositionY.Clone(),
|
||||
VelocityX = (float[])world.VelocityX.Clone(), VelocityY = (float[])world.VelocityY.Clone(),
|
||||
Radius = (float[])world.Radius.Clone(), Mass = (float[])world.Mass.Clone(),
|
||||
DynamicMask = (float[])world.DynamicMask.Clone(), Restitution = (float[])world.Restitution.Clone(),
|
||||
FieldIds = (string[])world.FieldIds.Clone(),
|
||||
FieldPositionX = (float[])world.FieldPositionX.Clone(), FieldPositionY = (float[])world.FieldPositionY.Clone(),
|
||||
FieldStrength = (float[])world.FieldStrength.Clone(), FieldRadius = (float[])world.FieldRadius.Clone(),
|
||||
DirectionX = (float[])world.DirectionX.Clone(), DirectionY = (float[])world.DirectionY.Clone(),
|
||||
AngularVelocity = (float[])world.AngularVelocity.Clone(),
|
||||
KinematicMask = (float[])world.KinematicMask.Clone(), BulletMask = (float[])world.BulletMask.Clone(),
|
||||
FieldParticipationMask = (float[])world.FieldParticipationMask.Clone(),
|
||||
CollisionCategoryBits = (ulong[])world.CollisionCategoryBits.Clone(),
|
||||
CollisionMaskBits = (ulong[])world.CollisionMaskBits.Clone(),
|
||||
CollisionGroupIndex = (int[])world.CollisionGroupIndex.Clone()
|
||||
};
|
||||
|
||||
public YmirSoAWorld ToSoA() => new()
|
||||
{
|
||||
Time = Time, BodyIds = (string[])BodyIds.Clone(),
|
||||
PositionX = (float[])PositionX.Clone(), PositionY = (float[])PositionY.Clone(),
|
||||
VelocityX = (float[])VelocityX.Clone(), VelocityY = (float[])VelocityY.Clone(),
|
||||
Radius = (float[])Radius.Clone(), Mass = (float[])Mass.Clone(),
|
||||
DynamicMask = (float[])DynamicMask.Clone(), Restitution = (float[])Restitution.Clone(),
|
||||
FieldIds = (string[])FieldIds.Clone(),
|
||||
FieldPositionX = (float[])FieldPositionX.Clone(), FieldPositionY = (float[])FieldPositionY.Clone(),
|
||||
FieldStrength = (float[])FieldStrength.Clone(), FieldRadius = (float[])FieldRadius.Clone(),
|
||||
DirectionX = (float[])DirectionX.Clone(), DirectionY = (float[])DirectionY.Clone(),
|
||||
AngularVelocity = (float[])AngularVelocity.Clone(),
|
||||
KinematicMask = (float[])KinematicMask.Clone(), BulletMask = (float[])BulletMask.Clone(),
|
||||
FieldParticipationMask = (float[])FieldParticipationMask.Clone(),
|
||||
CollisionCategoryBits = (ulong[])CollisionCategoryBits.Clone(),
|
||||
CollisionMaskBits = (ulong[])CollisionMaskBits.Clone(),
|
||||
CollisionGroupIndex = (int[])CollisionGroupIndex.Clone()
|
||||
};
|
||||
}
|
||||
|
||||
public static class YmirCultCacheStore
|
||||
{
|
||||
public static async Task SaveWorldAsync(string path, string worldId, YmirSoAWorld world)
|
||||
|
|
@ -183,8 +253,8 @@ public static class YmirCultCacheStore
|
|||
using (cache)
|
||||
{
|
||||
await cache.UpsertAsync(
|
||||
YmirWorldStateDocumentV1.FromSoA(worldId, world),
|
||||
new CultRecordHandle<YmirWorldStateDocumentV1>(new CultRecordKey($"ymir:world:{worldId}")))
|
||||
YmirWorldStateDocumentV2.FromSoA(worldId, world),
|
||||
new CultRecordHandle<YmirWorldStateDocumentV2>(new CultRecordKey($"ymir:world:{worldId}")))
|
||||
.ConfigureAwait(false);
|
||||
await cache.FlushAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
|
@ -196,7 +266,8 @@ public static class YmirCultCacheStore
|
|||
path,
|
||||
new CultCacheOpenOptions { UseDirectoryStore = true }).ConfigureAwait(false);
|
||||
var key = new CultRecordKey($"ymir:world:{worldId}");
|
||||
return cache.Get<YmirWorldStateDocumentV1>(key)?.ToSoA()
|
||||
return cache.Get<YmirWorldStateDocumentV2>(key)?.ToSoA()
|
||||
?? cache.Get<YmirWorldStateDocumentV1>(key)?.ToSoA()
|
||||
?? cache.Get<YmirWorldStateDocumentV0>(key)?.ToSoA();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ internal static class Program
|
|||
ok = true,
|
||||
path,
|
||||
record = "ymir:world:sample",
|
||||
schema = "gamecult.ymir.world_state.v1",
|
||||
schema = "gamecult.ymir.world_state.v2",
|
||||
bodyCount = result.World.BodyCount,
|
||||
fieldCount = result.World.FieldCount
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue