public override void Initialize(Collidable newCollidableA, Collidable newCollidableB) { convex = newCollidableA as ConvexCollidable; voxelGrid = newCollidableB as VoxelGrid; if (convex == null || voxelGrid == null) { convex = newCollidableB as ConvexCollidable; voxelGrid = newCollidableA as VoxelGrid; if (convex == null || voxelGrid == null) { throw new ArgumentException("Inappropriate types used to initialize contact manifold."); } } ActivePairs = new QuickDictionary <Int3, GeneralConvexPairTester>(BufferPools <Int3> .Locking, BufferPools <GeneralConvexPairTester> .Locking, BufferPools <int> .Locking, 3); activePairsBackBuffer = new QuickDictionary <Int3, GeneralConvexPairTester>(BufferPools <Int3> .Locking, BufferPools <GeneralConvexPairTester> .Locking, BufferPools <int> .Locking, 3); }
public override void Initialize(BroadPhaseEntry entryA, BroadPhaseEntry entryB) { voxelGrid = entryA as VoxelGrid; convex = entryB as ConvexCollidable; if (voxelGrid == null || convex == null) { voxelGrid = entryB as VoxelGrid; convex = entryA as ConvexCollidable; if (voxelGrid == null || convex == null) { throw new ArgumentException("Inappropriate types used to initialize pair."); } } //Contact normal goes from A to B. broadPhaseOverlap = new BroadPhaseOverlap(convex, voxelGrid, broadPhaseOverlap.CollisionRule); UpdateMaterialProperties(convex.Entity != null ? convex.Entity.Material : null, voxelGrid.Material); base.Initialize(entryA, entryB); }
/// <summary> /// Constructs a new demo. /// </summary> /// <param name="game">Game owning this demo.</param> public SimpleVoxelCollidableDemo(DemosGame game) : base(game) { VoxelGridConvexPairHandler.EnsurePairsAreRegistered(); int cellCountX = 16; int cellCountY = 16; int cellCountZ = 16; var cells = new bool[cellCountX, cellCountY, cellCountZ]; for (int i = 0; i < cellCountX; ++i) { for (int j = 0; j < cellCountY; ++j) { for (int k = 0; k < cellCountZ; ++k) { cells[i, j, k] = (Math.Sin(i * 0.55f + 6f + j * -0.325f) + Math.Sin(j * 0.35f - 0.5f + MathHelper.PiOver2) + Math.Sin(k * 0.5f + MathHelper.Pi + 6 + j * 0.25f)) > 0; } } } var cellWidth = 1f; var shape = new VoxelGridShape(cells, cellWidth); var grid = new VoxelGrid(shape, new Vector3(-cellCountX * cellWidth * 0.5f, -cellCountY * cellWidth, -cellCountZ * cellWidth * 0.5f)); Space.Add(grid); int width = 10; int height = 10; float blockWidth = 2f; float blockHeight = 1f; float blockLength = 1f; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { var toAdd = new Box( new Vector3( i * blockWidth + .5f * blockWidth * (j % 2) - width * blockWidth * .5f, blockHeight * .5f + j * (blockHeight), 0), blockWidth, blockHeight, blockLength, 10); Space.Add(toAdd); } } game.Camera.Position = new Vector3(0, 0, 25); for (int i = 0; i < cellCountX; ++i) { for (int j = 0; j < cellCountY; ++j) { for (int k = 0; k < cellCountZ; ++k) { if (shape.Cells[i, j, k]) { //This is a turbo-inefficient way to render things, but good enough for now. If you want to visualize a larger amount... you'll probably have to write your own. game.ModelDrawer.Add(new DisplayModel(game.Content.Load <Model>("cube"), game.ModelDrawer) { WorldTransform = Matrix.CreateWorldRH(grid.Position + new Vector3((i + 0.5f) * cellWidth, (j + 0.5f) * cellWidth, (k + 0.5f) * cellWidth), Vector3.Forward, Vector3.Up) }); } } } } }
///<summary> /// Cleans up the pair handler. ///</summary> public override void CleanUp() { base.CleanUp(); voxelGrid = null; convex = null; }