private void CreateCompoundBody() { List <CompoundShapeEntry> shapes = new List <CompoundShapeEntry>(); // Create parts of compound shape Vector3 blockPos = new Vector3(); for (int z = 0; z < GAME_FIELD_SIZE; ++z) { for (int y = 0; y < GAME_FIELD_SIZE; ++y) { for (int x = 0; x < GAME_FIELD_SIZE; ++x) { blockPos.X = (x - GAME_FIELD_SIZE * 0.5f) * BLOCK_SIZE + BLOCK_SIZE * 0.5f; blockPos.Y = (y - GAME_FIELD_SIZE * 0.5f) * BLOCK_SIZE + BLOCK_SIZE * 0.5f; blockPos.Z = (z - GAME_FIELD_SIZE * 0.5f) * BLOCK_SIZE + BLOCK_SIZE * 0.5f; BoxShape blockShape = new BoxShape(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); CompoundShapeEntry entry = new CompoundShapeEntry(blockShape, blockPos); shapes.Add(entry); } } } // Create compound body compoundBody = new CompoundBody(shapes, COMPOUND_BODY_MASS); compoundBody.PositionUpdateMode = BEPUphysics.PositionUpdating.PositionUpdateMode.Continuous; compoundBody.AngularDamping = COMPOUND_BODY_ANGULAR_DAMPING; // Add constraint prevents compound body from moving constraint = new MaximumLinearSpeedConstraint(compoundBody, 0.0f); // Create collision group for removed blocks removedBlocksGroup = new CollisionGroup(); // Mark all chapes in compound body to be removed for now foreach (CompoundChild child in compoundBody.CollisionInformation.Children) { child.CollisionInformation.CollisionRules.Group = removedBlocksGroup; } // Add compound body and its constraints to physics space space.Add(compoundBody); space.Add(constraint); //compoundBody.CollisionInformation.Events.ContactCreated += handleContactCreated; }
protected override void OnEnabled() { if (!string.IsNullOrEmpty(JointTargetName)) { Collider collider = _goqs.FindByName(JointTargetName)?.GetComponent <Collider>(); if (collider != null) { Entity entity = new Sphere(Transform.Position, 0.1f, float.MaxValue) { IsAffectedByGravity = false }; MaximumLinearSpeedConstraint constraint = new MaximumLinearSpeedConstraint(entity, 0f); _physics.AddObject(entity); _physics.AddObject(constraint); RevoluteAngularJoint joint = new RevoluteAngularJoint(entity, collider.Entity, Transform.Up); _physics.AddObject(joint); } else { Console.WriteLine("Autojoint failed because no child Collider was found."); } } }
public void LoadLevel(String fileName) { // Load level data GameLevelContent levelData = Game.Content.Load <GameLevelContent>(fileName); int blocksCount = levelData.BlocksCount(); // List of blocks chapes for compound body List <CompoundShapeEntry> shapes = new List <CompoundShapeEntry>(); // Create block groups and block physics foreach (BlockGroupData blockGroup in levelData.BlockGroups) { // Create block group LoadBlockType(blockGroup.BlockTypeName, blocksCount); // Create physical representation of blocks in this group Vector3 scale; Quaternion rotation; Vector3 translation; foreach (BlockData blockData in blockGroup.Blocks) { // Extract size, position and orientation values from block data transform blockData.Transform.Decompose(out scale, out rotation, out translation); blockData.Scale = scale; // Create physical shape of the block to be part of compound body of blocks BoxShape blockShape = new BoxShape(scale.X, scale.Y, scale.Z); // Create compound shape entry for compund body of blocks CompoundShapeEntry entry = new CompoundShapeEntry(blockShape, new RigidTransform(translation, rotation)); shapes.Add(entry); } } // Create compound body compoundBody = new CompoundBody(shapes, COMPOUND_BODY_MASS); compoundBody.PositionUpdateMode = BEPUphysics.PositionUpdating.PositionUpdateMode.Continuous; compoundBody.AngularDamping = COMPOUND_BODY_ANGULAR_DAMPING; // Compound body has Position and LocalPosition (in Collision information) // Position property is position of mass center in global space - it is calculated automatically. // LocalPosition property is position of geometry of the body in its local space. // So in order to create compound body which is rotated around desired position ((0,0,0) for now) // We should switch Position and LocalPosition properties of our compound body. compoundBody.CollisionInformation.LocalPosition = compoundBody.Position; compoundBody.Position = Vector3.Zero; // Add constraint prevents compound body from moving constraint = new MaximumLinearSpeedConstraint(compoundBody, 0.0f); // Create collision group for removed blocks removedBlocksGroup = new CollisionGroup(); // Create blocks int childCollidableIndex = 0; foreach (BlockGroupData blockGroup in levelData.BlockGroups) { Matrix localPosTransform = Matrix.CreateTranslation(compoundBody.CollisionInformation.LocalPosition); foreach (BlockData blockData in blockGroup.Blocks) { // Obtain block type and instanced mesh for the block BlockType blockType = blockTypes[blockGroup.BlockTypeName]; InstancedMesh <VertexData> instancedMesh = blockInstancedMeshes[blockGroup.BlockTypeName]; // Obtain physics body (a part of compound body) for the block CompoundChild child = compoundBody.CollisionInformation.Children[childCollidableIndex]; // Create instance of the block in instanced mesh InstancedMesh <VertexData> .Instance instance = instancedMesh.AppendInstance(Matrix.CreateScale(blockData.Scale) * child.Entry.LocalTransform.Matrix * localPosTransform); // Store new block instance to the list Block block = new Block(blockType, instance, child); blocks.Add(block); block.Scale = blockData.Scale; childCollidableIndex++; } } // Add compound body and its constraints to physics space space.Add(compoundBody); space.Add(constraint); }