Esempio n. 1
0
        public CCPhysicsWorld(CCScene scene)
        {
            _gravity         = new CCPoint(0.0f, -98.0f);
            _speed           = 1.0f;
            _updateRate      = 1;
            _updateRateCount = 0;
            _updateTime      = 0.0f;
            _info            = null;
            _scene           = null;
            _delayDirty      = false;

            _info = new CCPhysicsWorldInfo();

            _scene = scene;

            _info.SetGravity(PhysicsHelper.CCPointToCpVect(_gravity));

            var spc = _info.Space;

            spc.defaultHandler = new cpCollisionHandler()
            {
                beginFunc     = (a, s, o) => CCPhysicsWorldCallback.CollisionBeginCallbackFunc(a as cpArbiter, s, this),
                preSolveFunc  = (a, s, o) => CCPhysicsWorldCallback.CollisionPreSolveCallbackFunc(a as cpArbiter, s, this),
                postSolveFunc = (a, s, o) => CCPhysicsWorldCallback.CollisionPostSolveCallbackFunc(a as cpArbiter, s, this),
                separateFunc  = (a, s, o) => CCPhysicsWorldCallback.CollisionSeparateCallbackFunc(a as cpArbiter, s, this)
            };
        }
Esempio n. 2
0
        public void SetSurfaceVelocity(CCPoint surfaceVelocity)
        {
            var vel = PhysicsHelper.CCPointToCpVect(surfaceVelocity);

            foreach (cpShape shape in _info.getShapes())
            {
                shape.SetSurfaceVelocity(vel);
            }
        }
Esempio n. 3
0
        public CCSize GetSize()
        {
            cpPolyShape shape = (_info.GetShapes().FirstOrDefault() as cpPolyShape);            //->getShapes().front();

            return(PhysicsHelper.cpv2size(
                       new cpVect(
                           cpVect.cpvdist(shape.GetVert(1), shape.GetVert(2)),
                           cpVect.cpvdist(shape.GetVert(0), shape.GetVert(1)))));
        }
Esempio n. 4
0
        public static CCPhysicsJointDistance Construct(CCPhysicsBody a, CCPhysicsBody b, CCPoint anchr1, CCPoint anchr2)
        {
            CCPhysicsJointDistance joint = new CCPhysicsJointDistance();

            if (joint != null && joint.Init(a, b, PhysicsHelper.CCPointToCpVect(anchr1), PhysicsHelper.CCPointToCpVect(anchr2)))
            {
                return(joint);
            }
            return(null);
        }
Esempio n. 5
0
        public List <CCPoint> GetPoints()
        {
            List <CCPoint> outPoints = new List <CCPoint>();

            // int i = 0;
            foreach (var shape in _info.getShapes())
            {
                outPoints.Add(PhysicsHelper.cpVectToCCPoint(((cpSegmentShape)shape).a));
            }
            return(outPoints);
        }
Esempio n. 6
0
        public CCPoint[] GetPoints()
        {
            var shapes = _info.getShapes();

            cpVect[] outPoints = new cpVect[shapes.Count];
            for (int i = 0; i < shapes.Count; i++)
            {
                outPoints[i] = new cpVect(((cpSegmentShape)shapes[i]).a);
            }

            return(PhysicsHelper.cpVectsTpCCPoints(outPoints));
        }
Esempio n. 7
0
        /** Get phsyics shapes that contains the point. */
        public List <CCPhysicsShape> GetShapes(CCPoint point)
        {
            List <CCPhysicsShape> arr = new List <CCPhysicsShape>();

            this._info.getSpace().PointQuery(
                PhysicsHelper.CCPointToCpVect(point), 0, new cpShapeFilter(cp.NO_GROUP, cp.ALL_LAYERS, cp.ALL_LAYERS),
                (s, v1, f, v2, o) => CCPhysicsWorldCallback.GetShapesAtPointFunc(s, f, PhysicsHelper.cpVectToCCPoint(v1), ref arr),
                null
                );

            return(arr);
        }
Esempio n. 8
0
        public List <CCPoint> GetPoints()
        {
            List <CCPoint> outPoints = new List <CCPoint>();

            foreach (var shape in _info.getShapes())
            {
                outPoints.Add(PhysicsHelper.cpVectToCCPoint(((cpSegmentShape)shape).a));
            }

            outPoints.Add(PhysicsHelper.cpVectToCCPoint(((cpSegmentShape)_info.getShapes().LastOrDefault()).a));

            return(outPoints);
        }
Esempio n. 9
0
 /** Test point is in shape or not */
 public bool ContainsPoint(CCPoint point)
 {
     foreach (var shape in _info.GetShapes())
     {
         cpPointQueryInfo info = null;
         shape.PointQuery(PhysicsHelper.CCPointToCpVect(point), ref info);
         if (info != null)
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 10
0
 /** set the gravity value */
 public void SetGravity(CCPoint gravity)
 {
     if (_bodies.Count > 0)
     {
         foreach (var body in _bodies)
         {
             // reset gravity for body
             if (!body.IsGravityEnabled())
             {
                 body.ApplyForce(PhysicsHelper.CCPointToCpVect((_gravity - gravity)) * body.GetMass());
             }
         }
     }
 }
Esempio n. 11
0
        /** move the points to the center */
        public static void RecenterPoints(CCPoint[] points, int count, CCPoint center)
        {
            var cpPoints = PhysicsHelper.CCPointsTocpVects(points);

            cp.RecenterPoly(count, cpPoints);
            points = PhysicsHelper.cpVectsTpCCPoints(cpPoints);
            if (center != CCPoint.Zero)
            {
                for (int i = 0; i < points.Length; ++i)
                {
                    points[i] += center;
                }
            }
        }
Esempio n. 12
0
        public CCPhysicsShapeCircle(CCPhysicsMaterial material, float radius, CCPoint offset)
        {
            _type = PhysicsType.CIRCLE;

            cpShape shape = new cpCircleShape(CCPhysicsShapeInfo.SharedBody, radius, PhysicsHelper.CCPointToCpVect(offset));

            _info.Add(shape);

            _area   = CalculateArea();
            _mass   = material.density == cp.Infinity ? cp.Infinity : material.density * _area;
            _moment = CalculateDefaultMoment();

            Material = material;
        }
Esempio n. 13
0
        public void Init(CCPoint[] vecs, int count, CCPhysicsMaterial material, float radius)
        {
            _type = PhysicsType.POLYGEN;

            cpShape shape = new cpPolyShape(CCPhysicsShapeInfo.SharedBody, count, PhysicsHelper.CCPointsTocpVects(vecs), radius);


            _info.Add(shape);

            _area   = CalculateArea();
            _mass   = material.density == cp.Infinity ? cp.Infinity : material.density * _area;
            _moment = CalculateDefaultMoment();

            Material = material;
        }
Esempio n. 14
0
        public CCPhysicsShapeEdgeSegment(CCPoint a, CCPoint b, CCPhysicsMaterial material, float border = 1)
        {
            cpShape shape = new cpSegmentShape(CCPhysicsShapeInfo.SharedBody,
                                               PhysicsHelper.CCPointToCpVect(a),
                                               PhysicsHelper.CCPointToCpVect(b),
                                               border);

            _type = PhysicsType.EDGESEGMENT;

            _info.Add(shape);

            _mass   = cp.Infinity;
            _moment = cp.Infinity;

            Material = material;
        }
Esempio n. 15
0
        public override CCPoint GetCenter()
        {
            var shapes = _info.getShapes();
            int count  = (int)shapes.Count;

            cpVect[] points = new cpVect[count];
            int      i      = 0;

            foreach (var shape in shapes)
            {
                points[i++] = ((cpSegmentShape)shape).a;
            }

            cpVect center = cp.CentroidForPoly(count, points);

            return(PhysicsHelper.cpVectToCCPoint(center));
        }
Esempio n. 16
0
        public override void Update(float delta)
        {
            if (_dirty)
            {
                float factor = cp.cpfabs(_newScaleX / _scaleX);

                cpCircleShape shape = (cpCircleShape)_info.GetShapes().FirstOrDefault(); //->getShapes().front();
                cpVect        v     = PhysicsHelper.CCPointToCpVect(Offset);             // cpCircleShapeGetOffset();
                v       = cpVect.cpvmult(v, factor);
                shape.c = v;

                shape.SetRadius(shape.GetRadius() * factor);
            }


            base.Update(delta);
        }
Esempio n. 17
0
        /** Searches for physics shapes that intersects the ray. */
        public void RayCast(Func <CCPhysicsWorld, CCPhysicsRayCastInfo, object, bool> func, CCPoint point1, CCPoint point2, object data)
        {
            cp.AssertWarn(func != null, "func shouldn't be nullptr");

            if (func != null)
            {
                CCRayCastCallbackInfo info = new CCRayCastCallbackInfo(this, func, point1, point2, data);
                //Action<cpShape, cpVect, cpVect, float, object> func
                CCPhysicsWorldCallback.continues = true;

                this._info.getSpace().SegmentQuery(
                    PhysicsHelper.CCPointToCpVect(point1),
                    PhysicsHelper.CCPointToCpVect(point2), 1f,
                    new cpShapeFilter(cp.NO_GROUP, cp.ALL_LAYERS, cp.ALL_LAYERS),
                    (shape, v1, v2, f, o) => CCPhysicsWorldCallback.RayCastCallbackFunc(shape, f, PhysicsHelper.cpVectToCCPoint(v1), ref info), data
                    );
            }
        }
Esempio n. 18
0
        public void GenerateContactData()
        {
            if (_contactInfo == null)
            {
                return;
            }

            cpArbiter arb = (cpArbiter)_contactInfo;

            _preContactData = _contactData;
            _contactData    = new CCPhysicsContactData();

            for (int i = 0; i < _contactData.count && i < CCPhysicsContactData.POINT_MAX; ++i)
            {
                _contactData.points[i] = PhysicsHelper.cpVectToCCPoint(arb.GetPointA(i));
            }

            _contactData.normal = _contactData.count > 0 ? PhysicsHelper.cpVectToCCPoint(arb.GetNormal()) : CCPoint.Zero;
        }
Esempio n. 19
0
        /** Searches for physics shapes that contains the point. */
        public void QueryPoint(Func <CCPhysicsWorld, CCPhysicsShape, object, bool> func, CCPoint point, object data)
        {
            cp.AssertWarn(func != null, "func shouldn't be nullptr");

            if (func != null)
            {
                //CCPointQueryCallbackInfo info = new CCPointQueryCallbackInfo(this, func, data);

                CCPointQueryCallbackInfo info = new CCPointQueryCallbackInfo(this, func, data);

                CCPhysicsWorldCallback.continues = true;

                this._info.getSpace().PointQuery(
                    PhysicsHelper.CCPointToCpVect(point), 0f,
                    new cpShapeFilter(cp.NO_GROUP, cp.ALL_LAYERS, cp.ALL_LAYERS),
                    (s, v, f1, f2, o) => CCPhysicsWorldCallback.QueryPointFunc(s, 0f, point, ref info),
                    data
                    );
            }
        }
Esempio n. 20
0
        //static PhysicsJointPin* ruct(PhysicsBody* a, PhysicsBody* b,  cpVect anchr);
        #region PROTECTED FUNC


        protected bool Init(CCPhysicsBody a, CCPhysicsBody b, CCPoint anchr)
        {
            if (!base.Init(a, b))
            {
                return(false);
            }


            cpConstraint joint = new cpPivotJoint(GetBodyInfo(a).Body, GetBodyInfo(b).Body,
                                                  PhysicsHelper.CCPointToCpVect(anchr));

            if (joint == null)
            {
                return(false);
            }

            _info.Add(joint);

            return(true);
        }
Esempio n. 21
0
        public CCPhysicsShapeEdgeChain(CCPoint[] vec, int count, CCPhysicsMaterial material, float border = 1)
        {
            _type = PhysicsType.EDGECHAIN;
            var vecs = PhysicsHelper.CCPointsTocpVects(vec);
            int i    = 0;

            for (; i < count; ++i)
            {
                cpShape shape = new cpSegmentShape(CCPhysicsShapeInfo.SharedBody, vecs[i], vecs[i + 1],
                                                   border);
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);

                _info.Add(shape);
            }

            _mass   = cp.Infinity;
            _moment = cp.Infinity;

            Material = material;
        }
Esempio n. 22
0
        /** Searches for physics shapes that contains in the rect. */
        public void QueryRect(Func <CCPhysicsWorld, CCPhysicsShape, object, bool> func, CCRect rect, object data)
        {
            cp.AssertWarn(func != null, "func shouldn't be nullptr");
            if (func != null)
            {
                CCRectQueryCallbackInfo info = new CCRectQueryCallbackInfo()
                {
                    world = this,
                    func  = func,
                    data  = data
                };

                CCPhysicsWorldCallback.continues = true;

                this._info.getSpace().BBQuery(
                    PhysicsHelper.rect2cpbb(rect),
                    new cpShapeFilter(cp.NO_GROUP, cp.ALL_LAYERS, cp.ALL_LAYERS),
                    (s, o) => CCPhysicsWorldCallback.QueryRectCallbackFunc(s, info),
                    data
                    );
            }
        }
Esempio n. 23
0
        /** return physics shape that contains the point. */
        public CCPhysicsShape GetShape(CCPoint point)
        {
            cpShape shape = null;

            this._info.getSpace().PointQuery(
                PhysicsHelper.CCPointToCpVect(point), 0, new cpShapeFilter(cp.NO_GROUP, cp.ALL_LAYERS, cp.ALL_LAYERS),
                (s, v1, f, v2, o) => { shape = s; }, null);

            if (shape == null)
            {
                return(null);
            }

            CCPhysicsShapeInfo dev;

            if (CCPhysicsShapeInfo.Map.TryGetValue(shape, out dev))
            {
                return(dev.getShape());
            }

            return(null);
        }
Esempio n. 24
0
        protected bool Init(CCPhysicsBody a, CCPhysicsBody b, CCPoint anchr1, CCPoint anchr2, float min, float max)
        {
            if (!base.Init(a, b))
            {
                return(false);
            }

            cpConstraint joint = new cpSlideJoint(GetBodyInfo(a).Body, GetBodyInfo(b).Body,
                                                  PhysicsHelper.CCPointToCpVect(anchr1),
                                                  PhysicsHelper.CCPointToCpVect(anchr2),
                                                  min,
                                                  max);

            if (joint == null)
            {
                return(false);
            }

            _info.Add(joint);

            return(true);
        }
Esempio n. 25
0
        public void GetPoints(out CCPoint[] outPoints)         //cpVect outPoints
        {
            cpShape shape = _info.GetShapes().FirstOrDefault();

            outPoints = PhysicsHelper.cpVectsTpCCPoints(((cpPolyShape)shape).GetVertices());
        }
Esempio n. 26
0
 public CCPoint GetPoint(int i)
 {
     return(PhysicsHelper.cpVectToCCPoint(((cpPolyShape)_info.GetShapes().FirstOrDefault()).GetVert(i)));
 }
Esempio n. 27
0
        public static float CalculateMoment(float mass, CCPoint[] vecs, int count, CCPoint offset)
        {
            float moment = mass == cp.Infinity ? cp.Infinity
                : cp.MomentForPoly(mass, count, PhysicsHelper.CCPointsTocpVects(vecs), PhysicsHelper.CCPointToCpVect(offset), 0.0f);

            return(moment);
        }
Esempio n. 28
0
 public static float CalculateMoment(float mass, float radius, CCPoint offset)
 {
     return(mass == cp.Infinity ? cp.Infinity
             : (cp.MomentForCircle(mass, 0, radius, PhysicsHelper.CCPointToCpVect(offset))));
 }
Esempio n. 29
0
 /** get center of the polyon points */
 public static CCPoint GetPolygonCenter(CCPoint[] points, int count)
 {
     return(PhysicsHelper.cpVectToCCPoint(cp.CentroidForPoly(count, PhysicsHelper.CCPointsTocpVects(points))));
 }
Esempio n. 30
0
 public CCPoint GetPointB()
 {
     return(PhysicsHelper.cpVectToCCPoint(((cpSegmentShape)(_info.getShapes().FirstOrDefault())).tb));
 }