Exemple #1
0
 public AddBodyArgs(CollisionShapeType collisionShape, double radius, double height, double mass)
 {
     this.CollisionShape = collisionShape;
     _radius             = radius;
     _height             = height;
     this.Mass           = mass;
 }
 public void MakePointsFromPolyline(Vector2[] points)
 {
     m_CollisionShapeType = CollisionShapeType.Polyline;
     m_IsClosed           = false;
     m_Points             = points.Select(pt => pt + m_Position).ToArray();
     ApplyRotationToPoints();
 }
        // This must be called in order for rotation and position offset to by applied
        public void RenderPoints(SuperTile tile, GridOrientation orientation, Vector2 gridSize)
        {
            if (orientation == GridOrientation.Isometric)
            {
                m_Position = IsometricTransform(m_Position, tile, gridSize);
                m_Position.x += gridSize.x * 0.5f;

                for (int i = 0; i < m_Points.Length; i++)
                {
                    m_Points[i] = IsometricTransform(m_Points[i], tile, gridSize);
                }

                // Also, we are forced to use polygon colliders for isometric projection
                if (m_CollisionShapeType == CollisionShapeType.Ellipse || m_CollisionShapeType == CollisionShapeType.Rectangle)
                {
                    m_CollisionShapeType = CollisionShapeType.Polygon;
                }
            }

            // Burn rotation into our points
            ApplyRotationToPoints();

            // Burn translation into our points
            m_Points = m_Points.Select(p => p + m_Position).ToArray();

            // Transform all points so that they wrt the bottom-left of the tile
            // This should make calculations later easier since Tiled treats the bottom-left corner of a tile as the local origin
            m_Points = m_Points.Select(p => LocalTransform(p, tile)).ToArray();
            m_Position = LocalTransform(m_Position, tile);
        }
Exemple #4
0
 public void MakePoint()
 {
     m_CollisionShapeType = CollisionShapeType.Point;
     m_IsClosed           = false;
     m_Points             = new Vector2[1];
     m_Points[0]          = m_Position;
 }
        //NOTE:  I'm keeping the number of constructor overloads small, and instead making the user call one of the static creation methods.  Otherwise it would be very
        //confusing to know what you're creating

        protected CollisionHull(IntPtr handle, WorldBase world, float[] offsetMatrix, CollisionShapeType collisionShape)
        {
            _handle = handle;
            _world = world;
            _offsetMatrix = offsetMatrix;
            _collisionShape = collisionShape;

            ObjectStorage.Instance.AddCollisionHull(_handle, this);
        }
Exemple #6
0
        //NOTE:  I'm keeping the number of constructor overloads small, and instead making the user call one of the static creation methods.  Otherwise it would be very
        //confusing to know what you're creating

        protected CollisionHull(IntPtr handle, WorldBase world, float[] offsetMatrix, CollisionShapeType collisionShape)
        {
            _handle         = handle;
            _world          = world;
            _offsetMatrix   = offsetMatrix;
            _collisionShape = collisionShape;

            ObjectStorage.Instance.AddCollisionHull(_handle, this);
        }
 private static T GetCollisionShape <T>(ActorRoot actorRoot, CollisionShapeType shapeType) where T : VCollisionShape, new()
 {
     if (actorRoot.shape == null || actorRoot.shape.GetShapeType() != shapeType)
     {
         T result = Activator.CreateInstance <T>();
         result.Born(actorRoot);
         return(result);
     }
     return(actorRoot.shape as T);
 }
        private static T GetCollisionShape <T>(ActorRoot actorRoot, CollisionShapeType shapeType) where T : VCollisionShape, new()
        {
            if ((actorRoot.shape != null) && (actorRoot.shape.GetShapeType() == shapeType))
            {
                return(actorRoot.shape as T);
            }
            T local = Activator.CreateInstance <T>();

            local.Born(actorRoot);
            return(local);
        }
Exemple #9
0
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.AddBody == null)
                {
                    MessageBox.Show("There are no event listeners for this button", "Clear Clicked", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                AddBodyArgs args = null;

                CollisionShapeType shape = GetSelectedShape();
                switch (shape)
                {
                case CollisionShapeType.Box:
                case CollisionShapeType.Sphere:
                    #region x y z

                    double x, y, z, mass1;
                    GetRatiosMass(out x, out y, out z, out mass1, shape);

                    args = new AddBodyArgs(shape, new Vector3D(x, y, z), mass1);

                    #endregion
                    break;

                case CollisionShapeType.Capsule:
                case CollisionShapeType.ChamferCylinder:
                case CollisionShapeType.Cone:
                case CollisionShapeType.Cylinder:
                    #region radius height

                    double radius, height, mass2;
                    GetRatiosMass(out radius, out height, out mass2, shape);

                    args = new AddBodyArgs(shape, radius, height, mass2);

                    #endregion
                    break;

                default:
                    throw new ApplicationException("Unknown CollisionShapeType: " + shape.ToString());
                }

                // Raise the event
                this.AddBody(this, args);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Add Clicked", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemple #10
0
        public void MakePointsFromRectangle()
        {
            m_CollisionShapeType = CollisionShapeType.Rectangle;
            m_IsClosed           = true;

            // Make the points the give us a rectangle shape
            // Note: points are counter-clockwise
            m_Points    = new Vector2[4];
            m_Points[0] = Vector2.zero;
            m_Points[1] = new Vector2(0, m_Size.y);
            m_Points[2] = new Vector2(m_Size.x, m_Size.y);
            m_Points[3] = new Vector2(m_Size.x, 0);
        }
        public void MakePointsFromRectangle()
        {
            m_CollisionShapeType = CollisionShapeType.Rectangle;
            m_IsClosed           = true;

            // Make the points the give us a rectangle shape
            // Note: points are counter-clockwise
            m_Points    = new Vector2[4];
            m_Points[0] = new Vector2(m_Position.x, m_Position.y);
            m_Points[1] = new Vector2(m_Position.x, m_Position.y + m_Size.y);
            m_Points[2] = new Vector2(m_Position.x + m_Size.x, m_Position.y + m_Size.y);
            m_Points[3] = new Vector2(m_Position.x + m_Size.x, m_Position.y);
            ApplyRotationToPoints();
        }
Exemple #12
0
        public void MakePointsFromEllipse(int numEdges)
        {
            m_CollisionShapeType = CollisionShapeType.Ellipse;
            m_IsClosed           = true;

            // Estimate the ellipse with a polygon
            float theta  = ((float)Math.PI * 2.0f) / numEdges;
            float half_x = m_Size.x * 0.5f;
            float half_y = m_Size.y * 0.5f;

            m_Points = new Vector2[numEdges];
            for (int i = 0; i < numEdges; i++)
            {
                m_Points[i].x = half_x + half_x * Mathf.Cos(theta * i);
                m_Points[i].y = half_y + half_y * Mathf.Sin(theta * i);
            }
        }
Exemple #13
0
    public bool EdgeIntersects(VCollisionShape shape)
    {
        bool result = false;

        if (shape != null)
        {
            CollisionShapeType shapeType = shape.GetShapeType();
            if (shapeType == CollisionShapeType.Box)
            {
                result = this.EdgeIntersects((VCollisionBox)shape);
            }
            else if (shapeType == CollisionShapeType.CylinderSector)
            {
                result = this.EdgeIntersects((VCollisionCylinderSector)shape);
            }
            else
            {
                result = this.EdgeIntersects((VCollisionSphere)shape);
            }
        }
        return(result);
    }
    public VCollisionShape CreateShape()
    {
        DebugHelper.Assert(!Singleton <BattleLogic> .instance.isFighting || Singleton <GameLogic> .instance.bInLogicTick);
        VCollisionShape    shape     = null;
        CollisionShapeType shapeType = this.shapeType;

        if (shapeType != CollisionShapeType.Box)
        {
            if (shapeType != CollisionShapeType.Sphere)
            {
                return(shape);
            }
        }
        else
        {
            return(new VCollisionBox {
                Size = this.Size, Pos = this.Pos
            });
        }
        return(new VCollisionSphere {
            Pos = this.Pos, Radius = this.Size.x
        });
    }
Exemple #15
0
        // This must be called in order for rotation and position offset to by applied
        public void RenderPoints(SuperTile tile, MapOrientation orientation, Vector2 gridSize)
        {
            if (orientation == MapOrientation.Isometric)
            {
                m_Position    = IsometricTransform(m_Position, tile, gridSize);
                m_Position.x += gridSize.x * 0.5f;
                m_Position.y += tile.m_Height - gridSize.y;

                for (int i = 0; i < m_Points.Length; i++)
                {
                    m_Points[i] = IsometricTransform(m_Points[i], tile, gridSize);
                }

                // Also, we are forced to use polygon colliders for isometric projection
                if (m_CollisionShapeType == CollisionShapeType.Ellipse || m_CollisionShapeType == CollisionShapeType.Rectangle)
                {
                    m_CollisionShapeType = CollisionShapeType.Polygon;
                }
            }

            ApplyRotationToPoints();
            m_Points = m_Points.Select(p => p + m_Position).ToArray();
        }
Exemple #16
0
        private Body GetJointBodyPairSprtBody(CollisionShapeType bodyType, Point3D centerPoint, RotateTransform3D rotation, Color color)
        {
            #region WPF Model (plus collision hull)

            // Material
            MaterialGroup materials = new MaterialGroup();
            materials.Children.Add(new DiffuseMaterial(new SolidColorBrush(color)));
            materials.Children.Add(new SpecularMaterial(Brushes.White, 100d));

            // Geometry Model
            GeometryModel3D geometry = new GeometryModel3D();
            geometry.Material     = materials;
            geometry.BackMaterial = materials;

            CollisionHull hull = null;
            switch (bodyType)
            {
            case CollisionShapeType.Box:
                geometry.Geometry = UtilityWPF.GetCube_IndependentFaces(new Point3D(-1, -1, -1d), new Point3D(1d, 1d, 1d));
                hull = CollisionHull.CreateBox(_world, 0, new Vector3D(2d, 2d, 2d), null);
                break;

            case CollisionShapeType.Sphere:
                geometry.Geometry = UtilityWPF.GetSphere_LatLon(5, 1d, 1d, 1d);
                hull = CollisionHull.CreateSphere(_world, 0, new Vector3D(1d, 1d, 1d), null);
                break;

            case CollisionShapeType.Cylinder:
                geometry.Geometry = UtilityWPF.GetCylinder_AlongX(20, 1d, 2d);
                hull = CollisionHull.CreateCylinder(_world, 0, 1d, 2d, null);
                break;

            case CollisionShapeType.Cone:
                geometry.Geometry = UtilityWPF.GetCone_AlongX(20, 1d, 2d);
                hull = CollisionHull.CreateCone(_world, 0, 1d, 2d, null);
                break;

            case CollisionShapeType.Capsule:
            case CollisionShapeType.ChamferCylinder:
                throw new ApplicationException("finish this");

            default:
                throw new ApplicationException("Unknown ConvexBody3D.CollisionShape: " + bodyType.ToString());
            }

            // Transform
            Transform3DGroup transform = new Transform3DGroup();                // rotate needs to be added before translate
            transform.Children.Add(rotation);
            transform.Children.Add(new TranslateTransform3D(centerPoint.ToVector()));

            // Model Visual
            ModelVisual3D model = new ModelVisual3D();
            model.Content   = geometry;
            model.Transform = transform;

            // Add to the viewport
            _viewport.Children.Add(model);

            #endregion

            #region Physics Body

            // Make a physics body that represents this shape
            Body body = new Body(hull, transform.Value, 1d, new Visual3D[] { model });          // being lazy with mass, but since size is fixed, it won't be too noticable
            hull.Dispose();
            body.Velocity = Math3D.GetRandomVector_Circular(1d);

            //body.LinearDamping = .01f;
            //body.AngularDamping = new Vector3D(.01f, .01f, .01f);

            body.ApplyForceAndTorque += new EventHandler <BodyApplyForceAndTorqueArgs>(Body_ApplyForceAndTorque);

            // This will be done later
            //_bodySets.Add(body);

            #endregion

            // Exit Function
            return(body);
        }
Exemple #17
0
        // these were copied from the 1.53 collision shapes tester
        private void GetRatiosMass(out double x, out double y, out double z, out double mass, CollisionShapeType shape)
        {
            // Ratios
            if (chkRandomRatios.IsChecked.Value)
            {
                x = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());         // reused as radius
                y = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());         // reused as height
                z = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());
            }
            else
            {
                x = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, trkX.Minimum, trkX.Maximum, trkX.Value);
                y = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, trkY.Minimum, trkY.Maximum, trkY.Value);
                z = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, trkZ.Minimum, trkZ.Maximum, trkZ.Value);
            }

            switch (shape)
            {
            case CollisionShapeType.Box:
                x   *= 2d;              // sphere treats x as a radius.  box thinks of it as width
                y   *= 2d;
                z   *= 2d;
                mass = x * y * z;
                break;

            case CollisionShapeType.Sphere:
                mass = (4d / 3d) * Math.PI * x * y * z;
                break;

            default:
                throw new ApplicationException("Unexpected CollisionShapeType: " + shape.ToString());
            }

            // If I try to be realistic, then it's boring, so I'll scale the result.  (density shrinks a bit as things get larger)
            mass = UtilityCore.GetScaledValue(MINMASS, MAXMASS, Math.Pow(MINRATIO, 3), Math.Pow(MAXRATIO, 3), mass);
        }
        private void AddBrick(CollisionShapeType shape, Color color, Vector3D size, double mass, Point3D position, DoubleVector directionFacing)
        {
            // Get the wpf model
            CollisionHull hull;
            Transform3DGroup transform;
            Quaternion rotation;
            DiffuseMaterial bodyMaterial;
            ModelVisual3D model = GetWPFModel(out hull, out transform, out rotation, out bodyMaterial, shape, color, Colors.White, 100d, size, position, directionFacing, true);

            // Add to the viewport
            _viewport.Children.Add(model);

            // Make a physics body that represents this shape
            Body body = new Body(hull, transform.Value, 1d, new Visual3D[] { model });
            hull.Dispose();
            body.MaterialGroupID = _material_Brick;

            body.ApplyForceAndTorque += new EventHandler<BodyApplyForceAndTorqueArgs>(Body_ApplyForceAndTorque);

            _bricks.Add(body);
            _bodyMaterials.Add(body, new BodyMaterial(bodyMaterial, color, size.Length));
        }
 public AddBodyArgs(CollisionShapeType collisionShape, double radius, double height, double mass)
 {
     this.CollisionShape = collisionShape;
     _radius = radius;
     _height = height;
     this.Mass = mass;
 }
 public AddBodyArgs(CollisionShapeType collisionShape, Vector3D size, double mass)
 {
     this.CollisionShape = collisionShape;
     _size = size;
     this.Mass = mass;
 }
        private ModelVisual3D GetWPFModel(out CollisionHull hull, out Transform3DGroup transform, out Quaternion rotation, out DiffuseMaterial bodyMaterial, CollisionShapeType shape, Color color, Color reflectionColor, double reflectionIntensity, Vector3D size, Point3D position, DoubleVector directionFacing, bool createHull)
        {
            // Material
            MaterialGroup materials = new MaterialGroup();
            bodyMaterial = new DiffuseMaterial(new SolidColorBrush(color));
            materials.Children.Add(bodyMaterial);
            materials.Children.Add(new SpecularMaterial(new SolidColorBrush(reflectionColor), reflectionIntensity));

            // Geometry Model
            GeometryModel3D geometry = new GeometryModel3D();
            geometry.Material = materials;
            geometry.BackMaterial = materials;

            hull = null;

            switch (shape)
            {
                case CollisionShapeType.Box:
                    Vector3D halfSize = size / 2d;
                    geometry.Geometry = UtilityWPF.GetCube_IndependentFaces(new Point3D(-halfSize.X, -halfSize.Y, -halfSize.Z), new Point3D(halfSize.X, halfSize.Y, halfSize.Z));
                    if (createHull)
                    {
                        hull = CollisionHull.CreateBox(_world, 0, size, null);
                    }
                    break;

                case CollisionShapeType.Sphere:
                    geometry.Geometry = UtilityWPF.GetSphere_LatLon(5, size.X, size.Y, size.Z);
                    if (createHull)
                    {
                        hull = CollisionHull.CreateSphere(_world, 0, size, null);
                    }
                    break;

                case CollisionShapeType.Cylinder:
                    geometry.Geometry = UtilityWPF.GetCylinder_AlongX(20, size.X, size.Y);
                    if (createHull)
                    {
                        hull = CollisionHull.CreateCylinder(_world, 0, size.X, size.Y, null);
                    }
                    break;

                case CollisionShapeType.Cone:
                    geometry.Geometry = UtilityWPF.GetCone_AlongX(20, size.X, size.Y);
                    if (createHull)
                    {
                        hull = CollisionHull.CreateCone(_world, 0, size.X, size.Y, null);
                    }
                    break;

                default:
                    throw new ApplicationException("Unexpected CollisionShapeType: " + shape.ToString());
            }

            // Transform
            transform = new Transform3DGroup();		// rotate needs to be added before translate



            //rotation = _defaultDirectionFacing.GetAngleAroundAxis(directionFacing);		// can't use double vector, it over rotates (not anymore, but this is still isn't rotating correctly)

            rotation = Math3D.GetRotation(_defaultDirectionFacing.Standard, directionFacing.Standard);



            transform.Children.Add(new RotateTransform3D(new QuaternionRotation3D(rotation)));
            transform.Children.Add(new TranslateTransform3D(position.ToVector()));

            // Model Visual
            ModelVisual3D retVal = new ModelVisual3D();
            retVal.Content = geometry;
            retVal.Transform = transform;

            // Exit Function
            return retVal;
        }
 /// <summary>
 /// Combines geometry type and static dynamic
 /// </summary>
 public int MakeShapeFlags(CollisionShapeType type, bool dynamic)
 {
     return((int)type | (int)(dynamic ? CollisionShapeFlags.eFlexShapeFlagDynamic : 0));
 }
Exemple #23
0
 public void MakePointsFromPolygon(Vector2[] points)
 {
     m_CollisionShapeType = CollisionShapeType.Polygon;
     m_IsClosed           = true;
     m_Points             = points;
 }
        private void AddCannonBall(CollisionShapeType shape, Color color, Vector3D size, double mass, Point3D position, Vector3D velocity, Vector3D angularVelocity, DoubleVector directionFacing)
        {
            // Get the wpf model
            CollisionHull hull;
            Transform3DGroup transform;
            Quaternion rotation;
            DiffuseMaterial bodyMaterial;
            ModelVisual3D model = GetWPFModel(out hull, out transform, out rotation, out bodyMaterial, shape, color, Colors.Gray, 50d, size, position, directionFacing, true);

            // Add to the viewport
            _viewport.Children.Add(model);

            // Make a physics body that represents this shape
            Body body = new Body(hull, transform.Value, mass, new Visual3D[] { model });
            hull.Dispose();
            body.IsContinuousCollision = true;
            body.MaterialGroupID = _material_Projectile;
            body.Velocity = velocity;
            //body.AngularVelocity = Math3D.RotateAroundAxis(angularVelocity, axis, radians);
            body.AngularVelocity = rotation.GetRotatedVector(angularVelocity);
            body.AngularDamping = new Vector3D(0, 0, 0);		// this one becomes very noticable with the big spinner

            body.ApplyForceAndTorque += new EventHandler<BodyApplyForceAndTorqueArgs>(Body_ApplyForceAndTorque);

            _projectiles.Add(body);
            _bodyMaterials.Add(body, new BodyMaterial(bodyMaterial, color, size.Length));
        }
Exemple #25
0
        private void Radio_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!_isInitialized)
                {
                    return;
                }

                bool isAllThree = false;

                // Figure out what to present
                CollisionShapeType shape = GetSelectedShape();
                switch (shape)
                {
                case CollisionShapeType.Box:
                case CollisionShapeType.Sphere:
                    isAllThree = true;
                    break;

                case CollisionShapeType.Cone:
                case CollisionShapeType.Capsule:
                    lblRatioHint.Text       = "height must be greater or equal to diameter";
                    lblRatioHint.Visibility = Visibility.Visible;
                    break;

                case CollisionShapeType.Cylinder:
                case CollisionShapeType.ChamferCylinder:
                    lblRatioHint.Visibility = Visibility.Collapsed;
                    break;

                default:
                    throw new ApplicationException("Unknown CollisionShapeType: " + shape.ToString());
                }

                // Change labels/visibility
                if (isAllThree)
                {
                    #region X Y Z

                    lblX.Content = "X";
                    lblY.Content = "Y";
                    lblZ.Content = "Z";

                    lblZ.Visibility = Visibility.Visible;
                    trkZ.Visibility = Visibility.Visible;

                    lblRatioHint.Visibility = Visibility.Collapsed;

                    #endregion
                }
                else
                {
                    #region Diameter Height

                    // Going with diameter instead of radius because some of the constraints talk about diameter, so it's easier to see the ratio as diameter
                    lblX.Content = "Diameter";
                    lblY.Content = "Height";

                    lblZ.Visibility = Visibility.Collapsed;
                    trkZ.Visibility = Visibility.Collapsed;

                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Hull Shape Radio", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private Body GetNewBody(CollisionShapeType shape, Color color, Vector3D size, double mass, Point3D position, Quaternion orientation, int materialID)
        {
            DoubleVector dirFacing = _defaultDirectionFacing.GetRotatedVector(orientation.Axis, orientation.Angle);

            // Get the wpf model
            CollisionHull hull;
            Transform3DGroup transform;
            Quaternion rotation;
            DiffuseMaterial bodyMaterial;
            ModelVisual3D model = GetWPFModel(out hull, out transform, out rotation, out bodyMaterial, shape, color, Colors.White, 100d, size, position, dirFacing, true);

            // Add to the viewport
            _viewport.Children.Add(model);

            // Make a physics body that represents this shape
            Body retVal = new Body(hull, transform.Value, mass, new Visual3D[] { model });
            retVal.AutoSleep = false;		// the falling sand was falling asleep, even though the velocity was non zero (the sand would suddenly stop)
            retVal.MaterialGroupID = materialID;

            return retVal;
        }
Exemple #27
0
 public AddBodyArgs(CollisionShapeType collisionShape, Vector3D size, double mass)
 {
     this.CollisionShape = collisionShape;
     _size     = size;
     this.Mass = mass;
 }
Exemple #28
0
        private void GetRatiosMass(out double radius, out double height, out double mass, CollisionShapeType shape)
        {
            #region Ratios

            if (chkRandomRatios.IsChecked.Value)
            {
                height = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());

                switch (shape)
                {
                case CollisionShapeType.Cone:
                case CollisionShapeType.Capsule:
                    // height must be greater or equal to diameter
                    radius = UtilityCore.GetScaledValue(MINRATIO * 2d, height, 0d, 1d, _rand.NextDouble());
                    break;

                default:
                    radius = UtilityCore.GetScaledValue(MINRATIO * 2d, MAXRATIO, 0d, 1d, _rand.NextDouble());
                    break;
                }
            }
            else
            {
                //NOTE:  I'm not going to error out if they have invalid values - this is a tester, and I want to test what happens
                radius  = UtilityCore.GetScaledValue(MINRATIO * 2d, MAXRATIO * 2d, trkX.Minimum, trkX.Maximum, trkX.Value);
                radius /= 2d;           // the slider is diameter
                height  = UtilityCore.GetScaledValue(MINRATIO * 2d, MAXRATIO * 2d, trkY.Minimum, trkY.Maximum, trkY.Value);
            }

            #endregion

            switch (shape)
            {
            case CollisionShapeType.Capsule:
                // This looks like a pill.  I'm guessing since the height is capped to the diameter that the rounded parts cut into the height
                // I'm also guessing that the rounded parts are spherical
                double cylinderHeight = height - (2d * radius);
                mass  = Math.PI * radius * radius * cylinderHeight;             // cylinder portion
                mass += (4d / 3d) * Math.PI * radius * radius * radius;         // end caps (adds up to a sphere)
                break;

            case CollisionShapeType.Cylinder:
                mass = Math.PI * radius * radius * height;
                break;

            case CollisionShapeType.Cone:
                mass = (1d / 3d) * Math.PI * radius * radius * height;
                break;

            case CollisionShapeType.ChamferCylinder:
                mass = Math.PI * radius * radius * height;              // I can't find any examples of what this looks like when the height is large.  It looks like it turns into a capsule if the height exceeds diameter?
                break;

            default:
                throw new ApplicationException("Unexpected CollisionShapeType: " + shape.ToString());
            }

            // If I try to be realistic, then it's boring, so I'll scale the result.  (density shrinks a bit as things get larger)
            mass = UtilityCore.GetScaledValue(MINMASS, MAXMASS, Math.Pow(MINRATIO, 3), Math.Pow(MAXRATIO, 3), mass);
        }
Exemple #29
0
 public void MakePointsFromPolyline(Vector2[] points)
 {
     m_CollisionShapeType = CollisionShapeType.Polyline;
     m_IsClosed           = false;
     m_Points             = points;
 }
        // these were copied from the 1.53 collision shapes tester
        private void GetRatiosMass(out double x, out double y, out double z, out double mass, CollisionShapeType shape)
        {
            // Ratios
            if (chkRandomRatios.IsChecked.Value)
            {
                x = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());		// reused as radius
                y = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());		// reused as height
                z = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());
            }
            else
            {
                x = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, trkX.Minimum, trkX.Maximum, trkX.Value);
                y = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, trkY.Minimum, trkY.Maximum, trkY.Value);
                z = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, trkZ.Minimum, trkZ.Maximum, trkZ.Value);
            }

            switch (shape)
            {
                case CollisionShapeType.Box:
                    x *= 2d;		// sphere treats x as a radius.  box thinks of it as width
                    y *= 2d;
                    z *= 2d;
                    mass = x * y * z;
                    break;

                case CollisionShapeType.Sphere:
                    mass = (4d / 3d) * Math.PI * x * y * z;
                    break;

                default:
                    throw new ApplicationException("Unexpected CollisionShapeType: " + shape.ToString());
            }

            // If I try to be realistic, then it's boring, so I'll scale the result.  (density shrinks a bit as things get larger)
            mass = UtilityCore.GetScaledValue(MINMASS, MAXMASS, Math.Pow(MINRATIO, 3), Math.Pow(MAXRATIO, 3), mass);
        }
        private void GetRatiosMass(out double radius, out double height, out double mass, CollisionShapeType shape)
        {
            #region Ratios

            if (chkRandomRatios.IsChecked.Value)
            {
                height = UtilityCore.GetScaledValue(MINRATIO, MAXRATIO, 0d, 1d, _rand.NextDouble());

                switch (shape)
                {
                    case CollisionShapeType.Cone:
                    case CollisionShapeType.Capsule:
                        // height must be greater or equal to diameter
                        radius = UtilityCore.GetScaledValue(MINRATIO * 2d, height, 0d, 1d, _rand.NextDouble());
                        break;

                    default:
                        radius = UtilityCore.GetScaledValue(MINRATIO * 2d, MAXRATIO, 0d, 1d, _rand.NextDouble());
                        break;
                }
            }
            else
            {
                //NOTE:  I'm not going to error out if they have invalid values - this is a tester, and I want to test what happens
                radius = UtilityCore.GetScaledValue(MINRATIO * 2d, MAXRATIO * 2d, trkX.Minimum, trkX.Maximum, trkX.Value);
                radius /= 2d;		// the slider is diameter
                height = UtilityCore.GetScaledValue(MINRATIO * 2d, MAXRATIO * 2d, trkY.Minimum, trkY.Maximum, trkY.Value);
            }

            #endregion

            switch (shape)
            {
                case CollisionShapeType.Capsule:
                    // This looks like a pill.  I'm guessing since the height is capped to the diameter that the rounded parts cut into the height
                    // I'm also guessing that the rounded parts are spherical
                    double cylinderHeight = height - (2d * radius);
                    mass = Math.PI * radius * radius * cylinderHeight;		// cylinder portion
                    mass += (4d / 3d) * Math.PI * radius * radius * radius;		// end caps (adds up to a sphere)
                    break;

                case CollisionShapeType.Cylinder:
                    mass = Math.PI * radius * radius * height;
                    break;

                case CollisionShapeType.Cone:
                    mass = (1d / 3d) * Math.PI * radius * radius * height;
                    break;

                case CollisionShapeType.ChamferCylinder:
                    mass = Math.PI * radius * radius * height;		// I can't find any examples of what this looks like when the height is large.  It looks like it turns into a capsule if the height exceeds diameter?
                    break;

                default:
                    throw new ApplicationException("Unexpected CollisionShapeType: " + shape.ToString());
            }

            // If I try to be realistic, then it's boring, so I'll scale the result.  (density shrinks a bit as things get larger)
            mass = UtilityCore.GetScaledValue(MINMASS, MAXMASS, Math.Pow(MINRATIO, 3), Math.Pow(MAXRATIO, 3), mass);
        }
        private Body GetJointBodyPairSprtBody(CollisionShapeType bodyType, Point3D centerPoint, RotateTransform3D rotation, Color color)
        {
            #region WPF Model (plus collision hull)

            // Material
            MaterialGroup materials = new MaterialGroup();
            materials.Children.Add(new DiffuseMaterial(new SolidColorBrush(color)));
            materials.Children.Add(new SpecularMaterial(Brushes.White, 100d));

            // Geometry Model
            GeometryModel3D geometry = new GeometryModel3D();
            geometry.Material = materials;
            geometry.BackMaterial = materials;

            CollisionHull hull = null;
            switch (bodyType)
            {
                case CollisionShapeType.Box:
                    geometry.Geometry = UtilityWPF.GetCube_IndependentFaces(new Point3D(-1, -1, -1d), new Point3D(1d, 1d, 1d));
                    hull = CollisionHull.CreateBox(_world, 0, new Vector3D(2d, 2d, 2d), null);
                    break;

                case CollisionShapeType.Sphere:
                    geometry.Geometry = UtilityWPF.GetSphere_LatLon(5, 1d, 1d, 1d);
                    hull = CollisionHull.CreateSphere(_world, 0, new Vector3D(1d, 1d, 1d), null);
                    break;

                case CollisionShapeType.Cylinder:
                    geometry.Geometry = UtilityWPF.GetCylinder_AlongX(20, 1d, 2d);
                    hull = CollisionHull.CreateCylinder(_world, 0, 1d, 2d, null);
                    break;

                case CollisionShapeType.Cone:
                    geometry.Geometry = UtilityWPF.GetCone_AlongX(20, 1d, 2d);
                    hull = CollisionHull.CreateCone(_world, 0, 1d, 2d, null);
                    break;

                case CollisionShapeType.Capsule:
                case CollisionShapeType.ChamferCylinder:
                    throw new ApplicationException("finish this");

                default:
                    throw new ApplicationException("Unknown ConvexBody3D.CollisionShape: " + bodyType.ToString());
            }

            // Transform
            Transform3DGroup transform = new Transform3DGroup();		// rotate needs to be added before translate
            transform.Children.Add(rotation);
            transform.Children.Add(new TranslateTransform3D(centerPoint.ToVector()));

            // Model Visual
            ModelVisual3D model = new ModelVisual3D();
            model.Content = geometry;
            model.Transform = transform;

            // Add to the viewport
            _viewport.Children.Add(model);

            #endregion

            #region Physics Body

            // Make a physics body that represents this shape
            Body body = new Body(hull, transform.Value, 1d, new Visual3D[] { model });		// being lazy with mass, but since size is fixed, it won't be too noticable
            hull.Dispose();
            body.Velocity = Math3D.GetRandomVector_Circular(1d);

            //body.LinearDamping = .01f;
            //body.AngularDamping = new Vector3D(.01f, .01f, .01f);

            body.ApplyForceAndTorque += new EventHandler<BodyApplyForceAndTorqueArgs>(Body_ApplyForceAndTorque);

            // This will be done later
            //_bodySets.Add(body);

            #endregion

            // Exit Function
            return body;
        }