Пример #1
0
        private Body(Body copy)
        {
            Initialize();
            this.ignoresCollisionResponce = copy.ignoresCollisionResponce;
            this.shape            = copy.shape;
            this.massInfo         = copy.massInfo;
            this.coefficients     = copy.coefficients;
            this.collisionIgnorer = copy.collisionIgnorer;
            this.matrices         = copy.matrices.Duplicate();
            this.state            = copy.state.Duplicate();
            this.lifetime         = copy.lifetime.Duplicate();

            this.transformation = copy.transformation;
            this.linearDamping  = copy.linearDamping;
            this.angularDamping = copy.angularDamping;

            this.ignoresCollisionResponce = copy.ignoresCollisionResponce;
            this.ignoresGravity           = copy.ignoresGravity;
            this.ignoresPhysicsLogics     = copy.ignoresPhysicsLogics;
            this.isTransformed            = copy.isTransformed;
            this.isCollidable             = copy.isCollidable;
            this.isEventable = copy.isEventable;

            this.tag = copy.tag;
        }
Пример #2
0
        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);
        }
Пример #3
0
        public Body(float x, float y, float width, float height, float bounce, float friction, float mass)
        {
            Coefficients coffecients = new Coefficients(bounce, friction);
            Shape        shape       = new Polygon(Polygon.CreateRectangle(height, width), 3.0f);

            state = new PhysicsState(new ALVector2D(0.0f, x, y));
            data  = new Physics2DDotNet.Body(state, shape, mass, coffecients, new Lifespan());
        }
Пример #4
0
        public Body(float x, float y, float radius, float bounce, float friction, float mass)
        {
            Coefficients coffecients = new Coefficients(bounce, friction);
            Shape        shape       = new Circle(radius, 16);

            state = new PhysicsState(new ALVector2D(0.0f, x, y));
            data  = new Physics2DDotNet.Body(state, shape, mass, coffecients, new Lifespan());
        }
Пример #5
0
        public override void Init(PrototypingFramework.engine.GameEngine game)
        {
            base.Init(game);
            world = new PhysicsEngine();
            world.BroadPhase = new Physics2DDotNet.Detectors.SelectiveSweepDetector();
            world.Solver = new Physics2DDotNet.Solvers.SequentialImpulsesSolver();

            PhysicsTimer timer = new PhysicsTimer(world.Update, .01f);
            timer.IsRunning = true;

            rs = new RectangleShape(new SFML.Window.Vector2f(10, 10));
            rs.FillColor = Color.Black;
            rs.Position = new SFML.Window.Vector2f(10, 5);

            Coefficients coffecients = new Coefficients(/*restitution*/1, /*friction*/.5f);
            IShape shape2 = new PolygonShape(VertexHelper.CreateRectangle(10, 20), 3);
             body2 = new Body(new PhysicsState(new ALVector2D(0,10,5)), shape2, 5, coffecients, new Lifespan());

            world.AddBody(body2);

            PhysicsLogic logGravity;

            logGravity = (PhysicsLogic)new GravityField(new Vector2D(0f, 200f), new Lifespan());
            //pretty basic, create a downward force

            world.AddLogic(logGravity);

            Body bdyFloor;
            PhysicsState flrState;
            PolygonShape flrShape;
            Coefficients flrCoff;
            Lifespan flrLife;

            flrState = new PhysicsState(new ALVector2D((float)0.0, 0, (float)_game.Window.Size.Y-64));
            //create the state, centering the x-axis on screen and bottom of the y-axis

            flrShape = new PolygonShape(VertexHelper.CreateRectangle(_game.Window.Size.X, 64), 2);
            //create form.widthX64 rectangle (sq) with grid spacing at 2

            flrCoff = new Coefficients(0.5f, 0.4f, 0.4f);
            //might require tuning to your liking...

            flrLife = new Lifespan();
            //forever and ever

            bdyFloor = new Body(flrState, flrShape, float.PositiveInfinity, flrCoff, flrLife);
            //never ending mass means it isn't going to move on impact

            bdyFloor.IgnoresGravity = true;
            //make sure the floor stays

            world.AddBody(bdyFloor);

            floor = new RectangleShape(new SFML.Window.Vector2f( _game.Window.Size.X,64));
            floor.Position = new SFML.Window.Vector2f(0, _game.Window.Size.Y - 64);
            floor.FillColor = Color.Red;
        }
Пример #6
0
        public BaseModelBody(PhysicsState state, IShape shape, MassInfo massInfo, Coefficients coefficients, Lifespan lifetime, Guid modelId)
            : base(state, shape, massInfo, coefficients, lifetime)
        {
            if (modelId == Guid.Empty)
            {
                throw new ArgumentException("'guid' cannot be empty!");
            }

            ModelId = modelId;
        }
Пример #7
0
 /// <summary>
 /// Creates a new Body Instance.
 /// </summary>
 /// <param name="state">The State of the Body.</param>
 /// <param name="shape">The Shape of the Body.</param>
 /// <param name="mass">The mass of the Body The inertia will be aquired from the Shape.</param>
 /// <param name="coefficients">A object containing coefficients.</param>
 /// <param name="lifeTime">A object Describing how long the object will be in the engine.</param>
 public Body(
     PhysicsState state,
     IShape shape,
     Scalar mass,
     Coefficients coefficients,
     Lifespan lifetime)
     : this(
         state, shape,
         GetMassInfo(mass, shape),
         coefficients, lifetime)
 {
 }
Пример #8
0
 /// <summary>
 /// This applys the proxy.
 /// This will cause all other bodies in the proxy list to have their velocity set
 /// to this body’s.
 /// With the appropriate transformations applied.
 /// </summary>
 public void ApplyProxy()
 {
     if (proxies.Count == 0)
     {
         return;
     }
     for (LinkedListNode <BodyProxy> node = proxies.First;
          node != null;
          node = node.Next)
     {
         BodyProxy    proxy = node.Value;
         PhysicsState state = proxy.Body2.state;
         state.Velocity.Angular = this.state.Velocity.Angular;
         Vector2D.Transform(
             ref proxy.transformation,
             ref this.state.Velocity.Linear,
             out state.Velocity.Linear);
     }
 }
Пример #9
0
        /// <summary>
        /// Creates a new Body Instance.
        /// </summary>
        /// <param name="state">The State of the Body.</param>
        /// <param name="shape">The Shape of the Body.</param>
        /// <param name="massInfo">A object describing the mass and inertia of the Body.</param>
        /// <param name="coefficients">A object containing coefficients.</param>
        /// <param name="lifeTime">A object Describing how long the object will be in the engine.</param>
        public Body(
            PhysicsState state,
            IShape shape,
            MassInfo massInfo,
            Coefficients coefficients,
            Lifespan lifetime)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }
            if (shape == null)
            {
                throw new ArgumentNullException("shape");
            }
            if (massInfo == null)
            {
                throw new ArgumentNullException("massInfo");
            }
            if (coefficients == null)
            {
                throw new ArgumentNullException("coefficients");
            }
            if (lifetime == null)
            {
                throw new ArgumentNullException("lifetime");
            }
            Initialize();
            this.matrices       = new Matrices();
            this.transformation = Matrix2x3.Identity;

            this.state          = new PhysicsState(state);
            this.Shape          = shape;
            this.massInfo       = massInfo;
            this.coefficients   = coefficients;
            this.lifetime       = lifetime;
            this.linearDamping  = 1;
            this.angularDamping = 1;
            this.isCollidable   = true;
            this.isEventable    = true;
            this.ApplyPosition();
        }
Пример #10
0
        private Body(Body copy)
        {
            this.proxies = new LinkedList <BodyProxy>();
            this.ignoresCollisionResponce = copy.ignoresCollisionResponce;
            this.shape            = copy.shape;
            this.massInfo         = copy.massInfo;
            this.coefficients     = copy.coefficients;
            this.collisionIgnorer = copy.collisionIgnorer;
            this.matrices         = copy.matrices.Duplicate();
            this.state            = copy.state.Duplicate();
            this.lifetime         = copy.lifetime.Duplicate();

            this.transformation = copy.transformation;
            this.linearDamping  = copy.linearDamping;
            this.angularDamping = copy.angularDamping;

            this.ignoresCollisionResponce = copy.ignoresCollisionResponce;
            this.ignoresGravity           = copy.ignoresGravity;
            this.isCollidable             = copy.isCollidable;
            this.ignoresGravity           = copy.ignoresGravity;
            this.isTransformed            = copy.isTransformed;

            this.tag = (copy.tag is ICloneable) ? (((ICloneable)copy.tag).Clone()) : (copy.tag);
        }
Пример #11
0
 public BaseModelBody(PhysicsState state, IShape shape, double mass, Coefficients coefficients, Lifespan lifetime, Guid modelId)
     : this(state, shape, new MassInfo { Mass = mass }, coefficients, lifetime, modelId)
 {
 }
Пример #12
0
        void CreateBody(FrameworkElement frameworkElement)
        {
            double angle = 0;

            //if an element already has rotatetransform, get it's angle.
            RotateTransform rotateTransform = frameworkElement.RenderTransform as RotateTransform;
            if (rotateTransform != null)
                angle = MathHelper.ToRadians(rotateTransform.Angle);

            PhysicsState state = new PhysicsState(new ALVector2D(angle, GetLeft(frameworkElement) + (frameworkElement.ActualWidth / 2),
                                                                 GetTop(frameworkElement) + (frameworkElement.ActualHeight / 2)));
            IShape shape = new PolygonShape(VertexHelper.CreateRectangle(frameworkElement.ActualWidth, frameworkElement.ActualHeight), 2);
            MassInfo mass = MassInfo.FromPolygon(shape.Vertexes, 1);
            Body body = new Body(state, shape, mass, new Coefficients(0.4, 0.95), new Lifespan());
            body.LinearDamping = LinearDamping;
            body.AngularDamping = AngularDamping;
            if (EnableWalls)
            {
                body.IsCollidable = true;
                body.CollisionIgnorer = ignorer;
            }
            else
                body.IsCollidable = false;
            body.Tag = frameworkElement;
            engine.AddBody(body);
            elementToBody.Add(frameworkElement, body);
            elementToScale.Add(frameworkElement, new ScaleState());

            TransformGroup transform = new TransformGroup();
            transform.Children.Add(new ScaleTransform());
            transform.Children.Add(new RotateTransform());
            frameworkElement.RenderTransform = transform;
            SetZTop(frameworkElement);
		
			SubscribeEventsToChild(frameworkElement);
        }
Пример #13
0
        void LaunchProjectile()
        {
            Scalar radius = 5;
            Scalar velocity = 2000;

            Matrix2x3 toWorld = avatarBodies[0].Matrices.ToWorld;
            Matrix2x2 toWorldNormal = avatarBodies[0].Matrices.ToWorldNormal;

          //  Matrix2D mat = avatarBodies[0].Matrices.ToWorld;
            Vector2D position = toWorld * (avatarBarrelOffset);
            Vector2D direction = toWorldNormal * Vector2D.XAxis;
            PhysicsState state = new PhysicsState();
            state.Position.Linear = position;
            state.Velocity.Linear = velocity * direction + avatarBodies[0].State.Velocity.Linear;

            Body weapon = new Body(state,
                new CircleShape(radius, 8),
                5,
                coefficients.Duplicate(),
                new Lifespan(10));
            weapon.CollisionIgnorer = avatarBodies[0].CollisionIgnorer;
            weapon.Collided += weapon_Collided;
            AddGlObject(weapon);
            engine.AddBody(weapon);
            avatarBodies[0].State.Velocity.Linear -= (velocity * weapon.Mass.Mass * avatarBodies[0].Mass.MassInv) * direction;
        }
Пример #14
0
		/// <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;
		}
Пример #15
0
        public static DisposeCallback CreateTank(DemoOpenInfo info, Vector2D position,List<Body> result)
        {
            Lifespan avatarLifespan = new Lifespan();

            IShape shape = ShapeFactory.CreateSprite(Cache<SurfacePolygons>.GetItem("tank.png"), 4, 18, 2);

            ObjectIgnorer ignorer = new ObjectIgnorer();
            Body tankBody = new Body(new PhysicsState(new ALVector2D(0, 0, 0)),
                shape,
                300,//new MassInfo(40, Scalar.PositiveInfinity),
                new Coefficients(0, 1),
                avatarLifespan);
            result.Add(tankBody);
            tankBody.State.Position.Linear += position;
            tankBody.ApplyPosition();

            tankBody.CollisionIgnorer = ignorer;
            BodyGraphic graphic = CreateGraphic(tankBody);
            graphic.ZOrder = 2;
            info.Scene.AddGraphic(graphic);

            Scalar wheelSize = 18;
            Scalar wheelSpacing = -9;
            Scalar lenghtPercent = .84f;
            Matrix2x3 ident = Matrix2x3.Identity;
            BoundingRectangle rect;
            shape.CalcBoundingRectangle(ref ident, out rect);
            Scalar y = (rect.Max.Y + 4);
            Body lastWheel = null;
            BoundingPolygon polygon = new BoundingPolygon(shape.Vertexes);

            Ray ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis);
            Scalar y3 = y - polygon.Intersects(ray2);
            Vector2D avatarBarrelOffset = new Vector2D(rect.Max.X + 10, y3);

            CircleShape wheelShape = ShapeFactory.CreateColoredCircle(wheelSize, 30);
            Scalar force = 0;

            for (Scalar x = rect.Min.X + wheelSize; x < (rect.Max.X - wheelSize) * lenghtPercent; x += (wheelSize * 2 + wheelSpacing))
            {

                Ray ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis);
                Scalar y2 = y - polygon.Intersects(ray);



                Vector2D offset = new Vector2D(x, y2);

                Body wheel = new Body(
                    new PhysicsState(new ALVector2D(0, offset + position)),
                    wheelShape,
                    10,
                    new Coefficients(0, 3),//  coefficients.Duplicate(),
                    avatarLifespan);
                result.Add(wheel);

                wheel.CollisionIgnorer = ignorer;
                wheel.AngularDamping = .9f;
                wheel.Updated += delegate(object sender, UpdatedEventArgs e)
                {
                    wheel.State.ForceAccumulator.Angular += force;
                };
                info.Scene.AddGraphic(CreateGraphic(wheel));

                HingeJoint joint = new HingeJoint(tankBody, wheel, offset + position, avatarLifespan);
                joint.Softness = .1f;
                info.Scene.Engine.AddJoint(joint);

                if (lastWheel != null)
                {
                    AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan);
                    info.Scene.Engine.AddJoint(joint2);
                }
                lastWheel = wheel;
            }


            CircleShape weaponShape = ShapeFactory.CreateColoredCircle(5, 8);

            //now begins the abuse of anominous delegates (BIG TIME)

            EventHandler<KeyboardEventArgs> keyDownHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.LeftArrow:
                        force = -1500000;
                        break;
                    case Key.RightArrow:
                        force = 1500000;
                        break;
                    case Key.Space:

                        Scalar velocity = 2000;

                        Matrix2x3 toWorld = tankBody.Matrices.ToWorld;
                        Matrix2x2 toWorldNormal = tankBody.Matrices.ToWorldNormal;

                        //  Matrix2D mat = avatarBodies[0].Matrices.ToWorld;
                        Vector2D direction = toWorldNormal * Vector2D.XAxis;
                        PhysicsState state = new PhysicsState();
                        state.Position.Linear = toWorld * (avatarBarrelOffset);
                        state.Velocity.Linear = velocity * direction + tankBody.State.Velocity.Linear;

                        Body weapon = new Body(state,
                            weaponShape,
                            5,
                            new Coefficients(1, 1),
                            new Lifespan(10));
                        //weapon.CollisionIgnorer = tankBody.CollisionIgnorer;

                        weapon.Collided += delegate(object sender2, CollisionEventArgs e2)
                        {
                            if (!weapon.Lifetime.IsExpired)
                            {
                                weapon.Lifetime.IsExpired = true;
                                AddParticles(info, weapon.State.Position.Linear, weapon.State.Velocity.Linear * .5f, 50);
                            }
                        };

                        //  weapon.Collided += weapon_Collided;
                        tankBody.State.Velocity.Linear -= (velocity * weapon.Mass.Mass * tankBody.Mass.MassInv) * direction;
                        info.Scene.AddGraphic(CreateGraphic(weapon));
                        break;
                }
            };
            EventHandler<KeyboardEventArgs> keyUpHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.LeftArrow:
                        force = 0;
                        break;
                    case Key.RightArrow:
                        force = 0;
                        break;
                }
            };
            Events.KeyboardDown += keyDownHandler;
            Events.KeyboardUp += keyUpHandler;

            return delegate()
            {
                Events.KeyboardDown -= keyDownHandler;
                Events.KeyboardUp -= keyUpHandler;
            };
        }
Пример #16
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);
					}
				}
			}
		}
Пример #17
0
 public BoneBody(PhysicsState state, IShape shape, MassInfo massInfo, Coefficients coefficients, Lifespan lifetime, Guid modelId)
     : base(state, shape, massInfo, coefficients, lifetime, modelId)
 {
 }
Пример #18
0
 public void Set(PhysicsState state)
 {
     if (state == null) { throw new ArgumentNullException("state"); }
     this.Position = state.Position;
     this.Velocity = state.Velocity;
     this.Acceleration = state.Acceleration;
     this.ForceAccumulator = state.ForceAccumulator;
 }
Пример #19
0
 public ChainMember(PhysicsState state, IShape shape, MassInfo mass, Coefficients coefficients, Lifespan lifetime, Guid modelId)
     : base(state, shape, mass, coefficients, lifetime, modelId)
 {
 }