コード例 #1
0
ファイル: GravityPointMass.cs プロジェクト: bsvercl/physics2d
 public GravityPointMass(Body source, Scalar metersPerDistanceUnit, Lifespan lifetime)
     : base(GetLifeSpan(source, lifetime))
 {
     if (metersPerDistanceUnit <= 0) { throw new ArgumentOutOfRangeException("metersPerDistanceUnit"); }
     this.source = source;
     this.metersPerDistanceUnit = metersPerDistanceUnit;
 }
コード例 #2
0
ファイル: Body.cs プロジェクト: Anttifer/Jypeli
        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;
        }
コード例 #3
0
ファイル: TouchablePanel.cs プロジェクト: zhuangfangwang/ise
        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);
        }
コード例 #4
0
 public VelocityLimiter(Scalar maxLinearVelocity, Scalar maxAngularVelocity, Lifespan lifetime)
     : base(lifetime)
 {
     if (maxLinearVelocity < 0) { throw new ArgumentOutOfRangeException("maxLinearVelocity"); }
     if (maxAngularVelocity < 0) { throw new ArgumentOutOfRangeException("maxAngularVelocity"); }
     this.maxLinearVelocity = maxLinearVelocity;
     this.maxAngularVelocity = maxAngularVelocity;
 }
コード例 #5
0
ファイル: BaseModelBody.cs プロジェクト: homoluden/fukami
        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;
        }
コード例 #6
0
ファイル: Body.cs プロジェクト: Anttifer/Jypeli
 /// <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)
 {
 }
コード例 #7
0
		/// <summary>
		/// Initializes a new instance of the <see cref="FixedSlidingHingeJoint"/> class.
		/// </summary>
		/// <param name="body">The body.</param>
		/// <param name="anchor">The anchor.</param>
		/// <param name="lifetime">The lifetime.</param>
		/// <param name="orientation">The orientation.</param>
		public FixedSlidingHingeJoint(Body body, Vector2D anchor, Lifespan lifetime, Orientation orientation)
			: base(lifetime)
		{
			if (body == null) { throw new ArgumentNullException("body"); }
			this.body = body;
			this.anchor = anchor;
			Orientation = orientation;
			body.ApplyPosition();
			Vector2D.Transform(ref body.Matrices.ToBody, ref anchor, out localAnchor1);
			softness = 0.001f;
			biasFactor = 0.2f;
			distanceTolerance = Scalar.PositiveInfinity;
		}
コード例 #8
0
ファイル: HingeJoint.cs プロジェクト: homoluden/Phisics2D.Net
        public HingeJoint(Body body1, Body body2, Vector2D anchor, Lifespan lifetime)
            : base(lifetime)
        {
            if (body1 == null) { throw new ArgumentNullException("body1"); }
            if (body2 == null) { throw new ArgumentNullException("body2"); }
            this.body1 = body1;
            this.body2 = body2;
            body1.ApplyMatrix();
            body2.ApplyMatrix();

            Matrix3x3 matrix1 = body1.Shape.MatrixInv.VertexMatrix;
            Matrix3x3 matrix2 = body2.Shape.MatrixInv.VertexMatrix;
            Vector2D.Transform(ref matrix1, ref anchor, out localAnchor1);
            Vector2D.Transform(ref matrix2, ref anchor, out localAnchor2);

            relaxation = 1.0f;
            biasFactor = .1f;
        }
コード例 #9
0
ファイル: Body.cs プロジェクト: Anttifer/Jypeli
        /// <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
ファイル: Body.cs プロジェクト: xiajiaonly/MultiTouchVista
        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
ファイル: Lifespan.cs プロジェクト: homoluden/Phisics2D.Net
 public Lifespan(Scalar age, Scalar maxAge, Lifespan master)
 {
     this.age = age;
     this.maxAge = maxAge;
     this.master = master;
 } 
コード例 #12
0
ファイル: Lifespan.cs プロジェクト: homoluden/Phisics2D.Net
 public Lifespan(Scalar maxAge, Lifespan master)
     : this(0, maxAge, master)
 { }
コード例 #13
0
ファイル: Lifespan.cs プロジェクト: homoluden/Phisics2D.Net
 public Lifespan(Lifespan master)
     : this(0, -1, master)
 { }
コード例 #14
0
ファイル: DemoHelper.cs プロジェクト: bsvercl/physics2d
        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;
            };
        }
コード例 #15
0
 public GravityPointField(Vector2D location, Scalar gravity, Lifespan lifetime)
     : base(lifetime)
 {
     this.location = location;
     this.gravity = gravity;
 }
コード例 #16
0
ファイル: Demo.cs プロジェクト: bsvercl/physics2d
        void CreateAvatar()
        {
            Lifespan avatarLifespan = new Lifespan();

            Sprite sprite = GetSprite("tank.png");
            Vector2D[][] polygons = sprite.Polygons;
            MultiPolygonShape shape = new MultiPolygonShape(polygons, 4);
            shape.Tag = sprite;

            ObjectIgnorer ignorer = new ObjectIgnorer();
            Body a = new Body(new PhysicsState(new ALVector2D(0, 0, 0)),
                shape,
                300,//new MassInfo(40, Scalar.PositiveInfinity),
                coefficients.Duplicate(),
                avatarLifespan);
            a.Updated += new EventHandler<UpdatedEventArgs>(avatar_Updated);
            avatarBodies = new List<Body>();
            avatarOffsets = new List<Vector2D>();
            avatarJoints = new List<Joint>();
            avatarBodies.Add(a);
            a.CollisionIgnorer = ignorer;




            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(polygons[0]);

            Ray ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis);
            Scalar y3 = y - polygon.Intersects(ray2);
            avatarBarrelOffset = new Vector2D(rect.Max.X, y3);
            
            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)),
                    new CircleShape(wheelSize, 30),
                    10,
                    new Coefficients(0,3),//  coefficients.Duplicate(),
                    avatarLifespan);
                HingeJoint joint = new HingeJoint(a, wheel, offset, avatarLifespan);
                joint.Softness = .1f;
                wheel.CollisionIgnorer = ignorer;

                if (lastWheel != null)
                {
                    AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan);
                    avatarJoints.Add(joint2);
                }

                avatarJoints.Add(joint);
                avatarOffsets.Add(offset);
                avatarBodies.Add(wheel);
                lastWheel = wheel;
            }
        }
コード例 #17
0
ファイル: BoneBody.cs プロジェクト: homoluden/fukami
 public BoneBody(PhysicsState state, IShape shape, MassInfo massInfo, Coefficients coefficients, Lifespan lifetime, Guid modelId)
     : base(state, shape, massInfo, coefficients, lifetime, modelId)
 {
 }
コード例 #18
0
ファイル: Joint.cs プロジェクト: homoluden/Phisics2D.Net
 protected Joint(Lifespan lifetime)
 {
     Lifetime = lifetime;
 }
コード例 #19
0
ファイル: ChainMember.cs プロジェクト: homoluden/fukami
 public ChainMember(PhysicsState state, IShape shape, MassInfo mass, Coefficients coefficients, Lifespan lifetime, Guid modelId)
     : base(state, shape, mass, coefficients, lifetime, modelId)
 {
 }
コード例 #20
0
ファイル: BaseModelBody.cs プロジェクト: homoluden/fukami
 public BaseModelBody(PhysicsState state, IShape shape, double mass, Coefficients coefficients, Lifespan lifetime, Guid modelId)
     : this(state, shape, new MassInfo { Mass = mass }, coefficients, lifetime, modelId)
 {
 }
コード例 #21
0
 public static FixedHingeJoint FixedHingeJointFactory(Body body, Vector2D anchor, Lifespan lifetime)
 {
     FixedHingeJoint joint = new FixedHingeJoint(body, anchor, lifetime);
     joint.Softness = TangramInformation.Softness;
     joint.BiasFactor = TangramInformation.BiasFactor - 0.1f;
     return joint;
 }
コード例 #22
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;
        }
コード例 #23
0
ファイル: GravityPointMass.cs プロジェクト: bsvercl/physics2d
 static Lifespan GetLifeSpan(Body source, Lifespan lifetime)
 {
     if (source == null) { throw new ArgumentNullException("source"); }
     if (lifetime == null) { throw new ArgumentNullException("lifeTime"); }
     return new Lifespan(lifetime.Age, lifetime.MaxAge, source.Lifetime);
 }
コード例 #24
0
 public GravityField(Vector2D gravity, Lifespan lifetime)
     : base(lifetime)
 {
     this.gravity = gravity;
 }
コード例 #25
0
ファイル: PhysicsLogic.cs プロジェクト: bsvercl/physics2d
 protected PhysicsLogic(Lifespan lifetime)
 {
     this.Lifetime = lifetime;
 }