/// <summary> /// Constructs a new static physics actor. /// </summary> /// <param name="desc">Descriptor for the actor.</param> public TerrainBEPUActor(ActorDesc desc) : base(desc) { if (desc.Shapes.Count != 1) { throw new Exception("Terrain actors can only consist of one shape"); } ShapeDesc shapeDesc = desc.Shapes[0]; if (shapeDesc is HeightFieldShapeDesc) { // For height fields, we need to copy the data into an Array2D. var heightFieldDesc = shapeDesc as HeightFieldShapeDesc; float spacing = heightFieldDesc.SizeX / heightFieldDesc.HeightField.GetLength(0); this.collidable = new Terrain(heightFieldDesc.HeightField, new AffineTransform( new Vector3(spacing, 1, spacing), Quaternion.Identity, Vector3.Zero)); } else { throw new Exception("Shape description for a Terrain actor must be HeightFieldShapeDesc."); } // Tag the physics with data needed by the engine var tag = new EntityTag(this.ownerEntityID); this.collidable.Tag = tag; this.spaceObject = this.collidable; }
/// <summary> /// Creates an actor using shape info and information from the parent BaseEntity. /// </summary> /// <param name="PhysicsScene">Reference to the physics scene</param> protected override void CreateActor(IPhysicsScene PhysicsScene) { ShapeDesc newShape = CreateShapeFromType(shapeType); // Phantoms cannot currently have dynamic physics this.isDynamic = false; var desc = new ActorDesc(); desc.Orientation = this.parentEntity.Rotation; desc.Mass = this.mass; desc.Dynamic = this.isDynamic; desc.AffectedByGravity = this.affectedByGravity; desc.Position = this.parentEntity.Position; desc.Shapes.Add(newShape); desc.EntityID = this.parentEntity.UniqueID; desc.Game = this.parentEntity.Game; desc.Type = ActorType.Static; this.actor = PhysicsScene.CreateActor(desc); if (this.actor != null) { this.actor.EnableCollisionListening(); } }
public QuadTree(ShapeDesc shapedesc, int quadLevel) { this.sampleSize = Game.sampleSize; // terrain/water if (shapedesc.name.ToLower(CultureInfo.InvariantCulture).Contains("land") || !shapedesc.textures[0].Contains("default.dds")) { isLand = true; } else { this.sampleSize = 1; this.isLand = false; } this.vertices = shapedesc.geometry.GetVertices(); this.boundingBox = new BBox(); this.boundingBox.Set(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue); this.segments = new QuadTreeLeaf[(int)(this.sampleSize * quadLevel * this.sampleSize * quadLevel)]; for (int index = 0; index < (this.sampleSize * quadLevel) * (this.sampleSize * quadLevel); index++) { this.segments[index] = this.CreateSegment(index, quadLevel, shapedesc.geometry.GetTriangles(), shapedesc.geometry.GetVertices()); boundingBox.GrowByBox(this.segments[index].boundingBox); } QuadTreeLeaf quadTreeLeaf = new QuadTreeLeaf(); List <Triangle> triangles = shapedesc.geometry.GetTriangles(); quadTreeLeaf.boundingBox.GrowByBox(boundingBox); quadTreeLeaf.triangles = triangles; this.entirequad = quadTreeLeaf; }
/// <summary> /// Creates an actor using shape info and information from the parent BaseEntity. /// </summary> /// <param name="PhysicsScene">Reference to the physics scene</param> protected virtual void CreateActor(IPhysicsScene PhysicsScene) { ShapeDesc newShape = CreateShapeFromType(shapeType); if (newShape == null) { throw new Exception("Shape did not load properly"); } ActorDesc desc = new ActorDesc(); desc.Orientation = this.parentEntity.Rotation; desc.Mass = this.mass; desc.Dynamic = this.isDynamic; desc.AffectedByGravity = this.affectedByGravity; desc.Position = this.parentEntity.Position; desc.Shapes.Add(newShape); desc.EntityID = this.parentEntity.UniqueID; desc.Game = this.parentEntity.Game; desc.Type = ActorType.Basic; if (newShape is TriangleMeshShapeDesc) { var triDesc = newShape as TriangleMeshShapeDesc; if (triDesc.Vertices.Count > 100) { desc.Type = ActorType.Static; } } this.actor = PhysicsScene.CreateActor(desc); }
//-------------------------------------------------------------------------------------------------- void InvalidateShape(ref ShapeDesc desc) { desc.Shape.Invalidate(); desc.OcShape = desc.Shape.GetBRep(); desc.Vertices = desc.OcShape.Vertices(); desc.Edges = desc.OcShape.Edges(); desc.Faces = desc.OcShape.Faces(); }
/// <summary> /// Constructs a new static physics actor. /// </summary> /// <param name="desc">Descriptor for the actor.</param> public WaterVolumeBEPUActor(ActorDesc desc) : base(desc) { if (desc.Shapes.Count != 1) { throw new Exception("Water volume actors can only consist of one shape"); } ShapeDesc shapeDesc = desc.Shapes[0]; if (shapeDesc is BoxShapeDesc) { var boxDesc = shapeDesc as BoxShapeDesc; this.volumeExtents = boxDesc.Extents; var tris = new List <Vector3[]>(); float basinWidth = boxDesc.Extents.X; float basinLength = boxDesc.Extents.Z; float waterHeight = desc.Position.Y; this.position = desc.Position; //Remember, the triangles composing the surface need to be coplanar with the surface. In this case, this means they have the same height. tris.Add(new[] { desc.Position, new Vector3(basinWidth, 0, 0) + desc.Position, new Vector3(0, 0, basinLength) + desc.Position }); tris.Add(new[] { new Vector3(0, 0, basinLength) + desc.Position, new Vector3(basinWidth, 0, 0) + desc.Position, new Vector3(basinWidth, 0, basinLength) + desc.Position }); var msgGetScene = ObjectPool.Aquire <MsgGetPhysicsScene>(); this.game.SendInterfaceMessage(msgGetScene, Interfaces.InterfaceType.Physics); this.fluidVolume = new FluidVolume(Vector3.Up, msgGetScene.PhysicsScene.Gravity.Y, tris, waterHeight, 1.0f, 0.8f, 0.7f, msgGetScene.PhysicsScene.PhysicsSpace.BroadPhase.QueryAccelerator, msgGetScene.PhysicsScene.PhysicsSpace.ThreadManager); } else { throw new Exception("Shape description for a Terrain actor must be HeightFieldShapeDesc."); } // Tag the physics with data needed by the engine var tag = new EntityTag(this.ownerEntityID); this.fluidVolume.Tag = tag; this.spaceObject = this.fluidVolume; }
private void setupPhysics() { //if (mControlled) //{ // SimpleShape shape = new SimpleShape(new Mogre.Vector3(50, CHAR_HEIGHT, 0)); // CharacterControllerDescription description = new CharacterControllerDescription(); // physicsController = new CharacterController(); // physicsController.createCharacterController(mBodyNode.Position, shape, description, mPhysicsScene); //} //else //{ rigidBody = new RigidBody(); RigidBodyDescription description = new RigidBodyDescription(); description.Density = 4; description.LinearVelocity = new Mogre.Vector3(0, 2, 5); ShapeDesc shape = mPhysics.CreateConvexHull(new StaticMeshData(mBodyEnt.GetMesh())); rigidBody.CreateDynamic( mBodyNode.Position, mBodyNode.Orientation.ToRotationMatrix(), description, mPhysicsScene, shape); //BodyDesc bodyDesc = new BodyDesc(); //bodyDesc.LinearVelocity = new Mogre.Vector3(0, 2, 5); // //// the actor properties control the mass, position and orientation //// if you leave the body set to null it will become a static actor and wont move //ActorDesc actorDesc = new ActorDesc(); //actorDesc.Density = 4; //actorDesc.Body = bodyDesc; //actorDesc.GlobalPosition = mBodyNode.Position; //actorDesc.GlobalOrientation = mBodyNode.Orientation.ToRotationMatrix(); // a quick trick the get the size of the physics shape right is to use the bounding box of the entity //actorDesc.Shapes.Add( // mPhysics.CreateConvexHull(new // StaticMeshData(mBodyEnt.GetMesh()))); // //// finally, create the actor in the physics scene //mActor = mPhysicsScene.CreateActor(actorDesc); if (!mControlled) { ((SinbadState)mWorld).AddActorNode(new ActorNode(mBodyNode, rigidBody.Actor)); } //} }
//-------------------------------------------------------------------------------------------------- ShapeDesc MakeBooleanCut(ShapeDesc op1, ShapeDesc op2) { var desc = new ShapeDesc { Shape = BooleanCut.Create(op1.Body, new BodyShapeOperand(op2.Body)) }; desc.OcShape = desc.Shape.GetBRep(); desc.Vertices = desc.OcShape.Vertices(); Assert.AreEqual(10, desc.Vertices.Count); desc.Edges = desc.OcShape.Edges(); Assert.AreEqual(15, desc.Edges.Count); desc.Faces = desc.OcShape.Faces(); Assert.AreEqual(7, desc.Faces.Count); return(desc); }
public void CreateDynamic( Vector3 pose, Matrix3 oritentaion, RigidBodyDescription description, Scene scene, ShapeDesc shape) { BodyDesc bodyDesc = new BodyDesc(); ActorDesc actorDesc = new ActorDesc(); description.ToNxActor(ref actorDesc, ref bodyDesc); actorDesc.Body = bodyDesc; actorDesc.GlobalPosition = pose; actorDesc.GlobalOrientation = oritentaion; actorDesc.Shapes.Add(shape); Actor = scene.CreateActor(actorDesc); }
//-------------------------------------------------------------------------------------------------- ShapeDesc MakeCylinder() { var desc = new ShapeDesc { Shape = new Cylinder() { Height = 10, Radius = 2, } }; desc.Body = TestGeomGenerator.CreateBody(desc.Shape, new Pnt(5, 5, 0)); desc.OcShape = desc.Shape.GetBRep(); desc.Vertices = desc.OcShape.Vertices(); Assert.AreEqual(2, desc.Vertices.Count); desc.Edges = desc.OcShape.Edges(); Assert.AreEqual(3, desc.Edges.Count); desc.Faces = desc.OcShape.Faces(); Assert.AreEqual(3, desc.Faces.Count); return(desc); }
//-------------------------------------------------------------------------------------------------- ShapeDesc MakeBox() { var desc = new ShapeDesc { Shape = new Box() { DimensionX = 10, DimensionY = 10, DimensionZ = 10, } }; desc.Body = TestGeomGenerator.CreateBody(desc.Shape); desc.OcShape = desc.Shape.GetBRep(); desc.Vertices = desc.OcShape.Vertices(); Assert.AreEqual(8, desc.Vertices.Count); desc.Edges = desc.OcShape.Edges(); Assert.AreEqual(12, desc.Edges.Count); desc.Faces = desc.OcShape.Faces(); Assert.AreEqual(6, desc.Faces.Count); return(desc); }
/// <summary> /// Creates an actor using shape info and information from the parent BaseEntity. /// </summary> /// <param name="PhysicsScene">Reference to the physics scene</param> protected override void CreateActor(IPhysicsScene PhysicsScene) { ShapeDesc newShape = CreateShapeFromType(shapeType); if (newShape == null) { throw new Exception("Shape did not load properly"); } ActorDesc desc = new ActorDesc(); desc.Orientation = this.parentEntity.Rotation; desc.Mass = this.mass; desc.Dynamic = false; desc.AffectedByGravity = false; desc.Position = this.parentEntity.Position; desc.Shapes.Add(newShape); desc.EntityID = this.parentEntity.UniqueID; desc.Game = this.parentEntity.Game; desc.Type = ActorType.Water; this.actor = PhysicsScene.CreateActor(desc); }
/// <summary> /// Constructs a new physics actor. /// </summary> /// <param name="desc">Descriptor for the actor.</param> public StaticBEPUActor(ActorDesc desc) : base(desc) { // Build all shapes that make up the actor. for (int i = 0; i < desc.Shapes.Count; ++i) { ShapeDesc shapeDesc = desc.Shapes[i]; var tag = new EntityTag(this.ownerEntityID); if (shapeDesc is BoxShapeDesc) { var boxDesc = shapeDesc as BoxShapeDesc; this.body.PhysicsEntity = new Box(desc.Position, boxDesc.Extents.X, boxDesc.Extents.Y, boxDesc.Extents.Z); } else if (shapeDesc is SphereShapeDesc) { var sphereDesc = shapeDesc as SphereShapeDesc; this.body.PhysicsEntity = new Sphere(desc.Position, sphereDesc.Radius); } else if (shapeDesc is CapsuleShapeDesc) { var capDesc = shapeDesc as CapsuleShapeDesc; this.body.PhysicsEntity = new Capsule(desc.Position, capDesc.Length, capDesc.Radius); } else if (shapeDesc is CylinderShapeDesc) { var cylDesc = shapeDesc as CylinderShapeDesc; this.body.PhysicsEntity = new Cylinder(desc.Position, cylDesc.Height, cylDesc.Radius); } else if (shapeDesc is TriangleMeshShapeDesc) { var triDesc = shapeDesc as TriangleMeshShapeDesc; this.collidable = new StaticMesh(triDesc.Vertices.ToArray(), triDesc.Indices.ToArray(), new AffineTransform(Quaternion.CreateFromRotationMatrix(desc.Orientation), desc.Position)); this.collidable.Tag = tag; this.spaceObject = this.collidable; } else if (shapeDesc is HeightFieldShapeDesc) { throw new Exception("To load terrain physics you must use a TerrainBEPUActor."); } else { throw new Exception("Bad shape."); } if (null != this.body.PhysicsEntity) { SetMovable(false); this.body.PhysicsEntity.Tag = tag; this.body.PhysicsEntity.CollisionInformation.Tag = tag; this.spaceObject = this.body.PhysicsEntity; } } if (this.body.PhysicsEntity != null) { this.spaceObject = this.body.PhysicsEntity; this.body.PhysicsEntity.BecomeKinematic(); this.body.PhysicsEntity.IsAffectedByGravity = desc.Dynamic; } Debug.Assert(this.spaceObject != null, "A physics entity was not properly created."); }
public void CreateStatic(Vector3 pose, RigidBodyDescription description, Scene scene, ShapeDesc shape) { ActorDesc actorDesc = new ActorDesc(); actorDesc.GlobalPosition = pose; actorDesc.Body = null; description.ToNxActor(ref actorDesc); actorDesc.Shapes.Add(shape); Actor = scene.CreateActor(actorDesc); }