Beispiel #1
0
        public static void ApplyCollisionData(CollisionData cdata)
        {
            if (cdata.TargetBody == null) return;

            cdata.TargetBody.Position += cdata.dPos;
            cdata.TargetBody.Velocity += cdata.dVel;
            cdata.TargetBody.AngularVelocity += cdata.dAngVel;
        }
Beispiel #2
0
        public static CollisionData CalcCollision(Contact con)
        {
            var cdata = new CollisionData();
            cdata.TargetBody = con.TargetBody;
            cdata.dPos = Vector2.Zero;
            cdata.dVel = Vector2.Zero;
            cdata.dAngVel = 0;

            var A = con.TargetBody;
            var B = con.ImpactBody;

            //penetration resolution
            var totalInvMass = A.InvMass() + B.InvMass();
            cdata.dPos = (con.Disp * A.InvMass() / (totalInvMass)) * con.Normal; //more massive-> less weight. objects in collision share burden of displacing proportional to 1/M.

            //collision response
            var sepVel = Vector2.Dot(B.Velocity - A.Velocity, con.Normal); //separating velocity: relative velocity of impact body along normal.
            if (sepVel >= 0) return cdata; //return no change if bodies are separating

            float rest = (A.Restitution+B.Restitution)/2;
            float impulse = sepVel * (1 + rest) / (totalInvMass);
            cdata.dVel = (impulse * A.InvMass()) * con.Normal;

            return cdata;
        }
Beispiel #3
0
        public static CollisionData AggrCollisionData(List<CollisionData> cdata)
        {
            var aggr = new CollisionData();
            if (cdata.Count > 0)
            {
                aggr.TargetBody = cdata.First().TargetBody;

                foreach (var cd in cdata)
                    aggr += cd;

                //aggr /= cdata.Count;
                aggr.dPos *= HARDNESS;
            }
            return aggr;
        }