コード例 #1
0
 private void AddScatterRectangles()
 {
     _pieces = new ScatterRectangle[7];
     for (int i = 0; i < 7; ++i)
     {
         ScatterRectangle rectangle = new ScatterRectangle();
         BitmapImage bi = new BitmapImage();
         bi.BeginInit();
         bi.UriSource =
             new Uri(String.Format("{0}{1}{2}{3}{4}", Environment.CurrentDirectory, Define.ResoucePath,
                                   @"\widgets\tangram", i + 1, ".png"));
         bi.EndInit();
         rectangle.Background = new ImageBrush(bi);
         rectangle.Width = (int)(TangramInformation.TangramWidth[i] * TangramInformation.TangramSize / 100);
         rectangle.Height = (int)(TangramInformation.TangramHeight[i] * TangramInformation.TangramSize / 100);
         rectangle.Category = (TangramCategory)i;
         rectangle.Position = new Point(TangramInformation.TangramInitializationPosition[i].X, TangramInformation.TangramInitializationPosition[i].Y);
         this.Children.Add(rectangle);
         _pieces[i] = rectangle;
     }
 }
コード例 #2
0
        public static IShape CreateShape(ScatterRectangle target, Scalar width, Scalar height)
        {
            if (width <= 0) { throw new ArgumentOutOfRangeException("width", "must be greater then 0"); }
            if (height <= 0) { throw new ArgumentOutOfRangeException("height", "must be greater then 0"); }
            Vector2D[] vertexs = null;
            Scalar temp, temp2;

            switch (target.Category)
            {
                case TangramCategory.HorizontalSmallTriangle:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[3]
                    {
                        new Vector2D(temp, temp2),
                        new Vector2D(-temp, temp2),
                        new Vector2D(0, -temp2)
                    };
                    break;
                case TangramCategory.VerticalSmallTriangle:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[3]
                    {
                        new Vector2D(temp, -temp2),
                        new Vector2D(temp, temp2),
                        new Vector2D(-temp, 0)
                    };
                    break;
                case TangramCategory.Prallelogram:
                    temp = height * .5f;
                    temp2 = height + temp;
                    vertexs = new Vector2D[4]
                    {
                        new Vector2D(-temp2, temp),
                        new Vector2D(-temp, -temp),
                        new Vector2D(temp2, -temp),
                        new Vector2D(temp, temp)
                    };
                    break;
                case TangramCategory.HorizontalBigTriangle:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[3]
                    {
                        new Vector2D(-temp, -temp2),
                        new Vector2D(temp, -temp2),
                        new Vector2D(0, temp2)
                    };
                    break;
                case TangramCategory.MiddleTriangle:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[3]
                    {
                        new Vector2D(-temp, temp2),
                        new Vector2D(temp, -temp2),
                        new Vector2D(temp, temp2)
                    };
                    break;
                case TangramCategory.Square:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[4]
                    {
                        new Vector2D(-temp, 0),
                        new Vector2D(0, -temp2),
                        new Vector2D(temp, 0),
                        new Vector2D(0, temp2)
                    };
                    break;
                case TangramCategory.VerticalBigTriangle:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[3]
                    {
                        new Vector2D(-temp, -temp2),
                        new Vector2D(temp, 0),
                        new Vector2D(-temp, temp2)
                    };
                    break;
                default:
                    temp = width * .5f;
                    temp2 = height * .5f;
                    vertexs = new Vector2D[4]
                    {
                        new Vector2D(temp, 0),
                        new Vector2D(0, temp2),
                        new Vector2D(-temp, 0),
                        new Vector2D(0, -temp2)
                    };
                    break;
            }
            return new PolygonShape(vertexs, 2f);
        }
コード例 #3
0
        private void OnNewContact(ScatterRectangle rectangle, FingerEventArgs e)
        {
            int index = (int)rectangle.Category;
            //not a new contact
            if (_elementsContacts[index].ContainsKey(e.FingerID))
                return;

            //e.Position stands the whole table, get the relative position of Target, i.e. Design Window here
            Point position = rectangle.TranslatePoint(e.Position, _canvas);
            Vector2D contactPoint = new Vector2D((Scalar)position.X, (Scalar)position.Y);
            _fingers.Add(position);

            Body body = _bodies[index];
            if (body.Shape.CanGetIntersection)
            {
                FixedHingeJoint joint = PhysicsHelper.FixedHingeJointFactory(body, contactPoint, new Lifespan());
                _engine.AddJoint(joint);
                _contactJoints[e.FingerID] = joint;
                _contactBody[e.FingerID] = body;
                _elementsContacts[index].Add(e.FingerID, new Contact(e.FingerID, position));
            }
            rectangle.CaptureFinger(e.FingerID);
        }
コード例 #4
0
 public static PhysicsState CreateState(ScatterRectangle target, Scalar radius, Scalar width, Scalar height)
 {
     ALVector2D answer;
     answer = new ALVector2D(radius, (float)(target.Position.X + width * .5f), (float)(target.Position.Y + height * .5f));
     return new PhysicsState(answer);
 }
コード例 #5
0
        private void OnContactRemoved(ScatterRectangle rectangle, FingerEventArgs e)
        {
            int index = (int) rectangle.Category;
            if (!_elementsContacts[index].ContainsKey(e.FingerID))
                return;

            FixedHingeJoint removedJoint;
            if (_contactJoints.TryGetValue(e.FingerID, out removedJoint))
            {
                removedJoint.Lifetime.IsExpired = true;
                _contactBody.Remove(e.FingerID);
                _contactJoints.Remove(e.FingerID);

            }
            _elementsContacts[index].Remove(e.FingerID);
            _fingers.Remove(e.Position);
        }
コード例 #6
0
        private void OnContactMove(ScatterRectangle rectangle, FingerEventArgs e)
        {
            int index = (int)rectangle.Category;
            if (!_elementsContacts[index].ContainsKey(e.FingerID)) return;
            Point position = rectangle.TranslatePoint(e.Position, _canvas);
            _elementsContacts[index][e.FingerID].UpdatePosition(position);

            FixedHingeJoint joint;
            if (_contactJoints.TryGetValue(e.FingerID, out joint))
            {
                // move
                joint.Anchor = new Vector2D((Scalar)position.X, (Scalar)position.Y);
            }
        }
コード例 #7
0
 private ScatterRectangle HitTest(ScatterRectangle rectangle, FingerEventArgs args)
 {
     Point global = rectangle.TranslatePoint(args.Position, _canvas), temp;
     ScatterRectangle curr;
     BitmapImage bi;
     byte[] pixel = new byte[4];
     for (int i = 0; i < _canvas.Pieces.Length; ++i)
     {
         curr = _canvas.Pieces[i];
         temp = _canvas.TranslatePoint(global, curr);
         if (temp.X > curr.Width || temp.X < 0 || temp.Y > curr.Height || temp.Y < 0) continue;
         bi = (curr.Background as ImageBrush).ImageSource as BitmapImage;
         bi.CopyPixels(new System.Windows.Int32Rect((int) (temp.X / curr.Width * bi.PixelWidth),
                                                    (int) (temp.Y / curr.Height * bi.PixelHeight), 1, 1), pixel,
                       bi.PixelWidth * 4, 0);
         if (pixel[3] != 0)
             return curr;
     }
     return null;
 }
コード例 #8
0
 private void GetMultiTouchInput(ScatterRectangle rectangle, MultiTouchEventArgs args)
 {
     ScatterRectangle obj;
     foreach (FingerEventArgs e in args.FingerEvents)
     {
         switch (e.EventType)
         {
             case FingerEventType.FINGER_DOWN_EVENT:
             case FingerEventType.FINGER_IN_EVENT:
                 obj = HitTest(rectangle, e); // Since each ScatterRectangle is a rectangle but actually some are triangles, we need get the real "rectangle" according to the color of the mouse hit position
                 if (obj == null) return;
                 OnNewContact(obj, e);
                 break;
             case FingerEventType.FINGER_MOVE_EVENT:
                 OnContactMove(rectangle, e);
                 break;
             case FingerEventType.FINGER_OUT_EVENT:
             case FingerEventType.FINGER_UP_EVENT:
                 OnContactRemoved(rectangle, e);
                 break;
         }
     }
     args.Handled = true;
 }
コード例 #9
0
        private Body CreateBody(ScatterRectangle rectangle)
        {
            float radius = MathHelper.ToRadians((Scalar) rectangle.Orientation);
            PhysicsState state = PhysicsHelper.CreateState(rectangle, radius, (Scalar)rectangle.Width, (Scalar)rectangle.Height);
            IShape shape = PhysicsHelper.CreateShape(rectangle, (Scalar) rectangle.Width, (Scalar) rectangle.Height);
            MassInfo mass = MassInfo.FromPolygon(shape.Vertexes, TangramInformation.TangramMass);
            Body body = new Body(state, shape, mass, new Coefficients(TangramInformation.Restitution, TangramInformation.Restitution), new Lifespan());
            body.LinearDamping = TangramInformation.DefaultLinearDamping;
            body.AngularDamping = TangramInformation.DefaultAngularDamping;
            body.Transformation *=
                Matrix2x3.FromScale(new Vector2D((Scalar) rectangle.ScaleX, (Scalar) rectangle.ScaleY));
            body.IsCollidable = true;
            body.Tag = rectangle;
            _engine.AddBody(body);

            ScaleState scale = new ScaleState();
            scale.Scale = rectangle.ScaleX;
            scale.Center = new Point(rectangle.Width/2, rectangle.Height/2);
            _scaleses[(int)rectangle.Category] = scale;
            _elementsContacts[(int) rectangle.Category] = new Dictionary<int, Contact>();
            rectangle.InputReceived += PieceReceiveInput;
            return body;
        }