Example #1
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);
            }
        }
        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);
            }
        }
        private void addSimple1_AddBody(object sender, AddBodyArgs e)
        {
            try
            {
                #region WPF Model (plus collision hull)

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

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

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

                    case CollisionShapeType.Sphere:
                        geometry.Geometry = UtilityWPF.GetSphere_LatLon(5, e.Size.X, e.Size.Y, e.Size.Z);
                        hull = CollisionHull.CreateSphere(_world, 0, e.Size, null);
                        break;

                    case CollisionShapeType.Cylinder:
                        geometry.Geometry = UtilityWPF.GetCylinder_AlongX(20, e.Radius, e.Height);
                        hull = CollisionHull.CreateCylinder(_world, 0, e.Radius, e.Height, null);
                        break;

                    case CollisionShapeType.Cone:
                        geometry.Geometry = UtilityWPF.GetCone_AlongX(20, e.Radius, e.Height);
                        hull = CollisionHull.CreateCone(_world, 0, e.Radius, e.Height, null);
                        break;

                    case CollisionShapeType.Capsule:
                    case CollisionShapeType.ChamferCylinder:
                        MessageBox.Show("finish this");
                        return;

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

                // Transform
                Transform3DGroup transform = new Transform3DGroup();		// rotate needs to be added before translate
                transform.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(Math3D.GetRandomVector_Spherical(10), Math1D.GetNearZeroValue(360d))));
                transform.Children.Add(new TranslateTransform3D(Math3D.GetRandomVector_Spherical(CREATEOBJECTBOUNDRY)));

                // 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, e.Mass, new Visual3D[] { model });
                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);

                Body[] bodySet = new Body[] { body };
                _bodySets.Add(bodySet);

                #endregion

                BodiesAdded(bodySet);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #4
0
        private void addSimple1_AddBody(object sender, AddBodyArgs e)
        {
            try
            {
                #region WPF Model (plus collision hull)

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

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

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

                case CollisionShapeType.Sphere:
                    geometry.Geometry = UtilityWPF.GetSphere_LatLon(5, e.Size.X, e.Size.Y, e.Size.Z);
                    hull = CollisionHull.CreateSphere(_world, 0, e.Size, null);
                    break;

                case CollisionShapeType.Cylinder:
                    geometry.Geometry = UtilityWPF.GetCylinder_AlongX(20, e.Radius, e.Height);
                    hull = CollisionHull.CreateCylinder(_world, 0, e.Radius, e.Height, null);
                    break;

                case CollisionShapeType.Cone:
                    geometry.Geometry = UtilityWPF.GetCone_AlongX(20, e.Radius, e.Height);
                    hull = CollisionHull.CreateCone(_world, 0, e.Radius, e.Height, null);
                    break;

                case CollisionShapeType.Capsule:
                case CollisionShapeType.ChamferCylinder:
                    MessageBox.Show("finish this");
                    return;

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

                // Transform
                Transform3DGroup transform = new Transform3DGroup();            // rotate needs to be added before translate
                transform.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(Math3D.GetRandomVector_Spherical(10), Math1D.GetNearZeroValue(360d))));
                transform.Children.Add(new TranslateTransform3D(Math3D.GetRandomVector_Spherical(CREATEOBJECTBOUNDRY)));

                // 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, e.Mass, new Visual3D[] { model });
                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);

                Body[] bodySet = new Body[] { body };
                _bodySets.Add(bodySet);

                #endregion

                BodiesAdded(bodySet);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }