示例#1
0
        protected override RigidBody GeneratePhysicsDescription()
        {
            // there is almost certainly a better way to do this
            BoundingSphere boundingSphere = model.CalculateBounds();
            Shape collisionShape = new CylinderShape(Scale.Y*4f, boundingSphere.Radius*4f);
            var rigidBody = new RigidBody(collisionShape)
            {
                Position = PhysicsSystem.toJVector(Position),
                Orientation = PhysicsSystem.toJMatrix(OrientationMatrix),
                IsStatic = true,
                EnableDebugDraw = true,
            };

            return rigidBody;
        }
        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);
        }
public void Wall()
{
    Shape shape = new CylinderShape(0.5f, 0.5f);

    for (int i = 0; i < WallSize; i++)
    {
        for (int e = 0; e < WallSize; e++)
        {
            RigidBody body = new RigidBody(shape);
            body.Position = new JVector((i % 2 == 0) ? e : e + 0.5f, i + 0.5f, 0);
            world.AddBody(body);
        }
    }

}
示例#4
0
文件: JitterDemo.cs 项目: tpb3d/TPB3D
        private void SpawnRandomPrimitive(JVector position, JVector velocity)
        {
            RigidBody body = null;
            int rndn = random.Next(7);

            // less of the more advanced objects
            if (rndn == 5 || rndn == 6) rndn = random.Next(7);

            switch (rndn)
            {
                case 0:
                    body = new RigidBody(new ConeShape((float)random.Next(5, 50) / 20.0f, (float)random.Next(10, 20) / 20.0f));
                    break;
                case 1:
                    body = new RigidBody(new BoxShape((float)random.Next(10, 30) / 20.0f, (float)random.Next(10, 30) / 20.0f, (float)random.Next(10, 30) / 20.0f));
                    break;
                case 2:
                    body = new RigidBody(new SphereShape(1.0f));
                    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);

                    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);
                    break;
                case 6:
                    ConvexHullObject 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;
            //body.EnableDebugDraw = true;

            lastBody = body;
        }