diff --git a/BepuPhysics/Collidables/BoundingBoxBatcher.cs b/BepuPhysics/Collidables/BoundingBoxBatcher.cs index 80d5ec0d..b6dc3f8e 100644 --- a/BepuPhysics/Collidables/BoundingBoxBatcher.cs +++ b/BepuPhysics/Collidables/BoundingBoxBatcher.cs @@ -9,11 +9,11 @@ namespace BepuPhysics { - public struct BoundsContinuation + public readonly struct BoundsContinuation { //Bits 0-30: body index //Bit 31: compound flag; if set, the continuation should merge into the target slot rather than merely setting it. - uint packed; + readonly uint packed; /// /// Gets the index of the body associated with this continuation. @@ -39,6 +39,12 @@ public bool CompoundChild } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private BoundsContinuation(uint packed) + { + this.packed = packed; + } + /// /// Creates a bounding box calculation continuation for a given noncompound body. /// @@ -46,9 +52,7 @@ public bool CompoundChild public static BoundsContinuation CreateContinuation(int bodyIndex) { Debug.Assert(bodyIndex >= 0); - BoundsContinuation toReturn; - toReturn.packed = (uint)bodyIndex; - return toReturn; + return new BoundsContinuation((uint)bodyIndex); } /// /// Creates a bounding box calculation continuation for a given compound body. @@ -57,9 +61,7 @@ public static BoundsContinuation CreateContinuation(int bodyIndex) public static BoundsContinuation CreateCompoundChildContinuation(int compoundBodyIndex) { Debug.Assert(compoundBodyIndex >= 0); - BoundsContinuation toReturn; - toReturn.packed = (1u << 31) | (uint)compoundBodyIndex; - return toReturn; + return new BoundsContinuation((1u << 31) | (uint)compoundBodyIndex); } } diff --git a/BepuPhysics/Collidables/CollidableReference.cs b/BepuPhysics/Collidables/CollidableReference.cs index c9c329ea..2cc9b530 100644 --- a/BepuPhysics/Collidables/CollidableReference.cs +++ b/BepuPhysics/Collidables/CollidableReference.cs @@ -29,12 +29,12 @@ public enum CollidableMobility /// Uses a bitpacked representation to refer to a body or static collidable. /// [StructLayout(LayoutKind.Sequential, Size = 4)] - public struct CollidableReference : IEquatable + public readonly struct CollidableReference : IEquatable { /// /// Bitpacked representation of the collidable reference. /// - public uint Packed; + public readonly uint Packed; /// /// Gets the mobility state of the owner of this collidable. diff --git a/BepuPhysics/Collidables/TypedIndex.cs b/BepuPhysics/Collidables/TypedIndex.cs index 13a00558..c6f06c84 100644 --- a/BepuPhysics/Collidables/TypedIndex.cs +++ b/BepuPhysics/Collidables/TypedIndex.cs @@ -7,12 +7,12 @@ namespace BepuPhysics.Collidables /// /// Represents an index with an associated type packed into a single integer. /// - public struct TypedIndex : IEquatable + public readonly struct TypedIndex : IEquatable { /// /// Bit packed representation of the typed index. /// - public uint Packed; + public readonly uint Packed; /// /// Gets the type index of the object. @@ -38,7 +38,7 @@ public int Index public bool Exists { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return (Packed & (1 << 31)) > 0; } + get { return (Packed & (1u << 31)) > 0; } } public TypedIndex(int type, int index) @@ -48,7 +48,7 @@ public TypedIndex(int type, int index) //Note the inclusion of a set bit in the most significant slot. //This encodes that the index was explicitly constructed, so it is a 'real' reference. //A default constructed TypeIndex will have a 0 in the MSB, so we can use the default constructor for empty references. - Packed = (uint)((type << 24) | index | (1u << 31)); + Packed = ((uint)type << 24) | (uint)index | (1u << 31); } public override string ToString() diff --git a/BepuPhysics/CollisionDetection/CollisionBatcherContinuations.cs b/BepuPhysics/CollisionDetection/CollisionBatcherContinuations.cs index f11b8dfb..caeb75b5 100644 --- a/BepuPhysics/CollisionDetection/CollisionBatcherContinuations.cs +++ b/BepuPhysics/CollisionDetection/CollisionBatcherContinuations.cs @@ -77,12 +77,12 @@ public enum CollisionContinuationType : byte } - public struct PairContinuation + public readonly struct PairContinuation { - public int PairId; - public int ChildA; - public int ChildB; - public uint Packed; + public readonly int PairId; + public readonly int ChildA; + public readonly int ChildB; + public readonly uint Packed; /// /// Covers bits [0, 20) in the packed representation. Refers to the child pair index in a subtask generating collision task that generated this continuation. diff --git a/BepuPhysics/CollisionDetection/ContinuationIndex.cs b/BepuPhysics/CollisionDetection/ContinuationIndex.cs index af92adb4..c49e296c 100644 --- a/BepuPhysics/CollisionDetection/ContinuationIndex.cs +++ b/BepuPhysics/CollisionDetection/ContinuationIndex.cs @@ -3,9 +3,9 @@ namespace BepuPhysics.CollisionDetection { - public struct CCDContinuationIndex + public readonly struct CCDContinuationIndex { - public uint Packed; + public readonly uint Packed; //From least to most significant: 30 bits index, 1 bit type, 1 bit 'exists' flag. @@ -33,7 +33,7 @@ public int Type public bool Exists { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return (Packed & (1 << 31)) > 0; } + get { return (Packed & (1u << 31)) > 0; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -44,7 +44,7 @@ public CCDContinuationIndex(int type, int index) //Note the inclusion of a set bit in the most significant slot. //This encodes that the index was explicitly constructed, so it is a 'real' reference. //A default constructed TypeIndex will have a 0 in the MSB, so we can use the default constructor for empty references. - Packed = (uint)((type << 30) | index | (1u << 31)); + Packed = ((uint)type << 30) | (uint)index | (1u << 31); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public CCDContinuationIndex(int packed) diff --git a/BepuPhysics/Trees/Leaf.cs b/BepuPhysics/Trees/Leaf.cs index 4f76877f..2e74456f 100644 --- a/BepuPhysics/Trees/Leaf.cs +++ b/BepuPhysics/Trees/Leaf.cs @@ -7,7 +7,7 @@ namespace BepuPhysics.Trees /// Pointer to a leaf's tree location. /// /// The identity of a leaf is implicit in its position within the leaf array. - public struct Leaf + public readonly struct Leaf { /// /// Gets the index of the node that the leaf is directly held by. @@ -26,7 +26,7 @@ public int ChildIndex get { return (int)((packed & 0x80000000) >> 31); } } - uint packed; + readonly uint packed; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Leaf(int nodeIndex, int childIndex)