protected override void LoadContent() { model = Game.Content.Load <Model>("roomModels/CornerRoom"); body = new Body(); skin = new CollisionSkin(body); body.CollisionSkin = skin; triangleMesh = new TriangleMesh(); List <Vector3> vertexList = new List <Vector3>(); List <TriangleVertexIndices> indexList = new List <TriangleVertexIndices>(); ExtractModelData(vertexList, indexList, model); triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f); skin.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); Vector3 com = setMass(10.0f); body.MoveTo(position, Matrix.Identity); skin.ApplyLocalTransform(new JigLibX.Math.Transform(-com, Matrix.Identity)); body.Immovable = true; body.EnableBody(); }
public void setBody(Vector3 position) { _body = new Body(); _skin = new CollisionSkin(_body); _body.CollisionSkin = _skin; _body.ExternalData = new BodyExternalData(); ((BodyExternalData)_body.ExternalData).RenderableObject = this; rotMatrix = Matrix.CreateRotationX(rotation.X) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateRotationZ(rotation.Z); //Box box = new Box(position, rotMatrix, scale); Box box = new Box(position, Matrix.Identity, scale); // rotation is relative to body //collisionPrimitive = new Box(position, rotMatrix, scale); //_skin.AddPrimit ive(collisionPrimitive, new MaterialProperties(0.8f, 0.8f, 0.7f)); // why 2 primitives? _skin.AddPrimitive(box, (int)MaterialTable.MaterialID.NotBouncySmooth); Vector3 com = SetMass(1.0f); _body.MoveTo(position, rotMatrix); _skin.ApplyLocalTransform(new JigLibX.Math.Transform(-com, Matrix.Identity)); _body.EnableBody(); }
public BowlingPin(Game game, Model model, Matrix orientation, Vector3 position) : base(game, model) { body = new Body(); collision = new CollisionSkin(body); // add a capsule for the main corpus Primitive capsule = new Capsule(Vector3.Zero, Matrix.Identity, 0.1f, 1.3f); // add a small box at the buttom Primitive box = new Box(new Vector3(-0.1f, -0.1f, -0.1f), Matrix.Identity, Vector3.One * 0.2f); // add a sphere in the middle Primitive sphere = new Sphere(new Vector3(0.0f, 0.0f, 0.3f), 0.3f); collision.AddPrimitive(capsule, new MaterialProperties(0.1f, 0.5f, 0.5f)); collision.AddPrimitive(box, new MaterialProperties(0.1f, 0.5f, 0.5f)); collision.AddPrimitive(sphere, new MaterialProperties(0.1f, 0.5f, 0.5f)); body.CollisionSkin = this.collision; Vector3 com = SetMass(0.5f); body.MoveTo(position, orientation); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); this.scale = Vector3.One * 10.0f; }
public BowlingPin(Game game, Model model, Matrix orientation, Vector3 position) : base(game, model) { Body = new Body(); Collision = new CollisionSkin(Body); Primitive capsule = new Capsule(Vector3.Zero, Matrix.Identity, 0.1f, 1.3f); Primitive box = new Box(new Vector3(-0.1f, -0.1f, -0.1f), Matrix.Identity, Vector3.One * 0.2f); Primitive sphere = new Sphere(new Vector3(0.0f, 0.0f, 0.3f), 0.3f); Collision.AddPrimitive(capsule, new MaterialProperties(0.1f, 0.5f, 0.5f)); Collision.AddPrimitive(box, new MaterialProperties(0.1f, 0.5f, 0.5f)); Collision.AddPrimitive(sphere, new MaterialProperties(0.1f, 0.5f, 0.5f)); Body.CollisionSkin = Collision; var com = SetMass(0.5f); Body.MoveTo(position, orientation); Collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); Body.EnableBody(); Scale = Vector3.One * 10.0f; }
public CylinderObject(Game game, float radius, float length, Vector3 position, Model model) : base(game, model) { body = new Body(); collision = new CollisionSkin(body); if (length - 2.0f * radius < 0.0f) { throw new ArgumentException("Radius must be at least half length"); } Capsule middle = new Capsule(Vector3.Zero, Matrix.Identity, radius, length - 2.0f * radius); float sideLength = 2.0f * radius / (float)Math.Sqrt(2.0d); Vector3 sides = new Vector3(-0.5f * sideLength, -0.5f * sideLength, -radius); Box supply0 = new Box(sides, Matrix.Identity, new Vector3(sideLength, sideLength, length)); Box supply1 = new Box(Vector3.Transform(sides, Matrix.CreateRotationZ(MathHelper.PiOver4)), Matrix.CreateRotationZ(MathHelper.PiOver4), new Vector3(sideLength, sideLength, length)); collision.AddPrimitive(middle, new MaterialProperties(0.8f, 0.8f, 0.7f)); collision.AddPrimitive(supply0, new MaterialProperties(0.8f, 0.8f, 0.7f)); collision.AddPrimitive(supply1, new MaterialProperties(0.8f, 0.8f, 0.7f)); body.CollisionSkin = this.collision; Vector3 com = SetMass(1.0f); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); #region Manually set body inertia float cylinderMass = body.Mass; float comOffs = (length - 2.0f * radius) * 0.5f;; float Ixx = 0.5f * cylinderMass * radius * radius + cylinderMass * comOffs * comOffs; float Iyy = 0.25f * cylinderMass * radius * radius + (1.0f / 12.0f) * cylinderMass * length * length + cylinderMass * comOffs * comOffs; float Izz = Iyy; body.SetBodyInertia(Ixx, Iyy, Izz); #endregion body.MoveTo(position, Matrix.CreateRotationX(MathHelper.PiOver2)); body.EnableBody(); this.scale = new Vector3(radius, radius, length * 0.5f); }
internal void FinalizeBody() { try { Vector3 com = SetMass(1.0f); //body.MoveTo(Position, Orientation); Skin.ApplyLocalTransform(new JigLibX.Math.Transform(-com, Matrix.Identity)); } catch (Exception E) { System.Diagnostics.Debug.WriteLine(E.StackTrace); } }
public BoxObject(Game game, Model model, Vector3 sideLengths, Matrix orientation, Vector3 position) : base(game, model) { Body = new Body(); Collision = new CollisionSkin(Body); Collision.AddPrimitive(new Box(-0.5f * sideLengths, orientation, sideLengths), new MaterialProperties(0.8f, 0.8f, 0.7f)); Body.CollisionSkin = Collision; var com = SetMass(1.0f); Body.MoveTo(position, Matrix.Identity); Collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); Body.EnableBody(); Scale = sideLengths; }
public BoxObject(Vector3 sideLengths, Matrix orientation, Vector3 position) { body = new Body(); collision = new CollisionSkin(body); //Elasticity = e,StaticRoughness = sr,DynamicRoughness = dr; collision.AddPrimitive(new Box(-0.5f * sideLengths, orientation, sideLengths), new MaterialProperties(0.8f, 0.8f, 0.7f)); body.CollisionSkin = this.collision; Vector3 com = SetMass(1.0f); body.MoveTo(position, Matrix.Identity); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); this.scale = sideLengths; }
public CapsuleObject(Game game, Model model, float radius, float length, Matrix orientation, Vector3 position) : base(game, model) { Body = new Body(); Collision = new CollisionSkin(Body); Collision.AddPrimitive(new Capsule(Vector3.Transform(new Vector3(-0.5f, 0, 0), orientation), orientation, radius, length), (int)MaterialTable.MaterialID.BouncyNormal); Body.CollisionSkin = Collision; var com = SetMass(10.0f); Body.MoveTo(position + com, Matrix.Identity); Collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); Body.EnableBody(); Scale = new Vector3(radius, radius, length / 2); }
public CapsuleObject(float radius, float length, Matrix orientation, Vector3 position) { body = new Body(); collision = new CollisionSkin(body); collision.AddPrimitive(new Capsule(Vector3.Transform(new Vector3(-0.5f, 0, 0), orientation), orientation, radius, length), new MaterialProperties(0.8f, 0.7f, 0.6f)); body.CollisionSkin = this.collision; Vector3 com = SetMass(10.0f); body.MoveTo(position + com, Matrix.Identity); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); this.scale = new Vector3(radius, radius, length / 2); }
public virtual void Enable(bool bImmovable, float mass) { this.mass = mass; //set whether the object can move body.Immovable = bImmovable; //calculate the centre of mass Vector3 com = SetMass(mass); //adjust skin so that it corresponds to the 3D mesh as drawn on screen body.MoveTo(Transform3D.Translation, Matrix.Identity); //set the centre of mass collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); //enable so that any applied forces (e.g. gravity) will affect the object body.EnableBody(); }
static Body CreateCube(Vector3 pos, Vector3 size) { Body _body = new Body(); CollisionSkin _skin = new CollisionSkin(_body); _body.CollisionSkin = _skin; Box box = new Box(pos, Matrix4.Identity, size); _skin.AddPrimitive(box, new MaterialProperties(0.8f, 0.8f, 0.7f)); Vector3 com = SetMass(1.0f, _skin, _body); _body.MoveTo(pos, Matrix4.Identity); _skin.ApplyLocalTransform(new Transform(-com, Matrix4.Identity)); _body.EnableBody(); return(_body); }
public CylinderObject(Game game, float radius, float length, Vector3 position, Model model) : base(game, model) { Body = new Body(); Collision = new CollisionSkin(Body); if (length - 2.0f * radius < 0.0f) { throw new ArgumentException("Radius must be at least half length"); } var middle = new Capsule(Vector3.Zero, Matrix.Identity, radius, length - 2.0f * radius); var sideLength = 2.0f * radius / (float)Math.Sqrt(2.0d); var sides = new Vector3(-0.5f * sideLength, -0.5f * sideLength, -radius); var supply0 = new Box(sides, Matrix.Identity, new Vector3(sideLength, sideLength, length)); var supply1 = new Box(Vector3.Transform(sides, Matrix.CreateRotationZ(MathHelper.PiOver4)), Matrix.CreateRotationZ(MathHelper.PiOver4), new Vector3(sideLength, sideLength, length)); Collision.AddPrimitive(middle, new MaterialProperties(0.8f, 0.8f, 0.7f)); Collision.AddPrimitive(supply0, new MaterialProperties(0.8f, 0.8f, 0.7f)); Collision.AddPrimitive(supply1, new MaterialProperties(0.8f, 0.8f, 0.7f)); Body.CollisionSkin = Collision; var com = SetMass(1.0f); Collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); var cylinderMass = Body.Mass; var comOffs = (length - 2.0f * radius) * 0.5f; var ixx = 0.5f * cylinderMass * radius * radius + cylinderMass * comOffs * comOffs; var iyy = 0.25f * cylinderMass * radius * radius + 1.0f / 12.0f * cylinderMass * length * length + cylinderMass * comOffs * comOffs; var izz = iyy; Body.SetBodyInertia(ixx, iyy, izz); Body.MoveTo(position, Matrix.CreateRotationX(MathHelper.PiOver2)); Body.EnableBody(); Scale = new Vector3(radius, radius, length * 0.5f); }
public PlayerPhysicsObject(Vector3 size, Matrix orientation, Vector3 pos) { body = new Body(); collision = new CollisionSkin(body); collision.AddPrimitive(new Sphere(pos, size.Y), (int)MaterialTable.MaterialID.NotBouncyNormal); body.CollisionSkin = this.collision; Vector3 com = SetMass(1.0f); body.MoveTo(pos, Matrix.Identity); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); //body.SetBodyInvInertia(0.0f, 0.0f, 0.0f); body.AllowFreezing = false; }
public Cuboid(Vector dimension) { Dimension = dimension; PhysicsBody = new Body(); collisionSkin = new CollisionSkin(PhysicsBody); PhysicsBody.CollisionSkin = collisionSkin; var box = new Box(Vector3.Zero, Microsoft.Xna.Framework.Matrix.Identity, Dimension.ToXna()); collisionSkin.AddPrimitive(box, new MaterialProperties(0.1f, 0.4f, 0.9f)); var com = ApplyMass(1.0f); // PhysicsBody.MoveTo(Utils.VectorBalderToXna(this.Position), Microsoft.Xna.Framework.Matrix.Identity); collisionSkin.ApplyLocalTransform(new JigLibX.Math.Transform(-com, Microsoft.Xna.Framework.Matrix.Identity)); PhysicsBody.EnableBody(); }
public TowerPhysicsObject(Vector3 size, Matrix orientation, Vector3 pos) { body = new Body(); collision = new CollisionSkin(body); collision.AddPrimitive(new Box(Vector3.Zero, orientation, size * 2), new MaterialProperties(e: 0.8f, sr: 0.8f, dr: 0.7f)); body.CollisionSkin = this.collision; Vector3 com = SetMass(1.0f); body.MoveTo(pos, Matrix.Identity); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); body.Immovable = true; }
public BoxActor(Game game, Vector3 position, Vector3 scale) : base(game) { this.position = position; this.scale = scale; _body = new Body(); _skin = new CollisionSkin(_body); _body.CollisionSkin = _skin; Box box = new Box(Vector3.Zero, Matrix.Identity, scale); _skin.AddPrimitive(box, new MaterialProperties(0.8f, 0.8f, 0.7f)); Vector3 com = SetMass(1.0f); _body.MoveTo(position, Matrix.Identity); _skin.ApplyLocalTransform(new JigLibX.Math.Transform(-com, Matrix.Identity)); _body.EnableBody(); }
/// <summary> /// Updates the center of mass and the mass properties of the collision skin. /// </summary> protected void UpdateMass() { //Sets the mass properties of the object and transforms the collision skin according //to the returned center of mass. Vector3 newCOM = SetMassProperties(mDensity); //We only update the center of mass if it has changed. if (!newCOM.Equals(mCenterOfMass)) { mCenterOfMass = newCOM; mCollisionSkin.ApplyLocalTransform(new Transform(-mCenterOfMass, Matrix.Identity)); //The center of mass changed so we trigger the CenterOfMassChanged event. if (CenterOfMassChanged != null) { CenterOfMassChanged(this, new EventArgs()); } } }
public TriangleMeshObject(Game game, Model model, Matrix orientation, Vector3 position) : base(game, model) { Body = new Body(); Collision = new CollisionSkin(null); triangleMesh = new TriangleMesh(); var vertexList = new List <Vector3>(); var indexList = new List <TriangleVertexIndices>(); ExtractData(vertexList, indexList, model); triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f); Collision.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Collision); Collision.ApplyLocalTransform(new JigLibX.Math.Transform(position, orientation)); Body.MoveTo(position, orientation); }
public TriangleMeshObject(Game game, Model model, Matrix orientation, Vector3 position) : base(game, model) { body = new Body(); collision = new CollisionSkin(null); triangleMesh = new TriangleMesh(); List <Vector3> vertexList = new List <Vector3>(); List <TriangleVertexIndices> indexList = new List <TriangleVertexIndices>(); ExtractData(vertexList, indexList, model); triangleMesh.CreateMesh(vertexList, indexList, 4, 1.0f); collision.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(collision); // Transform collision.ApplyLocalTransform(new JigLibX.Math.Transform(position, orientation)); // we also need to move this dummy, so the object is *rendered* at the correct positiob body.MoveTo(position, orientation); }
void updatePosition() { _skin.ApplyLocalTransform(new JigLibX.Math.Transform(position, rotMatrix)); }
public XBoneMapObject(Vector3 position, ref XModel model) { //create a list to hold the collision primitives List <Primitive> prims = new List <Primitive>(); //parse for collision bones foreach (ModelBone bone in model.Model.Bones) { string[] keypairs = bone.Name.ToLower().Split('_'); //if checks for valid naming convention on collision bones if ((keypairs.Length == 2) && ((keypairs[0] == "sphere") || (keypairs[0] == "box") || (keypairs[0] == "capsule"))) { //determine object number int objectnum; if ((keypairs[1] != "") || (keypairs[1] != null)) { objectnum = int.Parse(keypairs[1]); } else { objectnum = 0; } //decompose bone transforms to components Vector3 pos; Vector3 scale; Quaternion qrot; bone.Transform.Decompose(out scale, out qrot, out pos); Matrix rot = Matrix.CreateFromQuaternion(qrot); //create collision primitive objects and add to list switch (keypairs[0]) { case ("sphere"): JigLibX.Geometry.Sphere sph = new JigLibX.Geometry.Sphere(pos, scale.X); prims.Add(sph); break; case ("box"): Box box = new Box(pos, rot, scale); prims.Add(box); break; case ("capsule"): break; } } } body = new Body(); collision = new CollisionSkin(body); if (prims.Count > 0) { foreach (Primitive prim in prims) { //TODO: Add ability to specify physics material type in art editor somehow collision.AddPrimitive(prim, (int)JigLibX.Collision.MaterialTable.MaterialID.NormalSmooth); } } else {//no collision prims detected from XSI so create a default one here using the mesh bounding spheres foreach (ModelMesh mesh in model.Model.Meshes) { //collision.AddPrimitive(new JigLibX.Geometry.Sphere(mesh.BoundingSphere.Center, mesh.BoundingSphere.Radius), new MaterialProperties(0.8f, 0.8f, 0.7f)); collision.AddPrimitive(new JigLibX.Geometry.Box(position, Matrix.Identity, new Vector3(mesh.BoundingSphere.Radius / 2f)), (int)JigLibX.Collision.MaterialTable.MaterialID.NormalRough); } } body.CollisionSkin = this.collision; Vector3 com = SetMass(1.0f); body.MoveTo(position, Matrix.Identity); collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity)); body.EnableBody(); }
public Planet(Vector3 center, Vector3 radius, double maxDeviation, double radianMeshSize, int subdivides, GraphicsDevice g, Texture2D texture) : base() { this.texture = texture; Body = new Body(); Skin = new CollisionSkin(null); for (double i = 0; i < MathHelper.TwoPi - JigLibX.Math.JiggleMath.Epsilon; i += radianMeshSize) { for (double j = 0; j < MathHelper.TwoPi - JigLibX.Math.JiggleMath.Epsilon; j += radianMeshSize) { //TriangleMesh tm = CreateSphericalMeshSegment(i, i + radianMeshSize, j, j + radianMeshSize, trianglesPerMesh, maxDeviation); TriangleMesh tm = new TriangleMesh(); List <Vector3> vl = new List <Vector3>(); // Polar -> Cartesion in 3-Spcae // X = R * Sin(Θ) * Cos(ϕ) // Y = R * Sin(Θ) * Sin(ϕ) // Z = R * Cos(Θ) vl.Add(SphericalToCartesian(i, j, radius)); vl.Add(SphericalToCartesian(i + radianMeshSize, j, radius)); vl.Add(SphericalToCartesian(i + radianMeshSize, j + radianMeshSize, radius)); vl.Add(SphericalToCartesian(i, j + radianMeshSize, radius)); List <TriangleVertexIndices> tvi = new List <TriangleVertexIndices>(); tvi.Add(new TriangleVertexIndices(0, 1, 2)); tvi.Add(new TriangleVertexIndices(1, 3, 2)); VertexPositionNormalTexture[] v = new VertexPositionNormalTexture[4]; Vector3 tmp = SphericalToCartesian(i, j, Vector3.One); v[0].Position = tmp * radius; v[0].Normal = Vector3.Zero; v[0].TextureCoordinate.X = tmp.X; v[0].TextureCoordinate.Y = tmp.Y; tmp = SphericalToCartesian(i + radianMeshSize, j, Vector3.One); v[1].Position = tmp * radius; v[1].Normal = Vector3.Zero; v[1].TextureCoordinate.X = tmp.X; v[1].TextureCoordinate.Y = tmp.Y; tmp = SphericalToCartesian(i + radianMeshSize, j + radianMeshSize, Vector3.One); v[2].Position = tmp * radius; v[2].Normal = Vector3.Zero; v[2].TextureCoordinate.X = tmp.X; v[2].TextureCoordinate.Y = tmp.Y; tmp = SphericalToCartesian(i, j + radianMeshSize, Vector3.One); v[3].Position = tmp * radius; v[3].Normal = Vector3.Zero; v[3].TextureCoordinate.X = tmp.X; v[3].TextureCoordinate.Y = tmp.Y; verts.Add(v); indices.Add(new int[] { 0, 1, 2, 1, 3, 2 }); SetNormalOfTriangleAtIndices(0, 1, 2, verts.Count - 1); SetNormalOfTriangleAtIndices(1, 3, 2, verts.Count - 1); tm.CreateMesh(vl, tvi, 1, 1f); // Last two parameters are listed as not used on the octree triangleMeshes.Add(tm); Skin.AddPrimitive(tm, (int)MaterialTable.MaterialID.NormalRough); } } List <Vector3> vertexList = new List <Vector3>(); List <TriangleVertexIndices> indexList = new List <TriangleVertexIndices>(); Effect = new BasicEffect(g); //ExtractData(vertexList, indexList, model); //triangleMesh.CreateMesh(vertexList,indexList, 4, 1.0f); //Skin.AddPrimitive(triangleMesh, new MaterialProperties(0.8f, 0.7f, 0.6f)); PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(Skin); // Transform Skin.ApplyLocalTransform(new JigLibX.Math.Transform(center, Matrix.Identity)); // we also need to move this dummy, so the object is *rendered* at the correct positiob Body.MoveTo(center, Matrix.Identity); CommonInit(center, new Vector3(1, 1, 1), null, false, -1); }