Esempio n. 1
0
        public void Update_t()
        {
            if (bbox != null)
            {
                inst.RemoveObject(bbox);
            }


            lower = new CompoundShape.TransformedShape(
                new BoxShape(1.430f, 0.6f, 4.040f), JMatrix.Identity, JVector.Zero);

            upper = new CompoundShape.TransformedShape(
                new BoxShape(1.430f, 0.5f, 1.040f), JMatrix.Identity, addinal);


            CompoundShape.TransformedShape[] subShapes = { lower, upper };

            chassis = new CompoundShape(subShapes);


            bbox               = new GameObjectJShape(chassis);
            bbox.Position      = position;
            bbox.Body.IsStatic = true;
            inst.AddObject(bbox);
        }
    public override CompoundShape.TransformedShape CreateTransformedShape(JRigidBody body)
    {
        var shape            = new CylinderShape(height, radius);
        var transformedShape = new CompoundShape.TransformedShape(shape, GetOrientation(), offset.ToJVector());

        return(transformedShape);
    }
Esempio n. 3
0
        private void BuildCar()
        {
            JitterDemo demo  = this.Game as JitterDemo;
            World      world = demo.World;

            CompoundShape.TransformedShape lower = new CompoundShape.TransformedShape(
                new BoxShape(2.5f, 1f, 6.0f), JMatrix.Identity, JVector.Zero);

            CompoundShape.TransformedShape upper = new CompoundShape.TransformedShape(
                new BoxShape(2.0f, 0.5f, 3.0f), JMatrix.Identity, JVector.Up * 0.75f + JVector.Backward * 1.0f);

            CompoundShape.TransformedShape[] subShapes = { lower, upper };

            Shape chassis = new CompoundShape(subShapes);

            //chassis = new BoxShape(2.5f, 1f, 6.0f);

            carBody = new DefaultCar(world, chassis);

            // use the inertia of the lower box.

            // adjust some driving values
            carBody.SteerAngle       = 30; carBody.DriveTorque = 155;
            carBody.AccelerationRate = 10;
            carBody.SteerRate        = 2f;
            carBody.AdjustWheelValues();

            carBody.Tag = BodyTag.DontDrawMe;
            carBody.AllowDeactivation = false;

            // place the car two units above the ground.
            carBody.Position = new JVector(0, 5, 0);

            world.AddBody(carBody);
        }
Esempio n. 4
0
        public override void Build()
        {
            AddGround();

            var shapes = BuildFromHACDTestObjFile(@"Content/ConvexDecomposition.obj");

            var transformedShapes
                = new CompoundShape.TransformedShape[shapes.Count];

            for (int i = 0; i < shapes.Count; i++)
            {
                transformedShapes[i] = new CompoundShape.TransformedShape
                {
                    Shape       = shapes[i],
                    Orientation = JMatrix.Identity,
                    Position    = -1.0f * shapes[i].Shift
                };
            }

            // Create one compound shape
            var cs = new CompoundShape(transformedShapes);

            for (int i = 0; i < 1; i++)
            {
                var compoundBody = new RigidBody(cs)
                {
                    EnableDebugDraw = true,
                    Position        = new JVector(0, 5 + (i * 10), 0) - cs.Shift
                };
                Demo.World.AddBody(compoundBody);
            }

            // Create several single bodies.
            for (int i = 0; i < shapes.Count; i++)
            {
                var body = new RigidBody(shapes[i])
                {
                    Position        = (-1.0f * shapes[i].Shift) + new JVector(-10, 5, 0),
                    EnableDebugDraw = true
                };
                Demo.World.AddBody(body);
            }

            for (int i = 0; i < shapes.Count; i++)
            {
                var body = new RigidBody(shapes[i])
                {
                    Position        = (-1.0f * shapes[i].Shift) + new JVector(-20, 5, 0),
                    EnableDebugDraw = true,
                    IsStatic        = true
                };
                Demo.World.AddBody(body);
            }
        }
Esempio n. 5
0
        public CarObjectKamaz()
        {
            //sc = EE.СurrentResources.GetResource<IDraweble>("models/PAZ.obj");
            sc = EE.СurrentResources.GetResource <IDrawable>("models/Kamaz.obj");
            wc = EE.СurrentResources.GetResource <IDrawable>("models/PickUpWheel.obj");

            //sc = EE.СurrentResources.GetResource<IDraweble>("models/PAZ.obj");
            CompoundShape.TransformedShape lower = new CompoundShape.TransformedShape(
                new BoxShape(2.050f, 2.0f, 7.2f), JMatrix.Identity, JVector.Zero);

            //CompoundShape.TransformedShape upper = new CompoundShape.TransformedShape(
            //    new BoxShape(1.430f, 0.5f, 1.040f), JMatrix.Identity, new JVector(0, 0.5f, -0.2f));

            CompoundShape.TransformedShape[] subShapes = { lower /*, upper */ };

            CompoundShape chassis = new CompoundShape(subShapes);

            Body      = new DefaultCar(this, chassis);
            Body.Mass = 7000f;
            // Body.Material.Restitution = 2.0f;
            /// Body.Material.KineticFriction = 0.1f;
            Body.Material.StaticFriction = 2f;

            wheels = new Wheel[6];
            wheels[(int)WheelPosition.FrontLeft]  = new Wheel(Body, new JVector(0, -1.00f, 0.6f) + (JVector.Left * 0.7f) + 2.80f * JVector.Forward, whRadius);
            wheels[(int)WheelPosition.FrontRight] = new Wheel(Body, new JVector(0, -1.00f, 0.6f) + (JVector.Right * 0.7f) + 2.80f * JVector.Forward, whRadius);

            wheels[(int)WheelPosition.BackLeft]  = new Wheel(Body, new JVector(0, -1.00f, -0.7f) + (JVector.Left * 0.7f) + 2.54f * JVector.Backward, whRadius);
            wheels[(int)WheelPosition.BackRight] = new Wheel(Body, new JVector(0, -1.00f, -0.7f) + (JVector.Right * 0.7f) + 2.54f * JVector.Backward, whRadius);

            wheels[(int)WheelPosition.Back1Left]  = new Wheel(Body, new JVector(0, -1.00f, -0.7f) + (JVector.Left * 0.7f) + 1.35f * JVector.Backward, whRadius);
            wheels[(int)WheelPosition.Back1Right] = new Wheel(Body, new JVector(0, -1.00f, -0.7f) + (JVector.Right * 0.7f) + 1.35f * JVector.Backward, whRadius);

            //угол поворта
            ((DefaultCar)Body).SteerAngle = 40;
            //крутящий момент
            ((DefaultCar)Body).DriveTorque = 30;

            //ускарение
            ((DefaultCar)Body).AccelerationRate = 500f;
            //ускорение поврота
            ((DefaultCar)Body).SteerRate = 18f;
            ((DefaultCar)Body).AdjustWheelValues(wheels);

            // carBody.Tag = BodyTag.DontDrawMe;
            Body.AllowDeactivation = false;

            // place the car two units above the ground.
            //Body.Position = new JVector(0, 2, 0);

            //   world.AddBody(Body);
        }
Esempio n. 6
0
        public override void Build()
        {
            AddGround();


            var path = Environment.CurrentDirectory;


            List <ConvexHullShape> shapes = BuildFromHACDTestObjFile("../../../Content/Model/ConvexDecomposition.obj");

            CompoundShape.TransformedShape[] transformedShapes
                = new CompoundShape.TransformedShape[shapes.Count];

            for (int i = 0; i < shapes.Count; i++)
            {
                transformedShapes[i]             = new CompoundShape.TransformedShape();
                transformedShapes[i].Shape       = shapes[i];
                transformedShapes[i].Orientation = JMatrix.Identity;
                transformedShapes[i].Position    = -1.0f * shapes[i].Shift;
            }


            // Create one compound shape
            CompoundShape cs = new CompoundShape(transformedShapes);

            for (int i = 0; i < 1; i++)
            {
                RigidBody compoundBody = new RigidBody(cs);
                compoundBody.EnableDebugDraw = true;
                compoundBody.Position        = new JVector(0, 5 + i * 10, 0) - cs.Shift;
                Demo.World.AddBody(compoundBody);
            }


            // Create several single bodies.
            for (int i = 0; i < shapes.Count; i++)
            {
                RigidBody body = new RigidBody(shapes[i]);
                body.Position        = -1.0f * shapes[i].Shift + new JVector(-10, 5, 0);
                body.EnableDebugDraw = true;
                Demo.World.AddBody(body);
            }

            for (int i = 0; i < shapes.Count; i++)
            {
                RigidBody body = new RigidBody(shapes[i]);
                body.Position        = -1.0f * shapes[i].Shift + new JVector(-20, 5, 0);
                body.EnableDebugDraw = true;
                body.IsStatic        = true;
                Demo.World.AddBody(body);
            }
        }
Esempio n. 7
0
        private void BuildScene()
        {
            // creating a box shape, representing the ground
            // and one to create the compound shape
            Shape boxShape    = new BoxShape(new JVector(1, 1, 3));
            Shape groundShape = new BoxShape(new JVector(10, 1, 10));

            // Build the CompoundShape.TransformedShape structure,
            // containing "normal" shapes and position/orientation
            // information.
            CompoundShape.TransformedShape[] transformedShapes =
                new CompoundShape.TransformedShape[2];

            // Create a rotation matrix (90°)
            JMatrix rotated =
                Conversion.ToJitterMatrix(Matrix.CreateRotationX(MathHelper.PiOver2));

            // the first "sub" shape. A rotatated boxShape.
            transformedShapes[0] = new CompoundShape.TransformedShape(
                boxShape, rotated, JVector.Zero);

            // the second "sub" shape.
            transformedShapes[1] = new CompoundShape.TransformedShape(
                boxShape, JMatrix.Identity, JVector.Zero);

            // Pass the CompoundShape.TransformedShape structure to the compound shape.
            CompoundShape compoundShape = new CompoundShape(transformedShapes);

            RigidBody compoundBody = new RigidBody(compoundShape);

            compoundBody.Position = new JVector(0, 5, 0);

            RigidBody groundBody = new RigidBody(groundShape);

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

            // add the bodies to the world.
            world.AddBody(compoundBody);
            world.AddBody(groundBody);
        }
Esempio n. 8
0
        public void Merge()
        {
            // \todo
#if true
            if (selectionManager == null)
            {
                return;
            }

            if (selectionManager.Models.Count <= 1)
            {
                return;
            }

            //  First pass: Find which model to use as new reference
            //  There should be one model which parent is not any
            //  of the models in selection.
            Model referenceModel = null;
            foreach (Model referenceCandidate in selectionManager.Models)
            {
                if (referenceCandidate == null)
                {
                    continue;
                }

                bool candidateHasParentInSelection = false;
                foreach (Model otherModel in selectionManager.Models)
                {
                    if (otherModel == null)
                    {
                        continue;
                    }
                    if (referenceCandidate.Frame.Parent == otherModel.Frame)
                    {
                        candidateHasParentInSelection = true;
                        break;
                    }
                }

                if (candidateHasParentInSelection == false)
                {
                    referenceModel = referenceCandidate;
                    break;
                }
            }
            if (referenceModel == null)
            {
                throw new Exception("Unable to pick selection root model for merge operation");
            }

            //  This is where compound shapes are collected.
            //  All shapes are added in the coordinate system of the reference model.
            //  Models that have no rigid bodies are skipped
            List <CompoundShape.TransformedShape> shapes = new List <CompoundShape.TransformedShape>();

            //  Add reference model (if it has rigidbody)
            if (referenceModel.RigidBody != null)
            {
                //var shape = new CompoundShape.TransformedShape(
                //    referenceModel.RigidBody.Shape,
                //    JMatrix.Identity,
                //    JVector.Zero
                //);
                //shapes.Add(shape);
                Shape         oldShape = referenceModel.RigidBody.Shape;
                CompoundShape compound = oldShape as CompoundShape;
                if (compound != null)
                {
                    foreach (var part in compound.Shapes)
                    {
                        var shape = new CompoundShape.TransformedShape(
                            part.Shape,
                            part.Transform
                            //part.Orientation,
                            //part.Position
                            );
                        shapes.Add(shape);
                    }
                }
                else
                {
                    var shape = new CompoundShape.TransformedShape(
                        referenceModel.RigidBody.Shape,
                        Matrix4.Identity
                        //JMatrix.Identity,
                        //JVector.Zero
                        );
                    shapes.Add(shape);
                }
            }

            GeometryMesh referencePolymesh = referenceModel.Batch.MeshSource as GeometryMesh;
            Geometry     mergedGeometry    = new CloneGeometryOperation(referencePolymesh.Geometry, null).Destination;

            //  Remove original reference model from scene graph
            sceneManager.RemoveModel(referenceModel);
            foreach (Model mergeModel in selectionManager.Models)
            {
                if (mergeModel == null || mergeModel == referenceModel)
                {
                    continue;
                }
                GeometryMesh addPolymesh = mergeModel.Batch.MeshSource as GeometryMesh;
                Geometry     addGeometry = new CloneGeometryOperation(addPolymesh.Geometry, null).Destination;
                Matrix4      toSameSpace = referenceModel.Frame.LocalToWorld.InverseMatrix * mergeModel.Frame.LocalToWorld.Matrix;
                addGeometry.Transform(toSameSpace);

                if (mergeModel.RigidBody != null)
                {
                    //  TODO: Decompose - for now we only have rotations and translations so this should work

                    /*JMatrix orientation;
                     * orientation.M11 = toSameSpace._00;
                     * orientation.M21 = toSameSpace._01;
                     * orientation.M31 = toSameSpace._02;
                     * orientation.M12 = toSameSpace._10;
                     * orientation.M22 = toSameSpace._11;
                     * orientation.M32 = toSameSpace._12;
                     * orientation.M13 = toSameSpace._20;
                     * orientation.M23 = toSameSpace._21;
                     * orientation.M33 = toSameSpace._22;
                     * orientation.M13 = toSameSpace._20;
                     * orientation.M23 = toSameSpace._21;
                     * orientation.M33 = toSameSpace._22;
                     * Vector3 p = toSameSpace.GetColumn3(3);
                     * JVector position = new JVector(p.X, p.Y, p.Z);*/

                    Shape         oldShape = mergeModel.RigidBody.Shape;
                    CompoundShape compound = oldShape as CompoundShape;
                    if (compound != null)
                    {
                        foreach (var part in compound.Shapes)
                        {
                            var shape = new CompoundShape.TransformedShape(
                                part.Shape,
                                toSameSpace * part.Transform
                                //JMatrix.Multiply(orientation, part.Orientation),
                                //position + JVector.Transform(part.Position, orientation)
                                );
                            shapes.Add(shape);
                        }
                    }
                    else
                    {
                        var shape = new CompoundShape.TransformedShape(
                            oldShape,
                            toSameSpace
                            //orientation,
                            //position
                            );
                        shapes.Add(shape);
                    }
                }

                mergedGeometry.Merge(addGeometry);
                sceneManager.RemoveModel(mergeModel);
            }

            Matrix4       modelOffset   = Matrix4.Identity;
            CompoundShape compoundShape = null;
            if (shapes.Count > 0)
            {
                compoundShape = new CompoundShape(shapes);

                Matrix4 vertexOffset = Matrix4.CreateTranslation(compoundShape.Shift.X, compoundShape.Shift.Y, compoundShape.Shift.Z);
                modelOffset = Matrix4.CreateTranslation(-compoundShape.Shift.X, -compoundShape.Shift.Y, -compoundShape.Shift.Z);

                mergedGeometry.Transform(vertexOffset);
            }
            mergedGeometry.ComputePolygonCentroids();
            mergedGeometry.ComputePolygonNormals();
            //mergedGeometry.ComputeCornerNormals(0.0f);
            mergedGeometry.SmoothNormalize("corner_normals", "polygon_normals", (0.0f * (float)Math.PI));
            mergedGeometry.BuildEdges();

            GeometryMesh mergedPolyMesh = new GeometryMesh(mergedGeometry, NormalStyle.PolygonNormals);

            //  Compute bounding box - debug
            {
                var bbox           = new BoundingBox();
                var pointLocations = mergedGeometry.PointAttributes.Find <Vector3>("point_locations");
                bbox.Clear();
                foreach (Point point in mergedGeometry.Points)
                {
                    bbox.ExtendBy(pointLocations[point]);
                }
            }

            Model newModel = new Model(
                "Merge(" + referenceModel.Name + ", " + selectionManager.Models.Count + ")",
                mergedPolyMesh,
                referenceModel.Batch.Material,
                referenceModel.Frame.LocalToParent.Matrix * modelOffset
                );
            if (compoundShape != null)
            {
                newModel.PhysicsShape = compoundShape;
            }
            //sceneManager.MakeModelPhysicsConvexHull(newModel);  This won't work as Shift is not properly taken into account
            newModel.Frame.Parent = referenceModel.Frame.Parent;
            newModel.Static       = referenceModel.Static;

            selectionManager.ClearSelection();

            //  This will create rigidbody using .PhysicsShape if set, otherwise it will call
            //  MakeModelPhysicsConvexHull().
            sceneManager.AddModel(newModel);

            selectionManager.HoverModel   = null;
            selectionManager.HoverPolygon = null;
#endif
        }
Esempio n. 9
0
        private void SpawnRandomPrimitive(JVector position, JVector velocity)
        {
            RigidBody body = null;
            int       rndn = rndn = random.Next(7);

            if (rndn == 5 || rndn == 6)
            {
                rndn = random.Next(7);
            }

            switch (rndn)
            {
            case 0:
                body = new RigidBody(new ConeShape(random.Next(5, 50) / 20.0f, random.Next(10, 20) / 20.0f));
                break;

            case 1:
                body = new RigidBody(new BoxShape(random.Next(10, 30) / 20.0f, random.Next(10, 30) / 20.0f, random.Next(10, 30) / 20.0f));
                break;

            case 2:
                body = new RigidBody(new SphereShape(0.4f));
                break;

            case 3:
                body = new RigidBody(new CylinderShape(1.0f, 0.5f));
                break;

            case 4:
                body = new RigidBody(new CapsuleShape(1.0f, 0.5f));
                break;

            case 5:
                Shape b1 = new BoxShape(new JVector(3, 1, 1));
                Shape b2 = new BoxShape(new JVector(1, 1, 3));
                Shape b3 = new CylinderShape(3.0f, 0.5f);

                var t1 = new CompoundShape.TransformedShape(b1, JMatrix.Identity, JVector.Zero);
                var t2 = new CompoundShape.TransformedShape(b2, JMatrix.Identity, JVector.Zero);
                var t3 = new CompoundShape.TransformedShape(b3, JMatrix.Identity, new JVector(0, 0, 0));

                var ms = new CompoundShape(new CompoundShape.TransformedShape[3] {
                    t1, t2, t3
                });

                body = new RigidBody(ms);
                break;

            case 6:
                var obj2 = new ConvexHullObject(this);
                Components.Add(obj2);
                body = obj2.body;
                body.Material.Restitution    = 0.2f;
                body.Material.StaticFriction = 0.8f;
                break;
            }

            World.AddBody(body);
            body.Position       = position;
            body.LinearVelocity = velocity;
            lastBody            = body;
        }
Esempio n. 10
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            if (TimePanel.Instance.Restarted && TimePanel.Instance.TrackbarValue == 0)
            {
                ObjRef[] objrefs;
                Result   rc = Rhino.Input.RhinoGet.GetMultipleObjects("Select the polysurfaces for the union",
                                                                      false, ObjectType.Brep, out objrefs);
                if (rc != Result.Success)
                {
                    return(rc);
                }
                if (objrefs == null || objrefs.Length <= 1)
                {
                    Dialogs.ShowMessage("You have to select 2 or more shapes.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                    return(Result.Failure);
                }


                List <Brep> in_breps0 = new List <Brep>();
                for (int i = 0; i < objrefs.Length; i++)
                {
                    int index = RigidBodyManager.GuidList.IndexOf(objrefs[i].ObjectId);
                    //Avoid to create a compound from another compound
                    if (RigidBodyManager.RigidBodies[index].Shape is CompoundShape)
                    {
                        Dialogs.ShowMessage("You cannot create compound shape from another compound shape. Try to create it at once.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                        return(Result.Failure);
                    }
                    //Accept shapes only if they intersect to each other
                    Brep brep = objrefs[i].Brep();
                    if (brep != null)
                    {
                        in_breps0.Add(brep);
                    }
                    RhinoDoc.ActiveDoc.Objects.Delete(objrefs[i], true);
                }

                //Create the rhino compound shape
                double tolerance = doc.ModelAbsoluteTolerance;
                Brep[] breps     = Brep.CreateBooleanUnion(in_breps0, tolerance);
                if (breps.Length > 1)
                {
                    Dialogs.ShowMessage("You cannot create more than a compound shape in once time.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                    return(Result.Failure);
                }

                Brep rhinoCompound = breps[0];
                // If the user create zero or more than 1 compound the command fails
                if (breps.Length != 1)
                {
                    return(Rhino.Commands.Result.Nothing);
                }

                Brep copyToAdd = rhinoCompound.DuplicateBrep();

                //Create the rigid compound shape
                CompoundShape.TransformedShape[] transformedShapes = new CompoundShape.TransformedShape[in_breps0.Count];

                for (int i = 0; i < in_breps0.Count; i++)
                {
                    Guid guid           = objrefs[i].ObjectId;
                    int  indexRigidBody = RigidBodyManager.GuidList.IndexOf(guid);
                    transformedShapes[i] = new CompoundShape.TransformedShape(RigidBodyManager.RigidBodies[indexRigidBody].Shape, RigidBodyManager.RigidBodies[indexRigidBody].Orientation, RigidBodyManager.RigidBodies[indexRigidBody].Position);
                }
                CompoundShape jCompound     = new CompoundShape(transformedShapes);
                RigidBody     jCompoundBody = new RigidBody(jCompound);

                //Move the center of mass of Jitter shape on the center of the BBox of rhino shape
                Point3d centerBbox = rhinoCompound.GetBoundingBox(true).Center;
                jCompoundBody.Position = RigidBodyManager.Point3dtoJVector(centerBbox);
                //Find the difference between rhino bbx center and jitter bbox center
                JVector bboxjitter = jCompoundBody.BoundingBox.Center;
                JVector diff       = bboxjitter - RigidBodyManager.Point3dtoJVector(centerBbox);
                //Align the center of both bboxes
                jCompoundBody.Position -= diff;

                //Translate the center of the Bbox to 0,0,0 and save it to geometry list
                Point3d bboxDoc = rhinoCompound.GetBoundingBox(true).Center;
                rhinoCompound.Translate(new Vector3d(-bboxDoc.X, -bboxDoc.Y, -bboxDoc.Z));

                RigidBodyManager.RigidBodies.Add(jCompoundBody);
                RigidBodyManager.GeometryList.Add(rhinoCompound);
                Guid guidToAdd = doc.Objects.Add(copyToAdd);
                RigidBodyManager.GuidList.Add(guidToAdd);

                doc.Views.Redraw();

                return(Result.Success);
            }
            else
            {
                Dialogs.ShowMessage("Press Restart before use other commands", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                return(Result.Success);
            }
        }
        protected override void Update(GameTime gameTime)
        {
            if (codeFormVisible)
            {
                return;
            }

            KeyboardState keyState   = Keyboard.GetState();
            MouseState    mouseState = Mouse.GetState();

            if (keyState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            bool leftHold  = (mouseState.LeftButton == ButtonState.Pressed);
            bool spaceHold = (keyState.IsKeyDown(Keys.Space));
            bool enterHold = (keyState.IsKeyDown(Keys.Enter));
            bool mHold     = (keyState.IsKeyDown(Keys.M));

            #region Turn multithreading on/off
            if (mHold)
            {
                if (!mClicked)
                {
                    multithread = !multithread; mClicked = true;
                }
            }
            #endregion

            #region Object drag & drop

            if (leftHold && !leftClicked)
            {
                JVector ray  = Conversion.ToJitterVector(RayTo(mouseState.X, mouseState.Y)); ray.Normalize();
                JVector camp = Conversion.ToJitterVector(Camera.Position);

                float fraction;
                bool  result = world.CollisionSystem.Raycast(camp, ray * 100, RaycastCallback, out resBody, out hitNormal, out fraction);

                if (result)
                {
                    hitPoint = camp + fraction * ray * 100;

                    if (wp != null)
                    {
                        world.RemoveConstraint(wp);
                    }

                    JVector lanchor = hitPoint - resBody.Position;
                    lanchor = JVector.Transform(lanchor, JMatrix.Transpose(resBody.Orientation));

                    wp = new WorldPointConstraint(resBody, lanchor);

                    world.AddConstraint(wp);
                    hitDistance = (Conversion.ToXNAVector(hitPoint) - Camera.Position).Length();
                    scrollWheel = mouseState.ScrollWheelValue;
                    wp.Anchor   = hitPoint;
                }

                leftClicked = true;
            }

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                hitDistance += (mouseState.ScrollWheelValue - scrollWheel) * 0.001f;
                scrollWheel  = mouseState.ScrollWheelValue;

                if (resBody != null)
                {
                    Vector3 ray = RayTo(mouseState.X, mouseState.Y); ray.Normalize();
                    wp.Anchor = Conversion.ToJitterVector(Camera.Position + ray * hitDistance);
                }
            }
            else
            {
                resBody = null;
                if (wp != null)
                {
                    world.RemoveConstraint(wp);
                }
            }

            #endregion

            #region Show code form

            if (enterHold && !enterClicked && gameTime.TotalGameTime.TotalSeconds > 0.1f)
            {
                display.DisplayText[5] = string.Empty;

                System.Windows.Forms.Form form =
                    (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(this.Window.Handle);

                codeFormVisible = true;
                System.Windows.Forms.DialogResult result = codeForm.ShowDialog(form);
                codeFormVisible = false;

                enterClicked = true;
            }
            #endregion

            #region Spawn random primitive
            if (spaceHold && !spaceClicked)
            {
                int rndn = random.Next(6);

                RigidBody body;

                if (rndn == 0)
                {
                    body = new RigidBody(new ConeShape((float)random.Next(5, 50) / 20.0f, (float)random.Next(10, 20) / 20.0f));
                }
                else if (rndn == 1)
                {
                    body = new RigidBody(new BoxShape(new JVector(
                                                          (float)random.Next(10, 30) / 20.0f,
                                                          (float)random.Next(10, 30) / 20.0f, (float)random.Next(10, 30) / 20.0f)));
                }
                else if (rndn == 2)
                {
                    body = new RigidBody(new SphereShape((float)random.Next(30, 100) / 100.0f));
                }
                else if (rndn == 3)
                {
                    body = new RigidBody(new CylinderShape(1.0f, 0.5f));
                }
                else if (rndn == 4)
                {
                    body = new RigidBody(new CapsuleShape(1.0f, 0.5f));
                }
                else
                {
                    Shape b1 = new BoxShape(new JVector(3, 1, 1));
                    Shape b2 = new BoxShape(new JVector(1, 1, 3));
                    Shape b3 = new CylinderShape(2.0f, 0.5f);

                    CompoundShape.TransformedShape t1 = new CompoundShape.TransformedShape(b1, JMatrix.Identity, JVector.Zero);
                    CompoundShape.TransformedShape t2 = new CompoundShape.TransformedShape(b2, JMatrix.Identity, JVector.Zero);
                    CompoundShape.TransformedShape t3 = new CompoundShape.TransformedShape(b3, JMatrix.Identity, new JVector(0, 0, 0));

                    CompoundShape ms = new CompoundShape(new CompoundShape.TransformedShape[3] {
                        t1, t2, t3
                    });

                    body = new RigidBody(ms);
                }

                world.AddBody(body);

                body.Position       = Conversion.ToJitterVector(Camera.Position);
                body.LinearVelocity = Conversion.ToJitterVector((Camera.Target - Camera.Position) * 40.0f);
                body.Update();

                spaceClicked = true;
            }
            #endregion

            spaceClicked = spaceHold;
            leftClicked  = leftHold;
            enterClicked = enterHold;
            mClicked     = mHold;

            int contactCount = 0;
            foreach (Arbiter ar in world.ArbiterMap.Values)
            {
                contactCount += ar.ContactList.Count;
            }

            display.DisplayText[0] = "Arbitercount: " + world.ArbiterMap.Values.Count.ToString() + ";" + " Contactcount: " + contactCount.ToString();
            display.DisplayText[2] = "Bodycount: " + world.RigidBodies.Count;
            display.DisplayText[3] = (multithread) ? "Multithreaded" : "Single Threaded";

            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (elapsedTime > 1.0f / 100.0f)
            {
                elapsedTime = 1.0f / 100.0f;
            }
            world.Step(elapsedTime, multithread);

            base.Update(gameTime);
        }