void OnNewContact(object sender, NewContactEventArgs e)
        {
            if (!firstContactId.HasValue)
            {
                firstContactId = e.Contact.Id;

                startPoint    = e.GetPosition(this);
                startOffset.X = HorizontalOffset;
                startOffset.Y = VerticalOffset;

                Vector2D contactPoint = startPoint.ToVector2D();
                scrollJoint = new FixedHingeJoint(Body, contactPoint, new Lifespan());
                engine.AddJoint(scrollJoint);
            }
        }
        void OnGotContactCapture(object sender, ContactEventArgs e)
        {
            FrameworkElement element   = (FrameworkElement)e.Source;
            FrameworkElement container = ItemsControl.ContainerFromElement(null, element) as FrameworkElement;

            if (container != null)
            {
                element = container;
            }

            Body body;

            if (elementToBody.TryGetValue(element, out body))
            {
                Point    position     = e.GetPosition(this);
                Vector2D contactPoint = position.ToVector2D();
                if (body.Shape.CanGetIntersection)
                {
                    Vector2D         temp = body.Matrices.ToBody * contactPoint;
                    IntersectionInfo intersectionInfo;
                    if (body.Shape.TryGetIntersection(temp, out intersectionInfo))
                    {
                        FixedHingeJoint joint = new FixedHingeJoint(body, contactPoint, new Lifespan());
                        joint.Softness = 0.3;
                        engine.AddJoint(joint);
                        contactJoints[e.Contact.Id] = joint;
                    }
                }
            }
            SetZTop(element);
        }
        public void OnNewContact(IntPtr windowHandle, Multitouch.Contracts.IContact contact)
        {
            Window window = GetWindow(windowHandle);

            if (window != null)
            {
                window.Contacts.Add(new WindowContact(contact));

                Body body;
                if (windowToBody.TryGetValue(window, out body))
                {
                    NativeMethods.POINT point = new NativeMethods.POINT((int)contact.X, (int)contact.Y);

                    Vector2D contactPoint = point.ToVector2D();

                    if (body.Shape.CanGetIntersection)
                    {
                        Vector2D         temp = body.Matrices.ToBody * contactPoint;
                        IntersectionInfo intersectionInfo;
                        if (body.Shape.TryGetIntersection(temp, out intersectionInfo))
                        {
                            FixedHingeJoint joint = new FixedHingeJoint(body, contactPoint, new Lifespan());
                            engine.AddJoint(joint);
                            contactJoints[contact.Id] = joint;
                        }
                    }
                }
            }
        }
Exemple #4
0
        public void AddJoint(IAxleJoint joint)
        {
            if (!(joint is AxleJoint))
            {
                throw new ArgumentException("Joint does not belong to this physics engine.");
            }

            var j = ((AxleJoint)joint).innerJoint;

            j.Lifetime.IsExpired = false;
            phsEngine.AddJoint(j);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            GroupCollection collection1 = new GroupCollection();
            GroupCollection collection2 = new GroupCollection();

            /* collection1.AddRange(new int[] { 1 });
            *  collection1.AddRange(new int[] {  3 });
            *  collection1.AddRange(new int[] {  2 });*/
            collection1.Add(1);
            collection1.Add(3);
            collection1.Add(2);
            collection2.Add(1);
            Console.WriteLine(GroupCollection.Intersect(collection1, collection2));

            Console.ReadLine();
            return;

            PhysicsEngine engine = new PhysicsEngine();

            engine.BroadPhase = new Physics2DDotNet.Detectors.SelectiveSweepDetector();
            engine.Solver     = new Physics2DDotNet.Solvers.SequentialImpulsesSolver();

            PhysicsTimer timer = new PhysicsTimer(engine.Update, .01f);

            timer.IsRunning = true;



            Coefficients coffecients = new Coefficients(/*restitution*/ 1, /*friction*/ .5f);


            IShape shape1 = new CircleShape(8, 7);
            IShape shape2 = new PolygonShape(VertexHelper.CreateRectangle(20, 10), 3);

            Scalar mass  = 5;
            Body   body1 = new Body(new PhysicsState(), shape1, mass, coffecients, new Lifespan());
            Body   body2 = new Body(new PhysicsState(), shape2, mass, coffecients, new Lifespan());

            engine.AddBody(body1);
            engine.AddBody(body2);
            Joint joint = new HingeJoint(body1, body2, Vector2D.Zero, new Lifespan());

            engine.AddJoint(joint);
            joint.Lifetime.IsExpired = true;

            engine.AddJoint(new HingeJoint(body1, body2, Vector2D.Zero, new Lifespan()));
            engine.Update(0, 0);

            body1.Lifetime.IsExpired = true;

            timer.IsRunning = false;
            engine.AddProxy(body1, body2, Matrix2x2.Identity);
            //  b1.RemoveFromProxy();



            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, engine);
            stream.Seek(0, SeekOrigin.Begin);
            PhysicsEngine engine2 = (PhysicsEngine)formatter.Deserialize(stream);



            Console.WriteLine();

            /*
             *
             * Vector2D[] vertexes1 = new Vector2D[]
             * {
             *  new Vector2D(-1,1),
             *  new Vector2D(-3,1),
             *  new Vector2D(-3,-1),
             *  new Vector2D(-1,-1),
             * };
             * Vector2D[] vertexes2 = new Vector2D[]
             * {
             *  new Vector2D(1,-1),
             *  new Vector2D(3,-1),
             *  new Vector2D(3,1),
             *  new Vector2D(1,1),
             * };
             * Vector2D[][] polygons = new Vector2D[2][];
             * polygons[0] = vertexes1;
             * polygons[1] = vertexes2;
             * Console.WriteLine(MultiPartPolygon.GetCentroid(polygons));
             * Console.WriteLine(MultiPartPolygon.GetArea(polygons));
             */
            Console.WriteLine("Finished");
            Console.ReadLine();
        }
Exemple #6
0
 public void AddJoint(Joint joint)
 {
     _engine.AddJoint(joint);
 }