private void UpdateRigidBody(RigidBody body, int sectorX, int sectorY)
{
Sector sector = this.sectors[sectorX, sectorY];
// Determine collision checksum
this.tempCollisionData.Clear();
int newChecksum = this.MergeCollisionData(sectorX, sectorY, this.tempCollisionData);
// If it differs from our previous value, update collision shapes
if (sector.Checksum != newChecksum)
{
// Clean up old shapes
if (sector.Shapes != null)
{
foreach (ShapeInfo shape in sector.Shapes)
body.RemoveShape(shape);
sector.Shapes.Clear();
}
else
{
sector.Shapes = new List<ShapeInfo>();
}
// Generate new shapes
{
// Determine general working data
Tilemap tilemap = this.referenceTilemap;
Tileset tileset = tilemap != null ? tilemap.Tileset.Res : null;
Vector2 tileSize = tileset != null ? tileset.TileSize : Tileset.DefaultTileSize;
Point2 sectorBaseTile = new Point2(
sectorX * SectorSize,
sectorY * SectorSize);
Vector2 sectorBasePos = sectorBaseTile * tileSize;
// Clear the temporary edge map first
this.tempEdgeMap.Clear();
// Populate the edge map with fence and block geometry
AddFenceCollisionEdges(this.tempCollisionData, this.tempEdgeMap);
AddBlockCollisionEdges(this.tempCollisionData, this.tempEdgeMap, sectorBaseTile, this.tileCount);
if (this.solidOuterEdges)
AddBorderCollisionEdges(this.tempEdgeMap, sectorBaseTile, this.tileCount);
// Now traverse the edge map and gradually create chain / loop
// shapes until all edges have been used.
Rect localRect = Rect.Align(this.origin, 0, 0, this.tileCount.X * tileSize.X, this.tileCount.Y * tileSize.Y);
GenerateCollisionShapes(this.tempEdgeMap, localRect.TopLeft + sectorBasePos, tileSize, this.roundedCorners, sector.Shapes);
// Add all the generated shapes to the target body
foreach (ShapeInfo shape in sector.Shapes)
{
body.AddShape(shape);
shape.Friction = this.shapeFriction;
shape.Restitution = this.shapeRestitution;
}
}
sector.Checksum = newChecksum;
}
this.sectors[sectorX, sectorY] = sector;
}