Ejemplo n.º 1
0
        //public static cpShapeFilter FILTER_ALL = new cpShapeFilter(cp.NO_GROUP, (int)cpShapeFilterMask.All, (int)cpShapeFilterMask.All);
        //public static cpShapeFilter FILTER_NONE = new cpShapeFilter(cp.NO_GROUP, (int)cpShapeFilterMask.None, (int)cpShapeFilterMask.None);


        public cpShape(cpBody body, cpShapeMassInfo massInfo)
        {
            /// The rigid body this collision shape is attached to.
            this.body = body;

            this.massInfo = massInfo;

            /// The current bounding box of the shape.
            /// The current bounding box of the shape.
            ///
            //this.bb_l = this.bb_b = this.bb_r = this.bb_t = 0;
            this.bb = new cpBB(0, 0, 0, 0);

            //this.hashid = (cp.shapeIDCounter++).ToString();

            /// Sensor flag.
            /// Sensor shapes call collision callbacks but don't produce collisions.
            this.sensor = false;

            filter = new cpShapeFilter(cp.NO_GROUP, (int)cpShapeFilterMask.Default, (int)cpShapeFilterMask.Default);


            /// Coefficient of restitution. (elasticity)
            this.e = 0;
            /// Coefficient of friction.
            this.u = 0;
            /// Surface velocity used when solving for friction.
            this.surfaceV = cpVect.Zero;

            /// Collision type of this shape used when picking collision handlers.
            this.type = 0;

            this.space = null;
        }
Ejemplo n.º 2
0
        public static cpShapeMassInfo cpSegmentShapeMassInfo(float mass, cpVect a, cpVect b, float r)
        {
            var info = new cpShapeMassInfo(
                mass, cp.MomentForBox(1.0f, cpVect.cpvdist(a, b) + 2.0f * r, 2.0f * r), // TODO is an approximation.
                cpVect.cpvlerp(a, b, 0.5f),
                cp.AreaForSegment(a, b, r)
                );

            return(info);
        }
Ejemplo n.º 3
0
        public static cpShapeMassInfo cpCircleShapeMassInfo(float mass, float radius, cpVect center)
        {
            var info = new cpShapeMassInfo(
                mass, cp.MomentForCircle(1.0f, 0.0f, radius, cpVect.Zero),
                center,
                cp.AreaForCircle(0.0f, radius)
                );

            return(info);
        }
Ejemplo n.º 4
0
        public static cpShapeMassInfo MassInfo(float mass, int count, cpVect[] verts, float radius)
        {
            // TODO moment is approximate due to radius.
            cpVect          centroid = cp.CentroidForPoly(count, verts);
            cpShapeMassInfo info     = new cpShapeMassInfo(
                mass, cp.MomentForPoly(1.0f, count, verts, cpVect.cpvneg(centroid), radius),
                centroid,
                cp.AreaForPoly(count, verts, radius)
                );

            return(info);
        }
Ejemplo n.º 5
0
        public static cpShapeMassInfo cpPolyShapeMassInfo(float mass, int count, cpVect[] verts, float radius)
        {
            cpVect centroid = cp.CentroidForPoly(count, verts);

            var info = new cpShapeMassInfo(
                mass,
                cp.MomentForPoly(1.0f, count, verts, cpVect.cpvneg(centroid), radius),
                centroid,
                cp.AreaForCircle(0.0f, radius)
                );

            return(info);
        }
Ejemplo n.º 6
0
        // Should *only* be called when shapes with mass info are modified, added or removed.
        public void AccumulateMassFromShapes()
        {
            if (bodyType != cpBodyType.DYNAMIC)
            {
                return;
            }

            // Reset the body's mass data.
            this.m = this.i = 0.0f;

            this.cog = cpVect.Zero;

            // Cache the position to realign it at the end.
            cpVect pos = GetPosition();

            // Accumulate mass from shapes.

            eachShape((shape, o) =>
            {
                cpShapeMassInfo info = shape.massInfo;
                float m = info.m;

                if (m > 0.0f)
                {
                    float msum = this.m + m;

                    this.i  += m * info.i + cpVect.cpvdistsq(this.cog, info.cog) * (m * this.m) / msum;
                    this.cog = cpVect.cpvlerp(this.cog, info.cog, m / msum);
                    this.m   = msum;
                }
            }, null);


            // Recalculate the inverses.
            this.m_inv = 1.0f / this.m;
            this.i_inv = 1.0f / this.i;

            // Realign the body since the CoG has probably moved.
            SetPosition(pos);

            AssertSaneBody();
        }