Ejemplo n.º 1
0
        protected override void Open()
        {
            dispose += DemoHelper.BasicDemoSetup(DemoInfo);
            dispose += DemoHelper.CreateTank(DemoInfo, new Vector2D(50, 0));
            List <Body> bodies = new List <Body>();

            dispose += DemoHelper.CreateTank(DemoInfo, new Vector2D(250, 0), bodies);

            Scene.Engine.AddLogic(new GravityField(new Vector2D(0, 1000), new Lifespan()));

            DemoHelper.AddFloor(DemoInfo, new ALVector2D(0, new Vector2D(700, 750)));

            IShape shape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("fighter.png"), 3, 16, 4);

            for (int i = 128 * 3; i > -128; i -= 128)
            {
                Body b = DemoHelper.AddShape(DemoInfo, shape, 40, new ALVector2D(1, new Vector2D(700, 272 + i)));
                //b.Transformation *= Matrix2x3.FromScale(new Vector2D(1, .5f));
            }

            Body line = DemoHelper.AddLine(DemoInfo, new Vector2D(300, 400), new Vector2D(600, 400), 20, Scalar.PositiveInfinity);

            line.IgnoresGravity = true;
            GroupedOneWayPlatformIgnorer ignorer = new GroupedOneWayPlatformIgnorer(-Vector2D.YAxis, 10);

            ignorer.AddGroup(bodies.ToArray());
            line.CollisionIgnorer = ignorer;

            Body ball = DemoHelper.AddCircle(DemoInfo, 80, 20, 4000, new ALVector2D(0, 1028, 272));

            ball.Transformation *=
                Matrix2x3.FromRotationZ(1) *
                Matrix2x3.FromScale(new Vector2D(.9f, .5f)) *
                Matrix2x3.FromRotationZ(-1);
        }
Ejemplo n.º 2
0
 protected internal override void UpdateTime(TimeStep step)
 {
     time  += step.Dt;
     radius = 1 + time * pressurePulseSpeed;
     explosionBody.Transformation = Matrix2x3.FromScale(new Vector2D(radius, radius));
     base.UpdateTime(step);
 }
Ejemplo n.º 3
0
        protected override void Open()
        {
            IShape shape = ShapeFactory.CreateColoredCircle(8, 15);

            DemoHelper.AddGrid(DemoInfo, shape, 40,
                               new BoundingRectangle(-500, 200, 800, 800),
                               50, 50);

            Viewport viewport2 = new Viewport(new Rectangle(Window.Size.Width / 2, 0, Window.Size.Width / 2, Window.Size.Height), Matrix2x3.Identity, Scene, new Lifespan());

            Window.AddViewport(viewport2);
            Viewport.Rectangle = new Rectangle(0, 0, Window.Size.Width / 2, Window.Size.Height);

            IShape furyShape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("starfury.png"), 2, 16, 3);
            Body   b1        = DemoHelper.AddShape(DemoInfo, furyShape, 400, new ALVector2D(0, 0, 0));
            Body   b2        = DemoHelper.AddShape(DemoInfo, furyShape, 400, new ALVector2D(0, 0, 0));


            dispose += DemoHelper.RegisterBodyTracking(DemoInfo, b1, Matrix2x3.FromRotationZ(-MathHelper.PiOver2) * Matrix2x3.FromScale(new Vector2D(.5f, .5f)));
            dispose += DemoHelper.RegisterBodyMovement(DemoInfo, b1, new ALVector2D(1000000, 100000, 0), Key.W, Key.S, Key.A, Key.D);
            DoGuns(b1, b2, Key.Q);

            dispose += DemoHelper.RegisterBodyTracking(
                new DemoOpenInfo(Window, viewport2, Scene)
                , b2, Matrix2x3.FromRotationZ(-MathHelper.PiOver2) * Matrix2x3.FromScale(new Vector2D(.5f, .5f)));
            dispose += DemoHelper.RegisterBodyMovement(
                DemoInfo, b2, new ALVector2D(1000000, 100000, 0));
            DoGuns(b2, b1, Key.Return);

            b1.State.Position.Linear = new Vector2D(200, 0);
            b2.State.Position        = new ALVector2D(MathHelper.Pi, 1, 0);
            b1.ApplyPosition();
            b2.ApplyPosition();

            DemoHelper.AddShell(DemoInfo, new BoundingRectangle(-700, -700, 1000, 1000), 90, Scalar.PositiveInfinity);

            dispose += delegate()
            {
                viewport2.Lifetime.IsExpired = true;
                Viewport.Rectangle           = new Rectangle(0, 0, Window.Size.Width, Window.Size.Height);
            };

            DemoHelper.AddStarField(DemoInfo, 1000, new BoundingRectangle(-1000, -1000, 1300, 1300));
        }
Ejemplo n.º 4
0
        void OnContactMoved(object sender, ContactEventArgs e)
        {
            Point position = e.GetPosition(this);

            FixedHingeJoint joint;

            if (contactJoints.TryGetValue(e.Contact.Id, out joint))
            {
                joint.Anchor = position.ToVector2D();

                //scale
                Body             body             = joint.Bodies.First();
                FrameworkElement frameworkElement = body.Tag as FrameworkElement;
                if (frameworkElement != null && GetIsScalable(frameworkElement))
                {
                    ScaleState state;
                    if (elementToScale.TryGetValue(frameworkElement, out state))
                    {
                        IEnumerable <Contact> contacts = MultitouchScreen.GetContactsCaptured((IInputElement)e.Source);
                        double    previousDistance     = 0;
                        double    currentDistance      = 0;
                        int       divisor       = 0;
                        Contact[] contactsArray = contacts.ToArray();

                        Point center = new Point(frameworkElement.ActualWidth / 2, frameworkElement.ActualHeight / 2);

                        for (int i = 0; i < contactsArray.Length; i++)
                        {
                            for (int j = i + 1; j < contactsArray.Length; j++)
                            {
                                Point  currFirst  = contactsArray[j].GetPosition(this);
                                Point  currSecond = contactsArray[i].GetPosition(this);
                                Vector vector     = frameworkElement.PointFromScreen(currFirst) - frameworkElement.PointFromScreen(currSecond);
                                currentDistance += vector.Length;

                                Point prevFirst = contactsArray[j].GetPoints(this).FirstOrDefault();
                                if (default(Point) == prevFirst)
                                {
                                    prevFirst = currFirst;
                                }
                                Point prevSecond = contactsArray[i].GetPoints(this).FirstOrDefault();
                                if (default(Point) == prevSecond)
                                {
                                    prevSecond = currSecond;
                                }
                                Vector previousVector = frameworkElement.PointFromScreen(prevFirst) - frameworkElement.PointFromScreen(prevSecond);
                                previousDistance += previousVector.Length;
                                divisor++;
                            }
                        }
                        if (divisor == 0)
                        {
                            divisor = 1;
                        }

                        previousDistance /= divisor;
                        currentDistance  /= divisor;

                        double delta = currentDistance / previousDistance;
                        if (double.IsNaN(delta))
                        {
                            delta = 1;
                        }

                        var newScale = state.Scale * delta;
                        if (newScale > MaxScale)
                        {
                            delta = MaxScale / state.Scale;
                        }
                        else if (newScale < MinScale)
                        {
                            delta = MinScale / state.Scale;
                        }

                        state.Scale         *= delta;
                        state.Center         = center;
                        body.Transformation *= Matrix2x3.FromScale(new Vector2D(delta, delta));
                    }
                }
            }
        }
Ejemplo n.º 5
0
		private void UpdateSize(Body body, SizeChangedEventArgs e)
		{
			double xScale = e.NewSize.Width / e.PreviousSize.Width;
			double yScale = e.NewSize.Height / e.PreviousSize.Height;
			body.Transformation *= Matrix2x3.FromScale(new Vector2D(xScale, yScale));
		}
Ejemplo n.º 6
0
        public static DisposeCallback RegisterDup(DemoOpenInfo info, List <Body> bodies)
        {
            LinkedList <Body> removable = new LinkedList <Body>();
            Body     body      = null;
            Scalar   rotation  = 0;
            Vector2D scale     = new Vector2D(1, 1);
            bool     duplicate = false;
            bool     makeJoint = false;
            Vector2D anchor    = Vector2D.Zero;
            int      count     = 0;
            Body     bj1       = null;
            Body     bj2       = null;
            EventHandler <ViewportMouseButtonEventArgs> mouseDown = delegate(object sender, ViewportMouseButtonEventArgs e)
            {
                IntersectionInfo temp;
                switch (e.Button)
                {
                case MouseButton.PrimaryButton:
                    if (makeJoint)
                    {
                        count++;
                        switch (count)
                        {
                        case 1:
                            for (LinkedListNode <Body> node = removable.First;
                                 node != null;
                                 node = node.Next)
                            {
                                Vector2D pos = node.Value.Matrices.ToBody * e.Position;
                                if (node.Value.Shape.TryGetIntersection(pos, out temp))
                                {
                                    bj1 = node.Value;
                                    return;
                                }
                            }
                            count--;
                            break;

                        case 2:
                            anchor = e.Position;
                            break;

                        case 3:
                            for (LinkedListNode <Body> node = removable.First;
                                 node != null;
                                 node = node.Next)
                            {
                                Vector2D pos = node.Value.Matrices.ToBody * e.Position;
                                if (node.Value.Shape.TryGetIntersection(pos, out temp))
                                {
                                    bj2 = node.Value;
                                    return;
                                }
                            }
                            count--;
                            break;
                        }
                    }
                    else
                    {
                        foreach (Body b in bodies)
                        {
                            Vector2D pos = b.Matrices.ToBody * e.Position;
                            if (b.Shape.TryGetIntersection(pos, out temp))
                            {
                                body = b.Duplicate();
                                info.Scene.AddGraphic(new BodyGraphic(body));
                                return;
                            }
                        }
                        for (LinkedListNode <Body> node = removable.First;
                             node != null;
                             node = node.Next)
                        {
                            Vector2D pos = node.Value.Matrices.ToBody * e.Position;
                            if (node.Value.Shape.TryGetIntersection(pos, out temp))
                            {
                                if (duplicate)
                                {
                                    body = node.Value.Duplicate();
                                    info.Scene.AddGraphic(new BodyGraphic(body));
                                    return;
                                }
                                else
                                {
                                    body = node.Value;
                                    removable.Remove(node);
                                    return;
                                }
                            }
                        }
                    }
                    break;

                case MouseButton.SecondaryButton:
                    for (LinkedListNode <Body> node = removable.First;
                         node != null;
                         node = node.Next)
                    {
                        Vector2D pos = node.Value.Matrices.ToBody * e.Position;
                        if (node.Value.Shape.TryGetIntersection(pos, out temp))
                        {
                            node.Value.Lifetime.IsExpired = true;
                            removable.Remove(node);
                            return;
                        }
                    }
                    break;
                }
            };
            EventHandler <ViewportMouseMotionEventArgs> mouseMotion = delegate(object sender, ViewportMouseMotionEventArgs e)
            {
                if (body != null)
                {
                    body.State.Position.Linear = e.Position;
                }
            };
            EventHandler <ViewportMouseButtonEventArgs> mouseUp = delegate(object sender, ViewportMouseButtonEventArgs e)
            {
                if (body != null)
                {
                    removable.AddLast(body);
                    body = null;
                }
            };
            EventHandler <KeyboardEventArgs> keyDown = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                case Key.Q:
                    rotation -= .05f;
                    break;

                case Key.E:
                    rotation += .05f;
                    break;

                case Key.W:
                    scale.Y += .05f;
                    break;

                case Key.S:
                    scale.Y -= .05f;
                    break;

                case Key.A:
                    scale.X -= .05f;
                    break;

                case Key.D:
                    scale.X += .05f;
                    break;

                case Key.LeftControl:
                case Key.RightControl:
                    duplicate = true;
                    break;

                case Key.LeftShift:
                case Key.RightShift:
                    count     = 0;
                    makeJoint = true;
                    break;
                }
            };
            EventHandler <KeyboardEventArgs> keyUp = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                case Key.Q:
                    rotation += .05f;
                    break;

                case Key.E:
                    rotation -= .05f;
                    break;

                case Key.W:
                    scale.Y -= .05f;
                    break;

                case Key.S:
                    scale.Y += .05f;
                    break;

                case Key.A:
                    scale.X += .05f;
                    break;

                case Key.D:
                    scale.X -= .05f;
                    break;

                case Key.LeftControl:
                case Key.RightControl:
                    duplicate = false;
                    break;

                case Key.LeftShift:
                case Key.RightShift:
                    switch (count)
                    {
                    case 2:
                        info.Scene.Engine.AddJoint(new FixedHingeJoint(bj1, anchor, new Lifespan()));
                        break;

                    case 3:
                        info.Scene.Engine.AddJoint(new HingeJoint(bj1, bj2, anchor, new Lifespan()));
                        break;
                    }

                    count     = 0;
                    makeJoint = false;
                    break;
                }
            };
            EventHandler <DrawEventArgs> onDraw = delegate(object sender, DrawEventArgs e)
            {
                if (body != null)
                {
                    body.State.Position.Angular += rotation;
                    body.Transformation         *= Matrix2x3.FromScale(scale);
                    body.ApplyPosition();
                }
            };

            info.Scene.BeginDrawing   += onDraw;
            info.Viewport.MouseDown   += mouseDown;
            info.Viewport.MouseMotion += mouseMotion;
            info.Viewport.MouseUp     += mouseUp;
            Events.KeyboardDown       += keyDown;
            Events.KeyboardUp         += keyUp;
            return(delegate()
            {
                info.Scene.BeginDrawing -= onDraw;
                info.Viewport.MouseDown -= mouseDown;
                info.Viewport.MouseMotion -= mouseMotion;
                info.Viewport.MouseUp -= mouseUp;
                Events.KeyboardDown -= keyDown;
                Events.KeyboardUp -= keyUp;
            });
        }
Ejemplo n.º 7
0
        public void OnContactMoved(IntPtr windowHandle, Multitouch.Contracts.IContact contact)
        {
            var w = (from win in windowToBody.Keys
                     where win.Contacts.Contains(contact.Id)
                     select win).SingleOrDefault();
            WindowContact winContact = null;

            if (w != null)
            {
                if (w.Contacts.TryGetValue(contact.Id, out winContact))
                {
                    winContact.Update(contact);
                }
            }
            Window window = GetWindow(windowHandle);

            if (window != null)
            {
                FixedHingeJoint joint;
                if (contactJoints.TryGetValue(contact.Id, out joint))
                {
                    joint.Anchor = new Vector2D(contact.X, contact.Y);

                    // scale
                    Body                 body             = joint.Bodies.First();
                    WindowContacts       contacts         = window.Contacts;
                    double               previousDistance = 0;
                    double               currentDistance  = 0;
                    int                  divisor          = 0;
                    RECT                 windowRect       = window.Rectangle;
                    System.Windows.Point center           = new System.Windows.Point(windowRect.Width / 2.0, windowRect.Height / 2.0);
                    WindowContact[]      contactsArray    = contacts.ToArray();
                    for (int i = 0; i < contactsArray.Length; i++)
                    {
                        for (int j = i + 1; j < contactsArray.Length; j++)
                        {
                            Vector vector = NativeMethods.ScreenToClient(window, contactsArray[j].Position.ToPoint()).ToPoint() -
                                            NativeMethods.ScreenToClient(window, contactsArray[i].Position.ToPoint()).ToPoint();
                            currentDistance += vector.Length;
                            center          += vector;

                            Vector previousVector = NativeMethods.ScreenToClient(window, contactsArray[j].PreviousPosition.ToPoint()).ToPoint() -
                                                    NativeMethods.ScreenToClient(window, contactsArray[i].PreviousPosition.ToPoint()).ToPoint();
                            previousDistance += previousVector.Length;
                            divisor++;
                        }
                    }
                    if (divisor == 0)
                    {
                        divisor = 1;
                    }

                    previousDistance /= divisor;
                    currentDistance  /= divisor;
                    center.X         /= divisor;
                    center.Y         /= divisor;

                    double delta = currentDistance / previousDistance;
                    if (double.IsNaN(delta))
                    {
                        delta = 1;
                    }
                    window.Scale        *= delta;
                    window.ScaleCenter   = center;
                    body.Transformation *= Matrix2x3.FromScale(new Vector2D(delta, delta));
                }
            }
            else if (w != null && winContact != null)
            {
                w.Contacts.Remove(contact.Id);
            }
        }