Ejemplo n.º 1
0
        public void processTouchPoints(ReadOnlyTouchPointCollection touches)
        {
            lastTouchPosition = touchPosition;

            int tagID = -1;
            if (touches.Count >= 1)
            {
                for (int i = 0; i < touches.Count; i++)
                {
                    if (touches[i].IsTagRecognized)
                    {
                        tagID = (int)touches[i].Tag.Value;
                        break;
                    }
                }
                if (tagID != -1)
                {
                    touchPosition = touches[0];
                    //First time touch
                    if (lastTouchPosition == null)
                    {
                        Segment s;
                        s.P1 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.CenterX, touchPosition.CenterY, 0f),
                            viewManager.Projection, viewManager.View, Matrix.Identity);
                        s.P2 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.CenterX, touchPosition.CenterY, 1f),
                            viewManager.Projection, viewManager.View, Matrix.Identity);
                        float scalar;
                        Vector3 point;
                        var c = physics.BroadPhase.Intersect(ref s, out scalar, out point);

                        if (c != null && c is BodySkin)
                        {
                            pickedObject = ((BodySkin)c).Owner;

                            pickedForce = new WorldPointConstraint(pickedObject, point);
                            physics.Add(pickedForce);
                            pickedDistance = scalar;
                            pickedObject.IsActive = true;
                        }
                        lastOrientation = touches.Count == 1 ? touches[0].Orientation : touches[1].Orientation;
                    }
                    else if (pickedObject != null)
                    {
                        Segment s;
                        s.P1 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.CenterX, touchPosition.CenterY, 0f),
                            viewManager.Projection, viewManager.View, Matrix.Identity);
                        s.P2 = game.GraphicsDevice.Viewport.Unproject(new Vector3(touchPosition.CenterX, touchPosition.CenterY, 1f),
                            viewManager.Projection, viewManager.View, Matrix.Identity);
                        Vector3 diff, point;
                        Vector3.Subtract(ref s.P2, ref s.P1, out diff);
                        Vector3.Multiply(ref diff, pickedDistance, out diff);
                        Vector3.Add(ref s.P1, ref diff, out point);
                        pickedForce.WorldPoint = point;
                        pickedObject.IsActive = true;

                        //SolidThing po = (SolidThing)pickedObject;
                        //Console.Out.WriteLine("UNFREEEEEEEEEEEEZE");
                        switch (tagID)
                        {
                            
                            //Pin a block
                            case 0:
                                pickedObject.Freeze();
                                break;
                            //unPin a block
                            case 1:
                                
                                pickedObject.Unfreeze();
                                break;
                            //Rotate a block
                            case 2:
                                pickedForce.orientation = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1.0f), touchPosition.Orientation);
                                break;
                            //Move a block towards or away from camera
                            case 3:
                                TouchPoint tagPoint = touches[0];
                                float deltaRotation = MathHelper.ToDegrees(lastOrientation) - MathHelper.ToDegrees(tagPoint.Orientation);

                                Vector3 direction = new Vector3(0, 0, 1.0f);
                                direction.Normalize();
                                pickedForce.WorldPoint = Vector3.Add(pickedForce.WorldPoint, Vector3.Multiply(direction, deltaRotation * 0.01f));

                                break;
                            //Rotate stack onto top view
                            /*case 4:
                                viewManager.rotateToSide(4, touch);
                                break;
                            //Corkscrew closer or further away
                            case 5:
                                viewManager.rotateToSide(5);
                                break;
                            case 6:
                                viewManager.rotateToSide(6);
                                break;
                            case 7:
                                viewManager.rotateToSide(7);
                                break;
                            case 8:
                                viewManager.rotateToSide(8);
                                break;
                                */

                        }
                    }
                    else if (pickedObject != null)
                    {
                        physics.Remove(pickedForce);
                        pickedObject = null;
                    }
                }
            }
            else if (pickedObject != null)
            {
                physics.Remove(pickedForce);
                pickedObject = null;
                touchPosition = null;
                lastTouchPosition = null;
            }
            else
            {
                touchPosition = null;
            }

        }
Ejemplo n.º 2
0
        public void CreateScene(int sceneNumber)
        {
            _physics.Clear();
            _markers.Clear();

            Room room = new Room(this);
            _physics.Add(room);
            _physics.Gravity = new Vector3(0f, 0f, -9.8f);

            Model cubeModel = this.Content.Load<Model>("models/small_cube");
            Model obeliskModel = this.Content.Load<Model>("models/obelisk");
            Model sphereModel = this.Content.Load<Model>("models/sphere");
            Model capsuleModel = this.Content.Load<Model>("models/capsule");
            Model torusModel = this.Content.Load<Model>("models/torus");
            Model slabModel = this.Content.Load<Model>("models/slab");
            Model triangleModel = this.Content.Load<Model>("models/triangle");

            switch (sceneNumber)
            {
                case 1:
                    {
                        for (int i = 0; i < 12; i++)
                        {
                            var cube = new SolidThing(this, cubeModel);
                            cube.SetWorld(new Vector3(0f, 0f, 0.25f + 0.51f * i));
                            _physics.Add(cube);
                        }
                    }
                    break;
                case 2:
                    {
                        for (int i = 0; i < 7; i++)
                        {
                            for (int j = 0; j < 7 - i; j++)
                            {
                                var cube = new SolidThing(this, cubeModel);
                                cube.SetWorld(new Vector3(0f, 0.501f * j + 0.25f * i, 0.5f + 0.55f * i));
                                _physics.Add(cube);
                            }
                        }
                    }
                    break;
                case 3:
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            for (int j = 0; j < 6 - i; j++)
                            {
                                var cube = new SolidThing(this, cubeModel);
                                cube.SetWorld(new Vector3(0f, 2.2f * j + 1f * i - 4.1f, 0.75f * i + 0.25f));
                                _physics.Add(cube);
                            }
                        }
                        for (int i = 0; i < 6; i++)
                        {
                            for (int j = 0; j < 5 - i; j++)
                            {
                                var plank = new SolidThing(this, obeliskModel);
                                plank.SetWorld(new Vector3(0f, 2.2f * j + 1f * i + 1f - 4f, 0.75f * i + 0.65f),
                                    Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.ToRadians(90f)));
                                _physics.Add(plank);
                            }
                        }
                    }
                    break;
                case 4:
                    {
                        int size = 9;
            #if WINDOWS
            #else
                        size = 4;
            #endif
                        for (int i = 0; i < size; i++)
                        {
                            for (int j = 0; j < size - i; j++)
                            {
                                for (int k = 0; k < size - i; k++)
                                {
                                    var sphere = new SolidThing(this, sphereModel);
                                    sphere.SetWorld(new Vector3(
                                        0.501f * j + 0.25f * i,
                                        0.501f * k + 0.25f * i,
                                        0.501f * i + 0.5f
                                        ), Quaternion.Identity);
                                    _physics.Add(sphere);
                                }
                            }
                        }
                    }
                    break;
                case 5:
                    {

                        var plank = new SolidThing(this, obeliskModel);
                        plank.SetWorld(new Vector3(0.0f, 0.0f, 4.0f),
                            Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathHelper.ToRadians(15f)));
                        MassProperties immovableMassProperties = new MassProperties(float.PositiveInfinity, Matrix.Identity);
                        plank.MassProperties = immovableMassProperties;
                        _physics.Add(plank);

                        var sphere = new SolidThing(this, sphereModel);
                        sphere.SetWorld(new Vector3(-4.9f, 0.0f, 9.0f), Quaternion.Identity);
                        _physics.Add(sphere);

            //                        int size = 9;
            //#if WINDOWS
            //#else
            //                        size = 4;
            //#endif
            //                        var models = new Model[] { cubeModel, sphereModel };
            //                        for (int i = 0; i < size; i++)
            //                        {
            //                            for (int j = 0; j < size - i; j++)
            //                            {
            //                                for (int k = 0; k < size - i; k++)
            //                                {
            //                                    var sphere = new SolidThing(this, i % 2 == 0 ? sphereModel : cubeModel);
            //                                    sphere.SetWorld(new Vector3(
            //                                        0.501f * j + 0.25f * i,
            //                                        0.501f * k + 0.25f * i,
            //                                        0.501f * i + 0.5f
            //                                        ), Quaternion.Identity);
            //                                    _physics.Add(sphere);
            //                                }
            //                            }
            //                        }
                    }
                    break;
                case 6:
                    {
                        int size = 9;
            #if WINDOWS
            #else
                        size = 4;
            #endif
                        var models = new Model[] { cubeModel, sphereModel, capsuleModel };
                        for (int i = 0; i < size; i++)
                        {
                            for (int j = 0; j < size - i; j++)
                            {
                                for (int k = 0; k < size - i; k++)
                                {
                                    var sphere = new SolidThing(this, models[_rand.Next(3)]);
                                    sphere.SetWorld(new Vector3(
                                        0.501f * j + 0.25f * i,
                                        0.501f * k + 0.25f * i,
                                        1f * i + 0.5f));
                                    _physics.Add(sphere);
                                }
                            }
                        }
                    }
                    break;
                case 7:
                    {
                        var o = new SolidThing(this, torusModel);
                        o.SetWorld(new Vector3(0f, 0f, 0.5f), Quaternion.CreateFromAxisAngle(Vector3.UnitY, -MathHelper.PiOver2));
                        _physics.Add(o);

                        o = new SolidThing(this, torusModel);
                        o.SetWorld(new Vector3(0f, 0f, 4f), Quaternion.CreateFromAxisAngle(Vector3.UnitY, -MathHelper.PiOver4));
                        _physics.Add(o);

                        o = new SolidThing(this, slabModel);
                        o.SetWorld(new Vector3(-4f, 4f, 2f));
                        _physics.Add(o);

                        o = new SolidThing(this, this.Content.Load<Model>("models/cone"));
                        o.SetWorld(new Vector3(-4f, -4f, 1f), Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.PiOver2));
                        _physics.Add(o);

                        o = new SolidThing(this, cubeModel);
                        o.SetWorld(new Vector3(-4f, 6.1f, 3f));
                        _physics.Add(o);
                    }
                    break;
                case 8:
                    {
                        RigidBody oLast = null;
                        for (int i = 0; i < 10; i++)
                        {
                            var o = new SolidThing(this, capsuleModel);
                            o.SetWorld(new Vector3(0f, 0f, 9.5f - i));
                            _physics.Add(o);
                            if (i == 0)
                            {
                                var j = new PointConstraint(o, room, new Vector3(0f, 0f, 10f));
                                j.IsCollisionEnabled = false;
                                _physics.Add(j);
                            }
                            else
                            {
                                var j = new PointConstraint(oLast, o, new Vector3(0f, 0f, 10f - (float)i));
                                j.IsCollisionEnabled = false;
                                _physics.Add(j);
                            }
                            oLast = o;
                        }

                        var a = new SolidThing(this, cubeModel);
                        a.SetWorld(new Vector3(1f, 0f, 0.25f));
                        _physics.Add(a);
                        var b = new SolidThing(this, cubeModel);
                        b.SetWorld(new Vector3(1f, 0f, 0.75f));
                        _physics.Add(b);
                        var j2 = new RevoluteJoint(b, a, new Vector3(1.25f, 0f, 0.5f), Vector3.UnitY,
                            0f, MathHelper.PiOver2);
                        j2.IsCollisionEnabled = false;
                        _physics.Add(j2);

                        a = new SolidThing(this, cubeModel);
                        a.SetWorld(new Vector3(1f, 1f, 0.25f));
                        _physics.Add(a);
                        b = new SolidThing(this, cubeModel);
                        b.SetWorld(new Vector3(1f, 1f, 0.75f));
                        _physics.Add(b);
                        var j4 = new GenericConstraint(b, a, new Frame(new Vector3(1f, 1f, 0.5f)),
                            Axes.All, Vector3.Zero, new Vector3(0f, 0f, 0.5f),
                            Axes.All, Vector3.Zero, Vector3.Zero);
                        j4.IsCollisionEnabled = false;
                        _physics.Add(j4);

                        a = new SolidThing(this, cubeModel);
                        a.SetWorld(new Vector3(1f, 2f, 0.25f));
                        _physics.Add(a);
                        b = new SolidThing(this, cubeModel);
                        b.SetWorld(new Vector3(1f, 2f, 0.75f));
                        _physics.Add(b);
                        var j5 = new GenericConstraint(b, a, new Frame(new Vector3(1f, 2f, 0.5f)),
                            Axes.All, new Vector3(-0.125f, -0.125f, 0f), new Vector3(0.125f, 0.125f, 0f),
                            Axes.All, Vector3.Zero, Vector3.Zero);
                        j5.IsCollisionEnabled = false;
                        _physics.Add(j5);

                        a = new SolidThing(this, sphereModel);
                        a.SetWorld(new Vector3(2f, 0f, 2f));
                        _physics.Add(a);
                        b = new SolidThing(this, sphereModel);
                        b.SetWorld(new Vector3(2f, 0f, 1f));
                        _physics.Add(b);
                        var g1 = new SpringForce(a, b, Vector3.Zero, Vector3.Zero, 1f, 5f, 0.05f);
                        _physics.Add(g1);
                        var j3 = new WorldPointConstraint(a, new Vector3(2f, 0f, 2f));
                        _physics.Add(j3);
                    }
                    break;
                case 9:
                    {
                        var a = new SolidThing(this, sphereModel);
                        a.Skin.Remove(a.Skin[0]);
                        a.Skin.Add(new SpherePart(new Sphere(Vector3.Zero, 0.25f)), new Material(0f, 0.5f));
                        a.SetWorld(7.0f, new Vector3(0f, 0f, 5f), Quaternion.Identity);
                        a.MassProperties = MassProperties.Immovable;
                        _physics.Add(a);

                        _physics.Add(new SingularityForce(new Vector3(0f, 0f, 5f), 1E12f));

                        _physics.Gravity = Vector3.Zero;

                        var b = new SolidThing(this, cubeModel);
                        b.SetWorld(new Vector3(0f, 0f, 8f),
                            Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.PiOver4 / 2.0f) *
                            Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathHelper.PiOver4)
                            );
                        _physics.Add(b);
                    }
                    break;
                default:
                    break;
            }
        }