Represents the collision part of the RigidBody. A shape is mainly definied through it's supportmap. Shapes represent convex objects. Inherited classes have to overwrite the supportmap function. To implement you own shape: derive a class from Shape, implement the support map function and call 'UpdateShape' within the constructor. GeometricCenter, Mass, BoundingBox and Inertia is calculated numerically based on your SupportMap implementation.
Inheritance: ISupportMappable
Ejemplo n.º 1
0
        public void AddShape(Shape shape)
        {
            if (shape is Multishape) throw new Exception("Multishapes not supported by MinkowskiSumShape.");
            shapes.Add(shape);

            this.UpdateShape();
        }
Ejemplo n.º 2
0
 public bool Remove(Shape shape)
 {
     if (shapes.Count == 1) throw new Exception("There must be at least one shape.");
     bool result = shapes.Remove(shape);
     UpdateShape();
     return result;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of the TransformedShape struct.
 /// </summary>
 /// <param name="shape">The shape.</param>
 /// <param name="orientation">The orientation this shape should have.</param>
 /// <param name="position">The position this shape should have.</param>
 public TransformedShape(Shape shape, JMatrix orientation, JVector position)
 {
     this.position = position;
     this.orientation = orientation;
     JMatrix.Transpose(ref orientation, out invOrientation);
     this.shape = shape;
 }
Ejemplo n.º 4
0
 internal void Build(bool isConvexHull, List<JVector> vertices, List<TriangleVertexIndices> indices)
 {
     if (isConvexHull)
     {
         Shape = new ConvexHullShape(vertices);
     }
     else
     {
         Shape = new TriangleMeshShape(new Octree(vertices, indices));
     }
 }
Ejemplo n.º 5
0
        //World.WorldStep postStep;

        /// <summary>
        /// Initializes a new instance of the DefaultCar class.
        /// </summary>
        /// <param name="world">The world the car should be in.</param>
        /// <param name="shape">The shape of the car. Recommend is a box shape.</param>
        public DefaultCar(World world,Shape shape) : base(shape)
        {
            this.world = world;
            //postStep = new World.WorldStep(world_PostStep);

            //world.Events.PostStep += postStep;

            // set some default values
            this.AccelerationRate = 5.0f;
            this.SteerAngle = 20.0f;
            this.DriveTorque = 50.0f;
            this.SteerRate = 5.0f;

            // create default wheels
            wheels[(int)WheelPosition.FrontLeft] = new Wheel(world, this, JVector.Left + 1.8f * JVector.Forward + 0.8f * JVector.Down,0.4f);
            wheels[(int)WheelPosition.FrontRight] = new Wheel(world, this, JVector.Right + 1.8f * JVector.Forward + 0.8f * JVector.Down, 0.4f);
            wheels[(int)WheelPosition.BackLeft] = new Wheel(world, this, JVector.Left + 1.8f * JVector.Backward + 0.8f * JVector.Down, 0.4f);
            wheels[(int)WheelPosition.BackRight] = new Wheel(world, this, JVector.Right + 1.8f * JVector.Backward + 0.8f * JVector.Down, 0.4f);

            AdjustWheelValues();
        }
Ejemplo n.º 6
0
		private void CreateBoxShape()
		{
			Vector3D size = Shape.Size;
			jitterShape = new BoxShape(size.Y, size.Z, size.X);
		}
Ejemplo n.º 7
0
		private void CreateSphereShape()
		{
			jitterShape = new SphereShape(Shape.Radius);
		}
Ejemplo n.º 8
0
		private void CreateTerrainShape()
		{
			jitterShape = new TerrainShape(
				ArrayExtensions.GetWithDefault<PhysicsShape.PropertyType, float[,]>(Shape.Properties,
					PhysicsShape.PropertyType.Height),
				ArrayExtensions.GetWithDefault<PhysicsShape.PropertyType, float>(Shape.Properties,
					PhysicsShape.PropertyType.ScaleX),
				ArrayExtensions.GetWithDefault<PhysicsShape.PropertyType, float>(Shape.Properties,
					PhysicsShape.PropertyType.ScaleY));
		}
Ejemplo n.º 9
0
		private void CreateMeshShape()
		{
			jitterShape = new TriangleMeshShape(
				new Octree(new List<JVector>(), new List<TriangleVertexIndices>()));
		}
 /// <summary>
 /// Creates a new instance of the TransformedShape struct.
 /// </summary>
 /// <param name="shape">The shape.</param>
 /// <param name="orientation">The orientation this shape should have.</param>
 /// <param name="position">The position this shape should have.</param>
 public TransformedShape(Shape shape, JMatrix orientation, JVector position)
 {
     this.position = position;
     this.orientation = orientation;
     JMatrix.Transpose(ref orientation, out invOrientation);
     this.shape = shape;
     this.boundingBox = new JBBox();
     UpdateBoundingBox();
 }
Ejemplo n.º 11
0
		private void CreateCylinderShape()
		{
			jitterShape = new CylinderShape(Shape.Height, Shape.Radius);
		}
        private void DrawShape(Shape shape, JMatrix ori, JVector pos)
        {
            Model model = null;
            Matrix scaleMatrix = Matrix.Identity;

            if (shape is TriangleMeshShape)
            {
                model = models[(int)Models.triangle];
            }
            else if (shape is BoxShape)
            {
                model = models[(int)Models.box];
                scaleMatrix = Matrix.CreateScale(Conversion.ToXNAVector((shape as BoxShape).Size));
            }
            else if (shape is SphereShape)
            {
                model = models[(int)Models.sphere];
                scaleMatrix = Matrix.CreateScale((shape as SphereShape).Radius);
            }
            else if (shape is CylinderShape)
            {
                model = models[(int)Models.cylinder];
                CylinderShape cs = shape as CylinderShape;
                scaleMatrix = Matrix.CreateScale(cs.Radius, cs.Height, cs.Radius);
            }
            else if (shape is CapsuleShape)
            {
                model = models[(int)Models.capsule];

            }
            else if (shape is ConeShape)
            {
                ConeShape cs = shape as ConeShape;
                scaleMatrix = Matrix.CreateScale(cs.Radius * 2.0f, cs.Height, cs.Radius * 2.0f);
                model = models[(int)Models.cone];
            }

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = transforms[mesh.ParentBone.Index] * scaleMatrix * Conversion.ToXNAMatrix(ori) *
                        Matrix.CreateTranslation(Conversion.ToXNAVector(pos));
                }
                mesh.Draw();
            }
        }
Ejemplo n.º 13
0
Archivo: Game1.cs Proyecto: scy7he/Pong
        private void AddShapeToDrawList(Shape shape, JMatrix ori, JVector pos)
        {
            GeometricPrimitive primitive = null;
            Matrix scaleMatrix = Matrix.Identity;

            if (shape is BoxShape)
            {
                primitive = primitives[(int)Primitives.box];
                scaleMatrix = Matrix.CreateScale(Conversion.ToXNAVector((shape as BoxShape).Size));
            }
            else if (shape is SphereShape)
            {
                primitive = primitives[(int)Primitives.sphere];
                scaleMatrix = Matrix.CreateScale((shape as SphereShape).Radius);
            }
            else if (shape is CylinderShape)
            {
                primitive = primitives[(int)Primitives.cylinder];
                CylinderShape cs = shape as CylinderShape;
                scaleMatrix = Matrix.CreateScale(cs.Radius, cs.Height, cs.Radius);
            }
            else if (shape is CapsuleShape)
            {
                primitive = primitives[(int)Primitives.capsule];
                CapsuleShape cs = shape as CapsuleShape;
                scaleMatrix = Matrix.CreateScale(cs.Radius * 2, cs.Length, cs.Radius * 2);

            }
            else if (shape is ConeShape)
            {
                ConeShape cs = shape as ConeShape;
                scaleMatrix = Matrix.CreateScale(cs.Radius, cs.Height, cs.Radius);
                primitive = primitives[(int)Primitives.cone];
            }

            if (primitive != null)
                primitive.AddWorldMatrix(scaleMatrix * Conversion.ToXNAMatrix(ori) *
                            Matrix.CreateTranslation(Conversion.ToXNAVector(pos)));
        }
Ejemplo n.º 14
0
 public MassPoint(Shape shape, SoftBody owner, Material material)
     : base(shape, material)
 {
     this.isMassPoint = true; this.SoftBody = owner;
     useShapeMassProperties = false;
 }
Ejemplo n.º 15
0
 public PhysicsBody(Shape shape)
 {
     this.Body = new Jitter.Dynamics.RigidBody(shape);
 }
Ejemplo n.º 16
0
        public static ItemComponent CreateNewItem(MessageProvider messageProvider, GameState state,
            string name, string imageLocation, string description,
            string modelPath, Vector2i size, Vector3 offset, Quaternion rotation, Shape shape, ItemLocation location,
            AttackClass attackClasses, ItemUsage itemUsages, Protection protection, Material physicsMaterial,
            float mass, float healthDelta, float usageDeltaPerUsage, float attackStrength, float throwPower, float usage)
        {
            var entity = EntityFactory.Instance.CreateWith(name, messageProvider,
                systems: new[] { typeof(ItemSystem), typeof(ModelSystem), typeof(PhysicsSystem) });

            var item = entity.GetComponent<ItemComponent>();
            item.ImageLocation = imageLocation;
            item.Description = description;
            item.Size = size;
            item.Location = location;
            item.AttackClasses = attackClasses;
            item.ItemUsages = itemUsages;
            item.Protection = protection;
            item.HealthDelta = healthDelta;
            item.AttackStrength = attackStrength;
            item.ThrowPower = throwPower;
            item.Usage = usage;
            item.UsageDeltaPerUsage = usageDeltaPerUsage;
            item.Mass = mass;
            item.PhysicsMaterial = physicsMaterial;
            item.PositionOffset = offset;
            item.Rotation = rotation;
            item.ItemUsageHandler = new MazeItemUseHandler();

            var model = new ModelSceneObject(modelPath);
            model.Enabled = true;
            state.Scene.AddObject(model);
            entity.GetComponent<ModelComponent>().Model = model;

            var transform = entity.GetComponent<TransformComponent>();
            var physics = entity.GetComponent<PhysicsComponent> ();

            if (shape == null)
            {
                List<JVector> vertices = new List<JVector>();
                model.Model.Meshes[0].Vertices.ForEach(x => vertices.Add(x.ToJitterVector()));

                List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();

                for(int i = 0; i < model.Model.Meshes[0].Indices.Length; i+= 3)
                {
                    int i0 = model.Model.Meshes[0].Indices[i+0];
                    int i1 = model.Model.Meshes[0].Indices[i+1];
                    int i2 = model.Model.Meshes[0].Indices[i+2];

                    indices.Add(new TriangleVertexIndices(i0, i1, i2));
                }

                shape = new TriangleMeshShape(new Octree(vertices, indices));
            }

            var body = new RigidBody(shape);
            body.Position = transform.Position.ToJitterVector ();
            if (mass >= 0)
                body.Mass = mass;
            body.Material = physicsMaterial;
            body.AllowDeactivation = true;
            body.Tag = entity;

            state.PhysicsManager.World.AddBody(body);
            physics.RigidBody = body;
            physics.World = state.PhysicsManager.World;
            physics.PhysicsApplying = AffectedByPhysics.Orientation | AffectedByPhysics.Position;
            physics.RigidBody.IsStatic = false;
            physics.RigidBody.IsActive = false;
            physics.RigidBody.Position = transform.Position.ToJitterVector();
            model.Position = transform.Position;

            return item;
        }
Ejemplo n.º 17
0
		private void CreateCapsuleShape()
		{
			jitterShape = new CapsuleShape(Shape.Depth, Shape.Radius);
		}
Ejemplo n.º 18
0
        public void setPhysMesh(Shape newShape)
        {
            RigidBody newBody = new RigidBody(newShape);

            newBody.Position = GenericMethods.FromOpenTKVector(Position);
            newBody.Mass = 20f;

            newBody.Tag = this;
            newBody.Orientation = GenericMethods.FromOpenTKMatrix(Orientation);

            Body = newBody;
            Scene.world.AddBody(newBody);
        }
Ejemplo n.º 19
0
		private void CreateConeShape()
		{
			jitterShape = new ConeShape(Shape.Height, Shape.Radius);
		}
Ejemplo n.º 20
0
 private void SupportMapping(RigidBody body, Shape workingShape, ref JVector direction, out JVector result)
 {
     JVector.Transform(ref direction, ref body.invOrientation, out result);
     workingShape.SupportMapping(ref result, out result);
     JVector.Transform(ref result, ref body.orientation, out result);
     JVector.Add(ref result, ref body.position, out result);
 }
Ejemplo n.º 21
0
        public static double CalculateMassInertia(Shape shape, out JVector centerOfMass,
            out JMatrix inertia)
        {
            double mass = 0.0f;
            centerOfMass = JVector.Zero; inertia = JMatrix.Zero;

            if (shape is Multishape) throw new ArgumentException("Can't calculate inertia of multishapes.", "shape");

            // build a triangle hull around the shape
            List<JVector> hullTriangles = new List<JVector>();
            shape.MakeHull(ref hullTriangles, 3);

            // create inertia of tetrahedron with vertices at
            // (0,0,0) (1,0,0) (0,1,0) (0,0,1)
            double a = 1.0f / 60.0f, b = 1.0f / 120.0f;
            JMatrix C = new JMatrix(a, b, b, b, a, b, b, b, a);

            for (int i = 0; i < hullTriangles.Count; i += 3)
            {
                JVector column0 = hullTriangles[i + 0];
                JVector column1 = hullTriangles[i + 1];
                JVector column2 = hullTriangles[i + 2];

                JMatrix A = new JMatrix(column0.X, column1.X, column2.X,
                    column0.Y, column1.Y, column2.Y,
                    column0.Z, column1.Z, column2.Z);

                double detA = A.Determinant();

                // now transform this canonical tetrahedron to the target tetrahedron
                // inertia by a linear transformation A
                JMatrix tetrahedronInertia = JMatrix.Multiply(A * C * JMatrix.Transpose(A), detA);

                JVector tetrahedronCOM = (1.0f / 4.0f) * (hullTriangles[i + 0] + hullTriangles[i + 1] + hullTriangles[i + 2]);
                double tetrahedronMass = (1.0f / 6.0f) * detA;

                inertia += tetrahedronInertia;
                centerOfMass += tetrahedronMass * tetrahedronCOM;
                mass += tetrahedronMass;
            }

            inertia = JMatrix.Multiply(JMatrix.Identity, inertia.Trace()) - inertia;
            centerOfMass = centerOfMass * (1.0f / mass);

            double x = centerOfMass.X;
            double y = centerOfMass.Y;
            double z = centerOfMass.Z;

            // now translate the inertia by the center of mass
            JMatrix t = new JMatrix(
                -mass * (y * y + z * z), mass * x * y, mass * x * z,
                mass * y * x, -mass * (z * z + x * x), mass * y * z,
                mass * z * x, mass * z * y, -mass * (x * x + y * y));

            JMatrix.Add(ref inertia, ref t, out inertia);

            return mass;
        }
        private void BuildScene()
        {
            const float cockpitWidth = 0.15f;
            const float cockpitLength = 0.3f;
            const float cockpitHeight = 0.3f;
            //            const float skidLength = 0.15f;
            //            const float skidDiameter = 0.005f;

            //            var cockpitBoxShape = new BoxShape(new JVector(cockpitWidth, cockpitHeight, cockpitLength));
            //
            //
            //            var skidCylinderShape = new CylinderShape(skidLength, skidDiameter/2);
            //            var rotX = Matrix.CreateRotationX(MathHelper.ToRadians(90));
            //            JMatrix jRotX = Conversion.ToJitterMatrix(rotX);
            //
            //            var leftSkid = new CompoundShape.TransformedShape(skidCylinderShape, jRotX, new JVector(-cockpitWidth/2 - skidDiameter/2, 0, -skidLength/2));
            //            var rightSkid = new CompoundShape.TransformedShape(skidCylinderShape, jRotX, new JVector(+cockpitWidth/2 + skidDiameter/2, 0, -skidLength/2));
            //            var cockpit = new CompoundShape.TransformedShape(cockpitBoxShape, JMatrix.Identity, new JVector(-cockpitWidth/2, 0, -cockpitLength/2));
            //            _helicopterShape = new CompoundShape(new[]
            //                                                        {
            //                                                            leftSkid, rightSkid, cockpit
            //                                                        });

            // TODO Add skids and rotor bounding boxes
            _helicopterShape = new BoxShape(new JVector(cockpitWidth, cockpitHeight, cockpitLength));

            HelicopterBody = new RigidBody(_helicopterShape);
            World.AddBody(HelicopterBody);
            HelicopterBody.Friction = 30;
            HelicopterBody.Mass = 2;

            World.SetDampingFactors(0.8f, 0.999f);

            SetHeightmap(_heightValues);

            // creating two boxShapes with different sizes
            //            Shape boxShape = new BoxShape(JVector.One);
            //            Shape groundShape = new BoxShape(new JVector(10, 1, 10));

            // create new instances of the rigid body class and pass
            // the boxShapes to them
            //            var boxBody1 = new RigidBody(boxShape);
            //            var boxBody2 = new RigidBody(boxShape);
            //
            //            boxBody1.Tag = Color.LightPink;
            //            boxBody2.Tag = Color.LightSkyBlue;

            //            var groundBody = new RigidBody(groundShape);
            //            groundBody.Tag = Color.LightGreen;

            // set the position of the box size=(1,1,1)
            // 2 and 5 units above the ground box size=(10,1,10)
            //            boxBody1.Position = new JVector(128, 50, 128);
            //            boxBody2.Position = new JVector(125, 50, 128);

            // make the body static, so it can't be moved
            //            groundBody.IsStatic = true;

            // add the bodies to the world.
            //            _world.AddBody(boxBody1);
            //            _world.AddBody(boxBody2);
            //            _world.AddBody(groundBody);
        }
Ejemplo n.º 23
0
        private void FindSupportPoints(RigidBody body1, RigidBody body2,
            Shape shape1, Shape shape2, ref JVector point, ref JVector normal,
            out JVector point1, out JVector point2)
        {
            JVector mn; JVector.Negate(ref normal, out mn);

            JVector sA; SupportMapping(body1, shape1, ref mn, out sA);
            JVector sB; SupportMapping(body2, shape2, ref normal, out sB);

            JVector.Subtract(ref sA, ref point, out sA);
            JVector.Subtract(ref sB, ref point, out sB);

            float dot1 = JVector.Dot(ref sA, ref normal);
            float dot2 = JVector.Dot(ref sB, ref normal);

            JVector.Multiply(ref normal, dot1, out sA);
            JVector.Multiply(ref normal, dot2, out sB);

            JVector.Add(ref point, ref sA, out point1);
            JVector.Add(ref point, ref sB, out point2);
        }
Ejemplo n.º 24
0
        public DetailObject CreateDetail(string model, Vector3 position, DetailObject parent = null, Shape shape = null)
        {
            var obj = new DetailObject(parent, ++this.detailCounter, shape)
            {
                Model = model,
                Position = position,
            };

            this.RegisterDetail(obj);

            return obj;
        }
Ejemplo n.º 25
0
 public MassPoint(Shape shape, SoftBody owner, Material material)
     : base(shape, material, true)
 {
     this.SoftBody = owner;
 }
        private void AddShape(Shape shape, JVector position, JVector velocity)
        {
            var body = new RigidBody(shape);
            body.Tag = Color.LightPink;
            body.Position = position;
            body.LinearVelocity = velocity;
            body.AngularVelocity = new JVector(0.5f);
            //            body.Restitution = 0.1f;  note: not implemented?

            // add the bodies to the world.
            World.AddBody(body);
        }