Exemple #1
0
        //Create a cricle physics body at the location of the
        public PhysicsObject CreateBoxPhysics(Vector2 size, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            PhysicalBody = Physics.CreateBox(OwningWorld, this.transform.Position, this.transform.Rotation, size, bodyType, mass, moment);

            transform.HasPhysicsAttached  = true;
            transform.AttachedPhysicsBody = PhysicalBody.Body;

            return(PhysicalBody);
        }
Exemple #2
0
        //Create a cricle physics body at the location of the
        public PhysicsObject CreateSpherePhysics(float radius, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            //Create the body and activate it
            PhysicalBody = Physics.CreateCircle(OwningWorld, this.transform.Position, this.transform.Rotation, radius, bodyType, mass, moment);

            transform.HasPhysicsAttached  = true;
            transform.AttachedPhysicsBody = PhysicalBody.Body;

            return(PhysicalBody);
        }
Exemple #3
0
        /// <summary>
        /// Creates a physics body and adds it to the world.
        /// It also adds shape to the body, unless it is null
        /// NOTE: Be sure to call activate on the body when you are ready for it to be simulated!
        /// </summary>
        /// <param name="world">The world to add to the body too</param>
        /// <param name="bodyType">The type of body to create</param>
        /// <param name="mass">The mass of the object</param>
        /// <param name="moment">The moment of inertia</param>
        /// <returns></returns>
        public static cpBody CreateBody(World world, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1f, float moment = 1f)
        {
            //Create the body and set the type
            cpBody body = new cpBody(mass, moment);

            body.SetBodyType(bodyType);

            //Add it to the world
            Worlds[world].AddBody(body);

            //Return it
            return(body);
        }
Exemple #4
0
		public void SetType(cpBodyType type)
		{
			_info.Body.SetBodyType(type);
		}
Exemple #5
0
        /// <summary>
        /// Create a physics body and a box shape and adds it to the world
        /// NOTE: Be sure to call activate on the body when you are ready for it to be simulated!
        /// </summary>
        /// <param name="world">The world to add it to</param>
        /// <param name="position">The position of the center of the box</param>
        /// <param name="rotation">The rotation of the box</param>
        /// <param name="size">The size of the box</param>
        /// <param name="bodyType">The body type of the physics body</param>
        /// <param name="mass">The mass of the physics body</param>
        /// <param name="moment">The moment of inertia for the physics body</param>
        /// <returns></returns>
        public static PhysicsObject CreateBox(World world, Vector2 position, float rotation, Vector2 size, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            //Create body
            cpBody body = CreateBody(world, bodyType, mass, moment);

            body.SetPosition(new cpVect(position.X / PHYSICS_TRANSFORM_SCALE, position.Y / PHYSICS_TRANSFORM_SCALE));
            body.SetAngle(rotation);

            cpPolyShape box = cpPolyShape.BoxShape(body, size.X / PHYSICS_TRANSFORM_SCALE, size.Y / PHYSICS_TRANSFORM_SCALE, 0.1f);

            Worlds[world].AddShape(box);

            //Return the Physics body
            return(new PhysicsObject()
            {
                Body = body, Shape = box
            });
        }
Exemple #6
0
        /// <summary>
        /// Create a physics body and a circle shape and adds it to the world
        /// NOTE: Be sure to call activate on the body when you are ready for it to be simulated!
        /// </summary>
        /// <param name="world">The world to add it to</param>
        /// <param name="position">The starting position for the circle</param>
        /// <param name="rotation">The starting rotation for the circle</param>
        /// <param name="radius">The radius of the circle</param>
        /// <param name="bodyType">The body type of the physics body</param>
        /// <param name="mass">The mass of the physics body</param>
        /// <param name="moment">The moment of inertia for the physics body</param>
        /// <returns></returns>
        public static PhysicsObject CreateCircle(World world, Vector2 position, float rotation, float radius, cpBodyType bodyType = cpBodyType.DYNAMIC, float mass = 1, float moment = 1)
        {
            //Create a body and set its transform
            cpBody body = CreateBody(world, bodyType, mass, moment);

            body.SetPosition(new cpVect(position.X / PHYSICS_TRANSFORM_SCALE, position.Y / PHYSICS_TRANSFORM_SCALE));
            body.SetAngle(rotation);

            //Create the circle shape and add it to the world
            cpCircleShape circleShape = new cpCircleShape(body, radius / PHYSICS_TRANSFORM_SCALE, cpVect.Zero);

            Worlds[world].AddShape(circleShape);

            //Return the phyics object
            return(new PhysicsObject()
            {
                Body = body, Shape = circleShape
            });
        }
Exemple #7
0
        public void SetBodyType(cpBodyType type)
        {
            cpBodyType oldType = bodyType;

            if (oldType == type)
            {
                return;
            }

            // Static bodies have their idle timers set to infinity.
            // Non-static bodies should have their idle timer reset.
            nodeIdleTime = (type == cpBodyType.STATIC ? cp.Infinity : 0.0f);


            if (type == cpBodyType.DYNAMIC)
            {
                this.m     = this.i = 0.0f;
                this.m_inv = this.i_inv = cp.Infinity;

                AccumulateMassFromShapes();
            }
            else
            {
                this.m     = this.i = cp.Infinity;
                this.m_inv = this.i_inv = 0.0f;

                this.v = cpVect.Zero;
                this.w = 0.0f;
            }

            // If the body is added to a space already, we'll need to update some space data structures.

            if (space != null)
            {
                cp.AssertSpaceUnlocked(space);


                if (oldType == cpBodyType.STATIC)
                {
                    // TODO This is probably not necessary
                    //			cpBodyActivateStatic(body, NULL);
                }
                else
                {
                    Activate();
                }

                // Move the bodies to the correct array.
                List <cpBody> fromArray = space.ArrayForBodyType(oldType);
                List <cpBody> toArray   = space.ArrayForBodyType(type);

                if (fromArray != toArray)
                {
                    fromArray.Remove(this);
                    toArray.Add(this);
                }

                // Move the body's shapes to the correct spatial index.
                cpBBTree fromIndex = (oldType == cpBodyType.STATIC ? space.staticShapes : space.dynamicShapes);
                cpBBTree toIndex   = (type == cpBodyType.STATIC ? space.staticShapes : space.dynamicShapes);

                if (fromIndex != toIndex)
                {
                    eachShape((s, o) =>
                    {
                        fromIndex.Remove(s.hashid);
                        toIndex.Insert(s.hashid, s);
                    }, null);
                }
            }
        }
Exemple #8
0
 public void SetType(cpBodyType type)
 {
     _info.Body.SetBodyType(type);
 }
Exemple #9
0
 public void SetType(cpBodyType type)
 {
     _info.GetBody().SetBodyType(type);
 }
Exemple #10
0
        List<CCPhysicsShape> j2cpShape(JObject fixtureValue, cpBodyType bodyType)
        {
            if (null == fixtureValue)
                return null;

            float restitution = jsonToFloat("restitution", fixtureValue);
            float friction = jsonToFloat("friction", fixtureValue);
            float density = jsonToFloat("density", fixtureValue);

            bool isSensor = fixtureValue["sensor"] == null ? false : (bool)fixtureValue["sensor"];

            ushort categoryBits = (fixtureValue["filter-categoryBits"] == null) ? (ushort)0x0001 : (ushort)fixtureValue["filter-categoryBits"];

            ushort maskBits = fixtureValue["filter-maskBits"] == null ? (ushort)0xffff : (ushort)fixtureValue["filter-maskBits"];

            short groupIndex = fixtureValue["filter-groupIndex"] == null ? (short)0 : (short)fixtureValue["filter-groupIndex"];

            List<CCPhysicsShape> shapes = new List<CCPhysicsShape>();

            //CCPhysicsShape shape = null;

            if (null != fixtureValue["circle"])
            {
                JObject circleValue = (JObject)fixtureValue["circle"];

                CCPoint center = jsonToPoint("center", circleValue);

                float radius = jsonToFloat("radius", circleValue);

                CCPhysicsShape shape = new CCPhysicsShape(radius, CCPhysicsMaterial.PHYSICSSHAPE_MATERIAL_DEFAULT, center);
                shapes.Add(shape);

                float area = cp.AreaForCircle(0, radius);
                float mass = density * area;

                shape.Moment = cp.MomentForCircle(mass, 0, radius, new cpVect(center.X, center.Y));

            }
            else if (null != fixtureValue["edge"])
            {

                JObject edgeValue = (JObject)fixtureValue["edge"];
                cpVect vertex1 = jsonToVec("vertex1", edgeValue);
                cpVect vertex2 = jsonToVec("vertex1", edgeValue);
                float radius = 0;

                CCPhysicsShape shape = new CCPhysicsShapeEdgeSegment(new CCPoint(vertex1.x, vertex1.y), new CCPoint(vertex2.x, vertex2.y), radius);
                shapes.Add(shape);
                //SetBodyTypeFromInt(shape, type);

                if (bodyType != cpBodyType.STATIC)
                {

                    float area = cp.AreaForSegment(vertex1, vertex2, radius);
                    float mass = density * area;

                    shape.Moment = cp.MomentForSegment(mass, vertex1, vertex2, 0.0f);
                }

            }
            //else if (null != fixtureValue["loop"])
            //{// support old
            //	// format (r197)

            //	JObject chainValue = (JObject)fixtureValue["loop"];
            //	b2ChainShape chainShape = new b2ChainShape();

            //	int numVertices = ((JArray)chainValue["x"]).Count;

            //	cpVect[] vertices = new cpVect[numVertices];
            //	for (int i = 0; i < numVertices; i++)
            //		vertices[i] = jsonToVec("vertices", chainValue, i);

            //	chainShape.CreateLoop(vertices, numVertices);

            //	fixtureDef.shape = chainShape;
            //	shape = body.CreateFixture(fixtureDef);

            //}
            else if (null != fixtureValue["chain"])
            {

                // FPE. See http://www.box2d.org/forum/viewtopic.php?f=4&t=7973&p=35363

                JObject chainValue = (JObject)fixtureValue["chain"];

                int numVertices = ((JArray)chainValue["vertices"]["x"]).Count;
                var vertices = new cpVect[numVertices];

                for (int i = 0; i < numVertices; i++)
                    vertices[i] = jsonToVec("vertices", chainValue, i);

                float radius = 0.2f;

                CCPhysicsShape shape;

                for (int i = 0; i < numVertices; i++)
                {
                    cpVect vertex1 = vertices[i];
                    cpVect vertex2 = vertices[(i + 1) % numVertices];

                    shape = new CCPhysicsShapeEdgeSegment(new CCPoint(vertex1.x, vertex1.y), new CCPoint(vertex2.x, vertex2.y), radius);//this function will end up only returning the last shape
                    shapes.Add(shape);
                    //SetBodyTypeFromInt(shape, type);

                    if (bodyType != cpBodyType.STATIC)
                    {
                        float area = cp.AreaForSegment(vertex1, vertex2, radius);
                        float mass = density * area;
                        shape.Moment = cp.MomentForSegment(mass, vertex1, vertex2, 0.0f);//hmm. How to set correct moment without clobbering moment from existing shapes?
                    }
                }

            }
            else if (null != fixtureValue["polygon"])
            {

                JObject polygonValue = (JObject)fixtureValue["polygon"];
                cpVect[] vertices = new cpVect[b2_maxPolygonVertices];

                int numVertices = ((JArray)polygonValue["vertices"]["x"]).Count;

                int k = 0;
                for (int i = numVertices - 1; i >= 0; i--) // ohh... clockwise?!
                    vertices[k++] = jsonToVec("vertices", polygonValue, i);

                CCPoint[] points = new CCPoint[numVertices];
                for (int i = 0; i < numVertices; i++) // ohh... clockwise?!
                    points[i] = new CCPoint(vertices[i].x, vertices[i].y);

                CCPhysicsShape shape =new CCPhysicsShapePolygon(points, numVertices,CCPhysicsMaterial.PHYSICSSHAPE_MATERIAL_DEFAULT, 1);
                shapes.Add(shape);

                if (bodyType != cpBodyType.STATIC)
                {
                    float area = Math.Abs(cp.AreaForPoly(numVertices, vertices, 0.0f));
                    float mass = density * area;
                    shape.Moment = cp.MomentForPoly(mass, numVertices, vertices, cpVect.Zero, 0.0f);
                }

                //	}
                //else
                //{

                //	b2PolygonShape polygonShape = new b2PolygonShape();
                //	for (int i = 0; i < numVertices; i++)
                //		vertices[i] = jsonToVec("vertices", polygonValue, i);
                //	polygonShape.Set(vertices, numVertices);
                //	fixtureDef.shape = polygonShape;
                //	shape = body.CreateFixture(fixtureDef);
                //}
            }

            //String fixtureName = fixtureValue["name"] == null ? "" : fixtureValue["name"].ToString();
            //if (fixtureName != "")
            //{

            //    foreach (var shape in shape.GetShapes())
            //    {
            //        SetFixtureName(shape, fixtureName);
            //    }
            //}

            return shapes;
        }
Exemple #11
0
        //MARK: Iteration

        public List <cpBody> ArrayForBodyType(cpBodyType type)
        {
            return(type == cpBodyType.STATIC ?
                   staticBodies : dynamicBodies);
        }
Exemple #12
0
		public void SetBodyType(cpBodyType type)
		{
			cpBodyType oldType = bodyType;

			if (oldType == type) return;

			// Static bodies have their idle timers set to infinity.
			// Non-static bodies should have their idle timer reset.
			nodeIdleTime = (type == cpBodyType.STATIC ? cp.Infinity : 0.0f);


			if (type == cpBodyType.DYNAMIC)
			{
				this.m = this.i = 0.0f;
				this.m_inv = this.i_inv = cp.Infinity;

				AccumulateMassFromShapes();

			}
			else
			{
				this.m = this.i = cp.Infinity;
				this.m_inv = this.i_inv = 0.0f;

				this.v = cpVect.Zero;
				this.w = 0.0f;
			}

			// If the body is added to a space already, we'll need to update some space data structures.

			if (space != null)
			{

				cp.AssertSpaceUnlocked(space);


				if (oldType == cpBodyType.STATIC)
				{
					// TODO This is probably not necessary
					//			cpBodyActivateStatic(body, NULL);
				}
				else
				{
					Activate();
				}

				// Move the bodies to the correct array.
				List<cpBody> fromArray = space.ArrayForBodyType(oldType);
				List<cpBody> toArray = space.ArrayForBodyType(type);

				if (fromArray != toArray)
				{
					fromArray.Remove(this);
					toArray.Add(this);
				}

				// Move the body's shapes to the correct spatial index.
				cpBBTree fromIndex = (oldType == cpBodyType.STATIC ? space.staticShapes : space.dynamicShapes);
				cpBBTree toIndex = (type == cpBodyType.STATIC ? space.staticShapes : space.dynamicShapes);

				if (fromIndex != toIndex)
				{
					eachShape((s, o) =>
					{
						fromIndex.Remove(s.hashid);
						toIndex.Insert(s.hashid, s);

					}, null);

				}
			}

		}
Exemple #13
0
 //MARK: Iteration
 public List<cpBody> ArrayForBodyType(cpBodyType type)
 {
     return type == cpBodyType.STATIC ?
         staticBodies : dynamicBodies;
 }