Esempio n. 1
0
		public PSolver(PShape shape1, PShape shape2, PContact[] contacts, int num) {
			s1 = shape1;
			s2 = shape2;
			b1 = s1._parent;
			b2 = s2._parent;
			fric = (float) System.Math.Sqrt(s1._fric * s2._fric);
			rest = (float) System.Math.Sqrt(s1._rest * s2._rest);
			cs = contacts;
			numContacts = num;
			for (int i = 0; i < numContacts; i++) {
				PContact c = cs[i];
				c.rel1 = c.pos.Sub(b1.pos);
				c.rel2 = c.pos.Sub(b2.pos);
				c.massN = PTransformer.CalcEffectiveMass(b1, b2, c.rel1, c.rel2,
						c.normal);
				c.massT = PTransformer.CalcEffectiveMass(b1, b2, c.rel1, c.rel2,
						c.tangent);
				c.relVel = PTransformer
						.CalcRelativeVelocity(b1, b2, c.rel1, c.rel2);
				float rvn = c.relVel.Dot(c.normal);
				if (rvn < -0.5F)
					c.targetVelocity = System.Math.Max(rest * -rvn,0.0F);
				else
					c.targetVelocity = 0.0F;
				c.tangent.Set(c.normal.y, -c.normal.x);
				c.localRel1.Set(c.rel1.x, c.rel1.y);
				c.localRel2.Set(c.rel2.x, c.rel2.y);
				b1.mAng.Transpose().MulEqual(c.localRel1);
				b2.mAng.Transpose().MulEqual(c.localRel2);
			}
	
		}
Esempio n. 2
0
 public void UnbindPhysics(SpriteBatchObject o)
 {
     if (usePhysics)
     {
         PBody body = (PBody)CollectionUtils.Remove(_Bodys, o);
         if (body != null)
         {
             body.SetTag(null);
             _manager.world.RemoveBody(body);
         }
     }
 }
Esempio n. 3
0
 public PBody FindPhysics(SpriteBatchObject o)
 {
     if (usePhysics)
     {
         PBody body = (PBody)CollectionUtils.Get(_Bodys, o);
         return(body);
     }
     else
     {
         throw new RuntimeException("You do not set the physics engine !");
     }
 }
Esempio n. 4
0
 public PBody BindPhysics(PBody body, SpriteBatchObject o)
 {
     if (usePhysics)
     {
         body.SetTag(o);
         _manager.AddBody(body);
         CollectionUtils.Put(_Bodys, o, body);
         return(body);
     }
     else
     {
         throw new RuntimeException("You do not set the physics engine !");
     }
 }
Esempio n. 5
0
 public PBody BindPhysics(bool fix, SpriteBatchObject o, float density)
 {
     if (usePhysics)
     {
         PBody body = _manager.AddBox(fix, o.GetRectBox(),
                                      MathUtils.ToRadians(o.GetRotation()), density);
         body.SetTag(o);
         CollectionUtils.Put(_Bodys, o, body);
         return(body);
     }
     else
     {
         throw new RuntimeException("You do not set the physics engine !");
     }
 }
Esempio n. 6
0
 public PBody BindCirclePhysics(bool fix, SpriteBatchObject o,
                                float density)
 {
     if (usePhysics)
     {
         RectBox rect = o.GetRectBox();
         float   r    = (rect.width + rect.height) / 4;
         PBody   body = _manager.AddCircle(fix, o.X(), o.Y(), r,
                                           MathUtils.ToRadians(o.GetRotation()), density);
         body.SetTag(o);
         CollectionUtils.Put(_Bodys, o, body);
         return(body);
     }
     else
     {
         throw new RuntimeException("You do not set the physics engine !");
     }
 }
Esempio n. 7
0
 public PBody BindTexturePhysics(bool fix, SpriteBatchObject o,
                                 float density)
 {
     if (usePhysics)
     {
         PBody body = _manager.AddShape(fix, o.GetAnimation()
                                        .GetSpriteImage(), MathUtils.ToRadians(o.GetRotation()),
                                        density);
         if (body.Size() > 0)
         {
             body.Inner_shapes()[0].SetPosition(o.X() / _manager.scale,
                                                o.Y() / _manager.scale);
         }
         body.SetTag(o);
         CollectionUtils.Put(_Bodys, o, body);
         return(body);
     }
     else
     {
         throw new RuntimeException("You do not set the physics engine !");
     }
 }
Esempio n. 8
0
 public PSolver(PShape shape1, PShape shape2, PContact[] contacts, int num)
 {
     s1          = shape1;
     s2          = shape2;
     b1          = s1._parent;
     b2          = s2._parent;
     fric        = (float)System.Math.Sqrt(s1._fric * s2._fric);
     rest        = (float)System.Math.Sqrt(s1._rest * s2._rest);
     cs          = contacts;
     numContacts = num;
     for (int i = 0; i < numContacts; i++)
     {
         PContact c = cs[i];
         c.rel1  = c.pos.Sub(b1.pos);
         c.rel2  = c.pos.Sub(b2.pos);
         c.massN = PTransformer.CalcEffectiveMass(b1, b2, c.rel1, c.rel2,
                                                  c.normal);
         c.massT = PTransformer.CalcEffectiveMass(b1, b2, c.rel1, c.rel2,
                                                  c.tangent);
         c.relVel = PTransformer
                    .CalcRelativeVelocity(b1, b2, c.rel1, c.rel2);
         float rvn = c.relVel.Dot(c.normal);
         if (rvn < -0.5F)
         {
             c.targetVelocity = System.Math.Max(rest * -rvn, 0.0F);
         }
         else
         {
             c.targetVelocity = 0.0F;
         }
         c.tangent.Set(c.normal.y, -c.normal.x);
         c.localRel1.Set(c.rel1.x, c.rel1.y);
         c.localRel2.Set(c.rel2.x, c.rel2.y);
         b1.mAng.Transpose().MulEqual(c.localRel1);
         b2.mAng.Transpose().MulEqual(c.localRel2);
     }
 }
Esempio n. 9
0
 public void RemoveFromWorld(Dyn.World world)
 {
     world.RemoveBody(PBody);
     PBody = PBody.Clone(world: DummyWorld);
 }
Esempio n. 10
0
 /// <summary>
 /// AddToWorld() and RemoveFromWorld() are necessary to make a square peg fit in a round hole, essentially.
 /// See, we're going to want to have actors with Body's attached to them that aren't in the current World.
 /// For instance, when they're representing a room that hasn't been entered yet.
 /// Or when they've just been loaded and we're about to create them but still need to know where the heck
 /// they are.
 /// Since Farseer makes it impossible to have a Body that doesn't have a World attached to it, we make a dummy
 /// World and use that to initialize Body's, then replace it with a copy that refers to the real game's World
 /// when the actor goes live.
 /// OPT: This allocates, irritatingly, but won't be happening every frame.
 /// </summary>
 /// <param name="world">World.</param>
 public void AddToWorld(Dyn.World world)
 {
     PBody   = PBody.Clone(world: world);
     Fixture = PBody.CreateFixture(Shape);
     Log.Message("Added body to world, type {0}, actor {1}", PBody.BodyType, Owner);
 }
Esempio n. 11
0
        public override void Alter(LTimerContext timer)
        {
            for (int i = 0; i < keySize; i++)
            {
                ActionKey act = (ActionKey)keyActions.Get(i);
                if (act.IsPressed())
                {
                    act.Act(elapsedTime);
                    if (act.isReturn)
                    {
                        return;
                    }
                }
            }
            if (content.IsVisible())
            {
                ProcessEvents();
                content.UpdateNode(timer.GetMilliseconds());
            }
            if (usePhysics)
            {
                if (_dt < 0)
                {
                    _manager.Step(timer.GetMilliseconds());
                }
                else
                {
                    _manager.Step(_dt);
                }
            }
            if (follow != null)
            {
                if (usePhysics)
                {
                    _manager.Offset(follow.GetX(), follow.GetY());
                }
                foreach (TileMap tile in tiles)
                {
                    float offsetX = GetHalfWidth() - follow.GetX();
                    offsetX = MathUtils.Min(offsetX, 0);
                    offsetX = MathUtils.Max(offsetX, GetWidth() - tile.GetWidth());

                    float offsetY = GetHalfHeight() - follow.GetY();
                    offsetY = MathUtils.Min(offsetY, 0);
                    offsetY = MathUtils
                              .Max(offsetY, GetHeight() - tile.GetHeight());

                    SetOffset(tile, offsetX, offsetY);
                    tile.Update(elapsedTime);
                }
            }
            foreach (SpriteBatchObject o in objects)
            {
                if (usePhysics)
                {
                    PBody body = (PBody)CollectionUtils.Get(_Bodys, o);
                    if (body != null)
                    {
                        PShape shape    = body.Inner_shapes()[0];
                        float  rotation = (shape.GetAngle() * MathUtils.RAD_TO_DEG) % 360;
                        AABB   aabb     = shape.GetAABB();
                        o.SetLocation(_manager.GetScreenX(aabb.minX),
                                      _manager.GetScreenY(aabb.minY));
                        o.SetRotation(rotation);
                    }
                }
                o.Update(elapsedTime);
                if (updateListener != null)
                {
                    updateListener.Act(o, elapsedTime);
                }
            }
            Update(elapsedTime);
            Commits();
        }