Skip to content

Commit a157761

Browse files
committed
update comments
1 parent 7c9b111 commit a157761

46 files changed

Lines changed: 527 additions & 413 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Box2D.NET/B2AABBs.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public static bool b2EnlargeAABB(ref B2AABB a, in B2AABB b)
5252

5353

5454
// Ray cast an AABB
55-
// From Real-time Collision Detection, p179.
5655
public static B2CastOutput b2AABB_RayCast(in B2AABB a, in B2Vec2 p1, in B2Vec2 p2)
5756
{
5857
// Radius not handled
@@ -169,4 +168,4 @@ public static B2CastOutput b2AABB_RayCast(in B2AABB a, in B2Vec2 p1, in B2Vec2 p
169168
return output;
170169
}
171170
}
172-
}
171+
}

src/Box2D.NET/B2ArenaAllocator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace Box2D.NET
88
{
9+
// This is a stack-like arena allocator used for fast per step allocations.
10+
// You must nest allocate/free pairs. The code will B2_ASSERT
11+
// if you try to interleave multiple allocate/free pairs.
12+
// This allocator uses the heap if space is insufficient.
13+
// I could remove the need to free entries individually.
914
public class B2ArenaAllocator
1015
{
1116
private readonly object _lock;
@@ -75,4 +80,4 @@ public Span<IB2ArenaAllocatable> AsSpan()
7580
return new Span<IB2ArenaAllocatable>(_allocators);
7681
}
7782
}
78-
}
83+
}

src/Box2D.NET/B2ArenaAllocators.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static void b2DestroyArenaAllocator(B2ArenaAllocator allocator)
9494
alloc.allocation -= entry.size;
9595
b2Array_Pop(ref alloc.entries);
9696
}
97-
97+
// Grow the arena based on usage
9898
public static void b2GrowArena(B2ArenaAllocator allocator)
9999
{
100100
var allocSpan = allocator.AsSpan();
@@ -143,4 +143,4 @@ public static int b2GetMaxArenaAllocation(B2ArenaAllocator allocator)
143143
return maxAllocation;
144144
}
145145
}
146-
}
146+
}

src/Box2D.NET/B2Bodies.cs

Lines changed: 70 additions & 45 deletions
Large diffs are not rendered by default.

src/Box2D.NET/B2BodyState.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,31 @@ namespace Box2D.NET
3030
// according to substep progress. Contacts have reduced stability when anchors are rotated during substeps, especially for
3131
// round shapes.
3232

33-
// 32 bytes
33+
// Body State
34+
// The body state is designed for fast conversion to and from SIMD via scatter-gather.
35+
// Only awake dynamic and kinematic bodies have a body state.
36+
// This is used in the performance critical constraint solver
37+
//
38+
// The solver operates on the body state. The body state array does not hold static bodies. Static bodies are shared
39+
// across worker threads. It would be okay to read their states, but writing to them would cause cache thrashing across
40+
// workers, even if the values don't change.
41+
// This causes some trouble when computing anchors. I rotate joint anchors using the body rotation every sub-step. For static
42+
// bodies the anchor doesn't rotate. Body A or B could be static and this can lead to lots of branching. This branching
43+
// should be minimized.
44+
//
45+
// Solution 1:
46+
// Use delta rotations. This means anchors need to be prepared in world space. The delta rotation for static bodies will be
47+
// identity using a dummy state. Base separation and angles need to be computed. Manifolds will be behind a frame, but that
48+
// is probably best if bodies move fast.
49+
//
50+
// Solution 2:
51+
// Use full rotation. The anchors for static bodies will be in world space while the anchors for dynamic bodies will be in local
52+
// space. Potentially confusing and bug prone.
53+
//
54+
// Note:
55+
// I rotate joint anchors each sub-step but not contact anchors. Joint stability improves a lot by rotating joint anchors
56+
// according to substep progress. Contacts have reduced stability when anchors are rotated during substeps, especially for
57+
// round shapes.
3458
public class B2BodyState
3559
{
3660
public B2Vec2 linearVelocity; // 8
@@ -75,4 +99,4 @@ public void CopyFrom(B2BodyState other)
7599
deltaRotation = other.deltaRotation;
76100
}
77101
}
78-
}
102+
}

src/Box2D.NET/B2BroadPhase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class B2BroadPhase
2525
// These are the results from the pair query and are used to create new contacts
2626
// in deterministic order. There is a move result linked list for each moving shape and
2727
// these follow the dynamic tree query order for determinism.
28-
// todo these could be in the step context
2928
public ArraySegment<B2MoveResult> moveResults;
3029
public ArraySegment<B2MovePair> movePairs;
3130
public int movePairCapacity;

src/Box2D.NET/B2BroadPhases.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public static void b2BufferMove(B2BroadPhase bp, int queryProxy)
6565
}
6666

6767

68-
//
69-
// static FILE* s_file = NULL;
68+
// #include <stdio.h>
7069
public static void b2CreateBroadPhase(ref B2BroadPhase bp)
7170
{
7271
B2_ASSERT((int)B2BodyType.b2_bodyTypeCount == 3, "must be three body types");

src/Box2D.NET/B2CTZs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Box2D.NET
88
{
99
public static class B2CTZs
1010
{
11-
// uint에 대해 trailing zero count (CTZ)
11+
// https://en.wikipedia.org/wiki/Find_first_set
1212
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1313
public static uint b2CTZ32(uint block)
1414
{
@@ -23,7 +23,7 @@ public static uint b2CTZ32(uint block)
2323
return count;
2424
}
2525

26-
// uint에 대해 leading zero count (CLZ)
26+
// This function doesn't need to be fast, so using the Ivy Bridge fallback.
2727
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2828
public static uint b2CLZ32(uint value)
2929
{
@@ -95,4 +95,4 @@ public static int b2RoundUpPowerOf2(int x)
9595
return 1 << (32 - (int)b2CLZ32((uint)x - 1));
9696
}
9797
}
98-
}
98+
}

src/Box2D.NET/B2ChainDef.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Box2D.NET
1818
/// https://en.wikipedia.org/wiki/Polygonal_chain
1919
/// Must be initialized using b2DefaultChainDef().
2020
/// @warning Do not use chain shapes unless you understand the limitations. This is an advanced feature.
21-
/// @ingroup shape
21+
/// @ingroup shape
2222
public struct B2ChainDef
2323
{
2424
/// Use this to store application specific shape data.

src/Box2D.NET/B2CollisionPlane.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct B2CollisionPlane
1111
/// The collision plane between the mover and some shape
1212
public B2Plane plane;
1313

14-
/// Setting this to float.MaxValue makes the plane as rigid as possible. Lower values can
14+
/// Setting this to FLT_MAX makes the plane as rigid as possible. Lower values can
1515
/// make the plane collision soft. Usually in meters.
1616
public float pushLimit;
1717

0 commit comments

Comments
 (0)