Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class BenchmarkShapeDistance : Sample
private B2Polygon m_polygonB;
private float m_minMilliseconds;
private int m_drawIndex;
private int m_minCycles;

// for draw
private int totalIterations = 0;
Expand Down Expand Up @@ -87,7 +86,6 @@ public BenchmarkShapeDistance(SampleContext context) : base(context)
}

m_drawIndex = 0;
m_minCycles = int.MaxValue;
m_minMilliseconds = float.MaxValue;
}

Expand Down Expand Up @@ -123,7 +121,6 @@ public override void Step()
totalIterations = 0;

ulong start = b2GetTicks();
ulong startCycles = b2GetTicks();
for (int i = 0; i < m_count; ++i)
{
B2SimplexCache cache = new B2SimplexCache();
Expand All @@ -133,10 +130,7 @@ public override void Step()
totalIterations += m_outputs[i].iterations;
}

ulong endCycles = b2GetTicks();

float ms = b2GetMilliseconds(start);
m_minCycles = b2MinInt(m_minCycles, (int)endCycles - (int)startCycles);
m_minMilliseconds = b2MinFloat(m_minMilliseconds, ms);
}

Expand All @@ -151,8 +145,6 @@ public override void Draw()
if (m_context.pause == false || m_context.singleStep == true)
{
DrawTextLine($"count = {m_count}");
DrawTextLine($"min cycles = {m_minCycles}");
DrawTextLine($"ave cycles = {(float)m_minCycles / m_count}");
DrawTextLine($"min ms = {m_minMilliseconds}, ave us = {1000.0f * m_minMilliseconds / (float)m_count}");
DrawTextLine($"average iterations = {totalIterations / (float)m_count}");
}
Expand All @@ -168,4 +160,4 @@ public override void Draw()
DrawLine(m_draw, output.pointA, output.pointA + 0.5f * output.normal, B2HexColor.b2_colorYellow);
DrawTextLine($"distance = {output.distance}");
}
}
}
22 changes: 1 addition & 21 deletions src/Box2D.NET/B2Arrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,6 @@ public static int b2Array_ByteCount<T>(ref B2Array<T> a)
return a;
}

// Remove an element from an int arrayA by swapping with the last element. This updates the index contained
// in the moved element in arrayB. Assumes the integers in arrayA index into arrayB. Assumes
// the elements of arrayB have an indexName member that is the index in arrayA.
public static void b2RemoveUpdate<T>(ref B2Array<int> arrayA, ref B2Array<T> arrayB, int indexB, Func<T, int> getIndexName, Action<T, int> setIndexName)
{
int lastIndex = (arrayA).count - 1;
B2_ASSERT(0 <= (indexB) && (indexB) < (arrayB).count);
int indexA = getIndexName.Invoke((arrayB).data[indexB]);
B2_ASSERT(0 <= indexA && indexA < (arrayA).count);
if (indexA != lastIndex)
{
int movedIndex = (arrayA).data[lastIndex];
(arrayA).data[indexA] = movedIndex;
setIndexName.Invoke((arrayB).data[movedIndex], indexA);
}

(arrayA).count -= 1;
}


/* Reserve */
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void b2Array_Reserve<T>(ref B2Array<T> a, int newCapacity) where T : new()
Expand All @@ -226,4 +206,4 @@ public static void b2Array_Destroy<T>(ref B2Array<T> a)
a.capacity = 0;
}
}
}
}
45 changes: 28 additions & 17 deletions src/Box2D.NET/B2Bodies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public static void b2LimitVelocity(B2BodyState state, float maxLinearSpeed)
}
}

public static void b2RemoveBodySim(ref B2Array<B2BodySim> bodySims, ref B2Array<B2Body> bodies, int localIndex)
{
B2_ASSERT(0 <= localIndex && localIndex < bodySims.count);
int lastIndex = bodySims.count - 1;
bodySims.data[localIndex].CopyFrom(bodySims.data[lastIndex]);
B2Body movedBody = b2Array_Get(ref bodies, bodySims.data[localIndex].bodyId);
B2_ASSERT(movedBody.localIndex == lastIndex);
movedBody.localIndex = localIndex;
if (localIndex != lastIndex)
{
bodySims.data[lastIndex] = new B2BodySim();
}

bodySims.count -= 1;
}

// Get a validated body from a world using an id.
public static B2Body b2GetBodyFullId(B2World world, B2BodyId bodyId)
{
Expand Down Expand Up @@ -127,8 +143,14 @@ internal static void b2RemoveBodyFromIsland(B2World world, B2Body body)

int islandId = body.islandId;
B2Island island = b2Array_Get(ref world.islands, islandId);

b2RemoveUpdate(ref island.bodies, ref world.bodies, body.id, x => x.islandIndex, (x, idx) => x.islandIndex = idx);
{
int localIndex = body.islandIndex;
int movedBodyId = island.bodies.data[island.bodies.count - 1];
island.bodies.data[localIndex] = movedBodyId;
B2_VALIDATE(world.bodies.data[movedBodyId].islandIndex == island.bodies.count - 1);
world.bodies.data[movedBodyId].islandIndex = localIndex;
island.bodies.count -= 1;
}

if (island.bodies.count == 0)
{
Expand Down Expand Up @@ -399,27 +421,16 @@ public static void b2DestroyBody(B2BodyId bodyId)

// Remove body sim from solver set that owns it
B2SolverSet set = b2Array_Get(ref world.solverSets, body.setIndex);
int movedIndex = b2Array_RemoveSwap(ref set.bodySims, body.localIndex);
if (movedIndex != B2_NULL_INDEX)
{
// Fix moved body index
B2BodySim movedSim = set.bodySims.data[body.localIndex];
int movedId = movedSim.bodyId;
B2Body movedBody = b2Array_Get(ref world.bodies, movedId);
B2_ASSERT(movedBody.localIndex == movedIndex);
movedBody.localIndex = body.localIndex;
}
b2RemoveBodySim(ref set.bodySims, ref world.bodies, body.localIndex);

// Remove body state from awake set
if (body.setIndex == (int)B2SolverSetType.b2_awakeSet)
{
int result = b2Array_RemoveSwap(ref set.bodyStates, body.localIndex);
B2_ASSERT(result == movedIndex);
B2_UNUSED(result);
b2Array_RemoveSwap(ref set.bodyStates, body.localIndex);
}
else if (set.setIndex >= (int)B2SolverSetType.b2_firstSleepingSet && set.bodySims.count == 0)
{
// Remove solver set if it's now an orphan.
// Remove solver set if it is empty
b2DestroySolverSet(world, set.setIndex);
}

Expand Down Expand Up @@ -2053,4 +2064,4 @@ internal static bool b2ShouldBodiesCollide(B2World world, B2Body bodyA, B2Body b
return true;
}
}
}
}
30 changes: 17 additions & 13 deletions src/Box2D.NET/B2Islands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,31 @@ public static void b2DestroyIsland(B2World world, int islandId)
// assume island is empty
B2Island island = b2Array_Get(ref world.islands, islandId);
B2SolverSet set = b2Array_Get(ref world.solverSets, island.setIndex);
int movedIndex = b2Array_RemoveSwap(ref set.islandSims, island.localIndex);
if (movedIndex != B2_NULL_INDEX)
{
// Fix index on moved element
B2IslandSim movedElement = set.islandSims.data[island.localIndex];
int movedId = movedElement.islandId;
B2Island movedIsland = b2Array_Get(ref world.islands, movedId);
B2_ASSERT(movedIsland.localIndex == movedIndex);
movedIsland.localIndex = island.localIndex;
int localIndex = island.localIndex;
int lastIndex = set.islandSims.count - 1;
B2_ASSERT(0 <= localIndex && localIndex <= lastIndex);
int moveIslandId = set.islandSims.data[lastIndex].islandId;
set.islandSims.data[localIndex].CopyFrom(set.islandSims.data[lastIndex]);
world.islands.data[moveIslandId].localIndex = localIndex;
if (localIndex != lastIndex)
{
set.islandSims.data[lastIndex] = new B2IslandSim();
}

set.islandSims.count -= 1;
}

// Free island and id (preserve island revision)
b2Array_Destroy(ref island.bodies);
b2Array_Destroy(ref island.contacts);
b2Array_Destroy(ref island.joints);
island.constraintRemoveCount = 0;
island.localIndex = B2_NULL_INDEX;
island.islandId = B2_NULL_INDEX;
island.setIndex = B2_NULL_INDEX;
island.localIndex = B2_NULL_INDEX;

B2_VALIDATE(island.localIndex == B2_NULL_INDEX);

b2FreeId(world.islandIdPool, islandId);
}
Expand Down Expand Up @@ -268,10 +274,9 @@ public static void b2UnlinkContact(B2World world, B2Contact contact)
movedContact.islandIndex = removeIndex;
}

island.constraintRemoveCount += 1;

contact.islandId = B2_NULL_INDEX;
contact.islandIndex = B2_NULL_INDEX;
island.constraintRemoveCount += 1;

b2ValidateIsland(world, islandId);
}
Expand Down Expand Up @@ -350,10 +355,9 @@ public static void b2UnlinkJoint(B2World world, B2Joint joint)
movedJoint.islandIndex = removeIndex;
}

island.constraintRemoveCount += 1;

joint.islandId = B2_NULL_INDEX;
joint.islandIndex = B2_NULL_INDEX;
island.constraintRemoveCount += 1;

b2ValidateIsland(world, islandId);
}
Expand Down
22 changes: 2 additions & 20 deletions src/Box2D.NET/B2SolverSets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,7 @@ internal static void b2TrySleepIsland(B2World world, int islandId)
//memcpy( sleepBodySim, awakeSim, sizeof( b2BodySim ) );
sleepBodySim.CopyFrom(awakeSim);

int movedIndex = b2Array_RemoveSwap(ref awakeSet.bodySims, awakeBodyIndex);
if (movedIndex != B2_NULL_INDEX)
{
// fix local index on moved element
B2BodySim movedSim = awakeSet.bodySims.data[awakeBodyIndex];
int movedId = movedSim.bodyId;
B2Body movedBody = b2Array_Get(ref world.bodies, movedId);
B2_ASSERT(movedBody.localIndex == movedIndex);
movedBody.localIndex = awakeBodyIndex;
}
b2RemoveBodySim(ref awakeSet.bodySims, ref world.bodies, awakeBodyIndex);

// destroy state, no need to clone
b2Array_RemoveSwap(ref awakeSet.bodyStates, awakeBodyIndex);
Expand Down Expand Up @@ -568,16 +559,7 @@ internal static void b2TransferBody(B2World world, B2SolverSet targetSet, B2Solv
targetSim.flags &= ~((uint)B2BodyFlags.b2_isFast | (uint)B2BodyFlags.b2_isSpeedCapped | (uint)B2BodyFlags.b2_hadTimeOfImpact);

// Remove body sim from solver set that owns it
int movedIndex = b2Array_RemoveSwap(ref sourceSet.bodySims, sourceIndex);
if (movedIndex != B2_NULL_INDEX)
{
// Fix moved body index
B2BodySim movedSim = sourceSet.bodySims.data[sourceIndex];
int movedId = movedSim.bodyId;
B2Body movedBody = b2Array_Get(ref world.bodies, movedId);
B2_ASSERT(movedBody.localIndex == movedIndex);
movedBody.localIndex = sourceIndex;
}
b2RemoveBodySim(ref sourceSet.bodySims, ref world.bodies, sourceIndex);

if (sourceSet.setIndex == (int)B2SolverSetType.b2_awakeSet)
{
Expand Down
15 changes: 0 additions & 15 deletions src/Box2D.NET/B2StackArray.cs

This file was deleted.

Loading
Loading