private void CreateWalls()
        {
            var wallCoff = new Coefficients(0.8f, 0.95f);
            var wallLife = new Lifespan();

            var flrState = new PhysicsState(new ALVector2D((float)0.0, ((float)ActualWidth) * ((float)0.5), (float)ActualHeight + 100.0));
            var flrShape = new PolygonShape(VertexHelper.CreateRectangle(ActualWidth, 200), 2);
            var bdyFloor = new Body(flrState, flrShape, float.PositiveInfinity, wallCoff, wallLife);

            var ceiState   = new PhysicsState(new ALVector2D((float)0.0, ((float)ActualWidth) * ((float)0.5), -100.0));
            var ceiShape   = new PolygonShape(VertexHelper.CreateRectangle(ActualWidth, 200), 2);
            var bdyCeiling = new Body(ceiState, ceiShape, float.PositiveInfinity, wallCoff, wallLife);

            var lwlState    = new PhysicsState(new ALVector2D((float)0.0, -100.0, ((float)ActualHeight) * ((float)0.5)));
            var lwlShape    = new PolygonShape(VertexHelper.CreateRectangle(200, ActualHeight), 2);
            var bdyLeftWall = new Body(lwlState, lwlShape, float.PositiveInfinity, wallCoff, wallLife);

            var rwlState     = new PhysicsState(new ALVector2D((float)0.0, (float)ActualWidth + 100.0, ((float)ActualHeight) * ((float)0.5)));
            var rwlShape     = new PolygonShape(VertexHelper.CreateRectangle(200, ActualHeight), 2);
            var bdyRightWall = new Body(rwlState, rwlShape, float.PositiveInfinity, wallCoff, wallLife);

            engine.AddBody(bdyFloor);
            engine.AddBody(bdyCeiling);
            engine.AddBody(bdyLeftWall);
            engine.AddBody(bdyRightWall);
        }
示例#2
0
        public World()
        {
            float w, h;

            w        = 640.0f; h = 480.0f;
            bodyList = new List <Body>();

            engine            = new PhysicsEngine();
            engine.BroadPhase = new Physics2DDotNet.Detectors.BruteForceDetector();
            engine.Solver     = new Physics2DDotNet.Solvers.SequentialImpulsesSolver();
            engine.AddLogic(new GravityField(new Vector2D(0, 500), new Lifespan()));

            Coefficients coffecients = new Coefficients(.8f, .5f);
            Shape        floor       = new Polygon(Polygon.CreateRectangle(100.0f, w), 2.0f);
            Shape        left        = new Polygon(Polygon.CreateRectangle(h, 100.0f), 2.0f);
            Shape        right       = new Polygon(Polygon.CreateRectangle(h, 100.0f), 2.0f);

            Physics2DDotNet.Body floorData = new Physics2DDotNet.Body(new PhysicsState(new ALVector2D(0.0f, w / 2.0f, h + 50.0f)), floor, new MassInfo(Scalar.PositiveInfinity, Scalar.PositiveInfinity), coffecients, new Lifespan());
            Physics2DDotNet.Body leftData  = new Physics2DDotNet.Body(new PhysicsState(new ALVector2D(0.0f, -50.0f, h / 2.0f)), left, new MassInfo(Scalar.PositiveInfinity, Scalar.PositiveInfinity), coffecients, new Lifespan());
            Physics2DDotNet.Body rightData = new Physics2DDotNet.Body(new PhysicsState(new ALVector2D(0.0f, w + 50.0f, h / 2.0f)), right, new MassInfo(Scalar.PositiveInfinity, Scalar.PositiveInfinity), coffecients, new Lifespan());
            floorData.IgnoresGravity = true;
            leftData.IgnoresGravity  = true;
            rightData.IgnoresGravity = true;
            engine.AddBody(floorData);
            engine.AddBody(leftData);
            engine.AddBody(rightData);
        }
        /// <summary>
        /// Called when the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property changes.
        /// </summary>
        /// <param name="oldContent">The old value of the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property.</param>
        /// <param name="newContent">The new value of the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property.</param>
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            timer.IsRunning = false;

            if (Body != null)
            {
                Body.Lifetime.IsExpired = true;
            }

            base.OnContentChanged(oldContent, newContent);

            PhysicsState state = new PhysicsState(new ALVector2D(0, 0, 0));
            IShape       shape = new PolygonShape(VertexHelper.CreateRectangle(5, 5), 1);
            MassInfo     mass  = MassInfo.FromPolygon(shape.Vertexes, 1);

            Body = new Body(state, shape, mass, new Coefficients(0, 1), new Lifespan());
            Body.LinearDamping        = LinearDumping;
            Body.Mass.MomentOfInertia = double.PositiveInfinity;
            Body.Tag = newContent;
            engine.AddBody(Body);

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                timer.IsRunning = true;
            }
        }
示例#4
0
        void CreateBody(Window window)
        {
            lock (lockPhysicsLoop)
            {
                double    angle    = window.Angle;
                Rectangle position = window.Position;
                if (position.Width > 0 && position.Height > 0)
                {
                    PhysicsState state = new PhysicsState(new ALVector2D(angle, position.Left + position.Width / 2, position.Top + position.Height / 2));
                    IShape       shape = new PolygonShape(VertexHelper.CreateRectangle(position.Height, position.Width), 2);
                    MassInfo     mass  = MassInfo.FromPolygon(shape.Vertexes, 1);
                    Body         body  = new Body(state, shape, mass, new Coefficients(0, 1), new Lifespan());
                    body.LinearDamping  = 0.95;
                    body.AngularDamping = 0.95;
                    body.IsCollidable   = false;
                    body.Tag            = window;
                    engine.AddBody(body);
                    windowToBody.Add(window, body);

                    try
                    {
                        dwm.SetWindowMatrix(window.HWnd.ToInt32(), 0f, 0f, 1f, 1f, 0f);
                    }
                    catch (Exception e)
                    {
                        HandleDwmCrash(e);
                    }
                }
            }
        }
示例#5
0
        public void AddBody(IPhysicsBody body)
        {
            if (!(body is PhysicsBody))
            {
                throw new ArgumentException("This body does not belong to this physics engine.");
            }

            Body b = ((PhysicsBody)body).Body;

            if (b.Engine != null)
            {
                return;
            }

            b.Lifetime.IsExpired = false;
            phsEngine.AddBody(b);
        }
示例#6
0
        public void AddBody(Body body)
        {
            bodyList.Add(body);

            engine.AddBody(body.Data);
        }
示例#7
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();
        }
示例#8
0
 /// <summary>
 /// Adds the body into internal dictionary and underlying physical engine
 /// </summary>
 /// <param name="body">The Body which value will be copied</param>
 public void AddBody(Body body)
 {
     _engine.AddBody(body);
 }