Esempio n. 1
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);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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 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;
        }
        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;
        }
Esempio n. 9
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);
        }