Ejemplo n.º 1
0
        public override void Step(b2TimeStep step)
        {
            float timestep = step.dt;

            if (timestep <= float.MinValue)
            {
                return;
            }
            if (timestep > maxTimestep && maxTimestep > 0.0f)
            {
                timestep = maxTimestep;
            }
            for (b2ControllerEdge i = m_bodyList; i != null; i = i.nextBody)
            {
                b2Body body = i.body;
                if (!body.IsAwake())
                {
                    //Sleeping bodies are still - so have no damping
                    continue;
                }
                b2Vec2 damping =
                    body.GetWorldVector(
                        b2Math.MulMV(T,
                                     body.GetLocalVector(
                                         body.GetLinearVelocity()
                                         )
                                     )
                        );
                body.SetLinearVelocity(new b2Vec2(
                                           body.GetLinearVelocity().x + damping.x * timestep,
                                           body.GetLinearVelocity().y + damping.y * timestep
                                           ));
            }
        }
Ejemplo n.º 2
0
        public void AddBody(b2Body body)
        {
            b2ControllerEdge edge = new b2ControllerEdge();

            edge.controller = this;
            edge.body       = body;
            //
            edge.nextBody = m_bodyList;
            edge.prevBody = null;
            m_bodyList    = edge;
            if (edge.nextBody != null)
            {
                edge.nextBody.prevBody = edge;
            }
            m_bodyCount++;
            //
            edge.nextController   = body.m_controllerList;
            edge.prevController   = null;
            body.m_controllerList = edge;
            if (edge.nextController != null)
            {
                edge.nextController.prevController = edge;
            }
            body.m_controllerCount++;
        }
Ejemplo n.º 3
0
 public override void Step(b2TimeStep step)
 {
     for (b2ControllerEdge i = m_bodyList; i != null; i = i.nextBody)
     {
         b2Body body = i.body;
         if (!body.IsAwake())
         {
             continue;
         }
         body.ApplyForce(F, body.GetWorldCenter());
     }
 }
Ejemplo n.º 4
0
        public override void Step(b2TimeStep step)
        {
            b2Vec2 smallA = new b2Vec2(A.x * step.dt, A.y * step.dt);

            for (b2ControllerEdge i = m_bodyList; i != null; i = i.nextBody)
            {
                b2Body body = i.body;
                if (!body.IsAwake())
                {
                    continue;
                }
                //Am being lazy here
                body.SetLinearVelocity(new b2Vec2(
                                           body.GetLinearVelocity().x + smallA.x,
                                           body.GetLinearVelocity().y + smallA.y
                                           ));
            }
        }
Ejemplo n.º 5
0
        public void RemoveBody(b2Body body)
        {
            b2ControllerEdge edge = body.m_controllerList;

            while (edge != null && edge.controller != this)
            {
                edge = edge.nextController;
            }

            //Attempted to remove a body that was not attached?
            //b2Settings.b2Assert(bEdge != null);
            if (edge == null)
            {
                return;              //add By kingBook 2019/3/15 11:48
            }
            if (edge.prevBody != null)
            {
                edge.prevBody.nextBody = edge.nextBody;
            }
            if (edge.nextBody != null)
            {
                edge.nextBody.prevBody = edge.prevBody;
            }
            if (edge.nextController != null)
            {
                edge.nextController.prevController = edge.prevController;
            }
            if (edge.prevController != null)
            {
                edge.prevController.nextController = edge.nextController;
            }
            if (m_bodyList == edge)
            {
                m_bodyList = edge.nextBody;
            }
            if (body.m_controllerList == edge)
            {
                body.m_controllerList = edge.nextController;
            }
            body.m_controllerCount--;
            m_bodyCount--;
            //b2Settings.b2Assert(body.m_controllerCount >= 0);
            //b2Settings.b2Assert(m_bodyCount >= 0);
        }
Ejemplo n.º 6
0
 public override void Step(b2TimeStep step)
 {
     if (m_bodyList == null)
     {
         return;
     }
     if (useWorldGravity)
     {
         gravity = GetWorld().GetGravity().Copy();
     }
     for (b2ControllerEdge i = m_bodyList; i != null; i = i.nextBody)
     {
         b2Body body = i.body;
         if (body.IsAwake() == false)
         {
             //Buoyancy force is just a function of position,
             //so unlike most forces, it is safe to ignore sleeping bodes
             continue;
         }
         b2Vec2 areac = new b2Vec2();
         b2Vec2 massc = new b2Vec2();
         float  area  = 0.0f;
         float  mass  = 0.0f;
         for (b2Fixture fixture = body.GetFixtureList(); fixture != null; fixture = fixture.GetNext())
         {
             b2Vec2 sc    = new b2Vec2();
             float  sarea = fixture.GetShape().ComputeSubmergedArea(normal, offset, body.GetTransform(), sc);
             area    += sarea;
             areac.x += sarea * sc.x;
             areac.y += sarea * sc.y;
             float shapeDensity;
             if (useDensity)
             {
                 //TODO: Figure out what to do now density is gone
                 shapeDensity = 1.0f;
             }
             else
             {
                 shapeDensity = 1.0f;
             }
             mass    += sarea * shapeDensity;
             massc.x += sarea * sc.x * shapeDensity;
             massc.y += sarea * sc.y * shapeDensity;
         }
         areac.x /= area;
         areac.y /= area;
         massc.x /= mass;
         massc.y /= mass;
         if (area < float.MinValue)
         {
             continue;
         }
         //Buoyancy
         b2Vec2 buoyancyForce = gravity.GetNegative();
         buoyancyForce.Multiply(density * area);
         body.ApplyForce(buoyancyForce, massc);
         //Linear drag
         b2Vec2 dragForce = body.GetLinearVelocityFromWorldPoint(areac);
         dragForce.Subtract(velocity);
         dragForce.Multiply(-linearDrag * area);
         body.ApplyForce(dragForce, areac);
         //Angular drag
         //TODO: Something that makes more physical sense?
         body.ApplyTorque(-body.GetInertia() / body.GetMass() * area * body.GetAngularVelocity() * angularDrag);
     }
 }
Ejemplo n.º 7
0
        public override void Step(b2TimeStep step)
        {
            //Inlined
            b2ControllerEdge i     = null;
            b2Body           body1 = null;
            b2Vec2           p1    = null;
            float            mass1 = 0.0f;
            b2ControllerEdge j     = null;
            b2Body           body2 = null;
            b2Vec2           p2    = null;
            float            dx    = 0.0f;
            float            dy    = 0.0f;
            float            r2    = 0.0f;
            b2Vec2           f     = null;

            if (invSqr)
            {
                for (i = m_bodyList; i != null; i = i.nextBody)
                {
                    body1 = i.body;
                    p1    = body1.GetWorldCenter();
                    mass1 = body1.GetMass();
                    for (j = m_bodyList; j != i; j = j.nextBody)
                    {
                        body2 = j.body;
                        p2    = body2.GetWorldCenter();
                        dx    = p2.x - p1.x;
                        dy    = p2.y - p1.y;
                        r2    = dx * dx + dy * dy;
                        if (r2 < float.MinValue)
                        {
                            continue;
                        }
                        f = new b2Vec2(dx, dy);
                        f.Multiply(G / r2 / Mathf.Sqrt(r2) * mass1 * body2.GetMass());
                        if (body1.IsAwake())
                        {
                            body1.ApplyForce(f, p1);
                        }
                        f.Multiply(-1.0f);
                        if (body2.IsAwake())
                        {
                            body2.ApplyForce(f, p2);
                        }
                    }
                }
            }
            else
            {
                for (i = m_bodyList; i != null; i = i.nextBody)
                {
                    body1 = i.body;
                    p1    = body1.GetWorldCenter();
                    mass1 = body1.GetMass();
                    for (j = m_bodyList; j != i; j = j.nextBody)
                    {
                        body2 = j.body;
                        p2    = body2.GetWorldCenter();
                        dx    = p2.x - p1.x;
                        dy    = p2.y - p1.y;
                        r2    = dx * dx + dy * dy;
                        if (r2 < float.MinValue)
                        {
                            continue;
                        }
                        f = new b2Vec2(dx, dy);
                        f.Multiply(G / r2 * mass1 * body2.GetMass());
                        if (body1.IsAwake())
                        {
                            body1.ApplyForce(f, p1);
                        }
                        f.Multiply(-1);
                        if (body2.IsAwake())
                        {
                            body2.ApplyForce(f, p2);
                        }
                    }
                }
            }
        }