/* * @brief remove a shape from body * @param shape the shape to be removed * @param reduceMassAndMoment if this is true, the body mass and moment will be reduced by shape. the default is true */ public void RemoveShape(CCPhysicsShape shape, bool reduceMassAndMoment = true) { if (_shapes.Exists((s) => s == shape)) { // deduce the area, mass and moment // area must update before mass, because the density changes depend on it. if (reduceMassAndMoment) { _area -= shape.Area; AddMass(-shape.Mass); AddMoment(-shape.Moment); } //remove if (_world != null) { _world.RemoveShape(shape); } // set shape->_body = nullptr make the shape->setBody will not trigger the _body->removeShape function call. shape._body = null; shape.Body = null; _shapes.Remove(shape); } }
public virtual void RemoveShape(CCPhysicsShape shape) { if (shape != null) { _info.removeShape(shape._info); } }
public virtual CCPhysicsShape AddShape(CCPhysicsShape shape) { if (shape != null) { _info.addShape(shape._info); return(shape); } return(null); }
public override bool HitTest(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { if (shapeA.GetGroup() == _group || shapeB.GetGroup() == _group) { return(true); } return(false); }
public override bool HitTest(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { if ((shapeA == _a && shapeB == _b) || (shapeA == _b && shapeB == _a)) { return(true); } return(false); }
public CCPhysicsShapeInfo(CCPhysicsShape shape) { _shapes = new List <cpShape>(); // TODO: Complete member initialization _shape = shape; _body = _sharedBody; _group = cp.NO_GROUP; //CP_NO_GROUP ?¿ }
public CCPhysicsShapeInfo(CCPhysicsShape shape) { _shapes = new List<cpShape>(); // TODO: Complete member initialization _shape = shape; _body = _sharedBody; _group = cp.NO_GROUP; //CP_NO_GROUP ?¿ }
public CCPhysicsContact() : base(PHYSICSCONTACT_EVENT_NAME) { _world = null; _shapeA = null; _shapeB = null; _eventCode = EventCode.NONE; _info = null; _notificationEnable = true; _result = true; _data = null; _contactInfo = null; _contactData = null; _preContactData = null; }
public CCPhysicsRayCastInfo(CCPhysicsShape shape, CCPoint start, CCPoint end, CCPoint contact, CCPoint normal, float fraction, object data) { this.shape = shape; this.start = start; this.end = end; //< in lua, it's name is "ended" this.contact = contact; this.normal = normal; this.fraction = fraction; this.data = data; }
public CCPhysicsShapeInfo(CCPhysicsShape shape) { _map = new Dictionary<cpShape, CCPhysicsShapeInfo>(); _shapes = new List<cpShape>(); // TODO: Complete member initialization _shape = shape; if (_sharedBody == null) _sharedBody = cpBody.NewStatic(); // = cpBodyNewStatic(); ¿¿ _body = _sharedBody; _group = cp.NO_GROUP; //CP_NO_GROUP ?¿ }
public CCPhysicsShapeInfo(CCPhysicsShape shape) { _map = new Dictionary <cpShape, CCPhysicsShapeInfo>(); _shapes = new List <cpShape>(); // TODO: Complete member initialization _shape = shape; if (_sharedBody == null) { _sharedBody = cpBody.NewStatic(); // = cpBodyNewStatic(); ¿¿ } _body = _sharedBody; _group = cp.NO_GROUP; //CP_NO_GROUP ?¿ }
//public static PhysicsContact Construct(PhysicsShape a, PhysicsShape b) //{ // PhysicsContact contact = new PhysicsContact(); // if (contact != null && contact.Init(a, b)) // { // return contact; // } // return null; //} bool Init(CCPhysicsShape a, CCPhysicsShape b) { if (a == null || b == null) { return(false); } _info = new CCPhysicsContactInfo(this); if (_info == null) { return(false); } _shapeA = a; _shapeB = b; return(true); }
/* * @brief add a shape to body * @param shape the shape to be added * @param addMassAndMoment if this is true, the shape's mass and moment will be added to body. the default is true */ public virtual CCPhysicsShape AddShape(CCPhysicsShape shape, bool addMassAndMoment = true) { if (shape == null) { return(null); } // add shape to body if (!_shapes.Exists((s) => s == shape)) { shape.Body = this; // calculate the area, mass, and desity // area must update before mass, because the density changes depend on it. if (addMassAndMoment) { _area += shape.Area; AddMass(shape.Mass); AddMoment(shape.Moment); } if (_world != null) { _world.AddShape(shape); } _shapes.Add(shape); if (_group != cp.NO_GROUP && shape.Group == cp.NO_GROUP) { shape.Group = _group; } } return(shape); }
public String GetFixtureName(CCPhysicsShape fixture) { if (m_fixtureToNameMap.ContainsKey(fixture)) return m_fixtureToNameMap[fixture]; return null; }
protected void readCustomPropertiesFromJson(CCPhysicsShape item, JObject value) { if (null == item) return; if (value["customProperties"] != null) return; int i = 0; JArray propValues = (JArray)value["customProperties"]; if (null != propValues) { int numPropValues = propValues.Count; for (i = 0; i < numPropValues; i++) { JObject propValue = (JObject)propValues[i]; String propertyName = propValue["name"].ToString(); if (propValue["int"] != null) SetCustomInt(item, propertyName, (int)propValue["int"]); if (propValue["float"] != null) SetCustomFloat(item, propertyName, (float)propValue["float"]); if (propValue["string"] != null) SetCustomString(item, propertyName, propValue["string"].ToString()); if (propValue["vec2"] != null) SetCustomVector(item, propertyName, this.jsonToVec("vec2", propValue)); if (propValue["bool"] != null) SetCustomBool(item, propertyName, (bool)propValue["bool"]); } } }
//public static PhysicsContact Construct(PhysicsShape a, PhysicsShape b) //{ // PhysicsContact contact = new PhysicsContact(); // if (contact != null && contact.Init(a, b)) // { // return contact; // } // return null; //} bool Init(CCPhysicsShape a, CCPhysicsShape b) { if (a == null || b == null) return false; _info = new CCPhysicsContactInfo(this); if (_info != null) return false; _shapeA = a; _shapeB = b; return true; }
public virtual CCPhysicsShape AddShape(CCPhysicsShape shape) { if (shape != null) { _info.addShape(shape._info); return shape; } return null; }
public override bool HitTest(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { if ((shapeA == _a && shapeB == _b) || (shapeA == _b && shapeB == _a)) { return true; } return false; }
//public void Draw() //{ // if (DebugDrawMask != cpDrawFlags.None) // { // _debugDraw.Begin(); // _info.getSpace().DrawDebugData(); // _debugDraw.End(); // } //} //public virtual void DebugDraw() //{ // if (_debugDraw == null) // { // _debugDraw = new PhysicsDebugDraw(this); // } // if (_debugDraw != null && _bodies.Count > 0) // { // if (_debugDraw.Begin()) // { // if (DebugDrawMask == cpDrawFlags.ALL || DebugDrawMask == cpDrawFlags.Shape) // { // foreach (CCPhysicsBody body in _bodies) // { // if (!body.IsEnabled()) // { // continue; // } // foreach (CCPhysicsShape shape in body.GetShapes()) // { // _debugDraw.DrawShape(shape); // } // } // } // if (DebugDrawMask == cpDrawFlags.ALL || DebugDrawMask == cpDrawFlags.Joint) // { // foreach (CCPhysicsJoint joint in _joints) // { // _debugDraw.DrawJoint(joint); // } // } // _debugDraw.End(); // } // } //} public virtual bool CollisionBeginCallback(CCPhysicsContact contact) { bool ret = true; CCPhysicsShape shapeA = contact.GetShapeA(); CCPhysicsShape shapeB = contact.GetShapeB(); CCPhysicsBody bodyA = shapeA.GetBody(); CCPhysicsBody bodyB = shapeB.GetBody(); List <CCPhysicsJoint> jointsA = bodyA.GetJoints(); // check the joint is collision enable or not foreach (CCPhysicsJoint joint in jointsA) { if (!_joints.Exists(j => j == joint)) { continue; } if (!joint.IsCollisionEnabled()) { CCPhysicsBody body = joint.GetBodyA() == bodyA?joint.GetBodyB() : joint.GetBodyA(); if (body == bodyB) { contact.SetNotificationEnable(false); return(false); } } } // bitmask check if ((shapeA.GetCategoryBitmask() & shapeB.GetContactTestBitmask()) == 0 || (shapeA.GetContactTestBitmask() & shapeB.GetCategoryBitmask()) == 0) { contact.SetNotificationEnable(false); } if (shapeA.GetGroup() != 0 && shapeA.GetGroup() == shapeB.GetGroup()) { ret = shapeA.GetGroup() > 0; } else { if ((shapeA.GetCategoryBitmask() & shapeB.GetCollisionBitmask()) == 0 || (shapeB.GetCategoryBitmask() & shapeA.GetCollisionBitmask()) == 0) { ret = false; } } if (contact.IsNotificationEnabled()) { contact.SetEventCode(EventCode.BEGIN); contact.SetWorld(this); _scene.DispatchEvent(contact); } return(ret ? contact.ResetResult() : false); }
public void SetCustomVector(CCPhysicsShape item, String propertyName, cpVect val) { m_fixturesWithCustomProperties.Add(item); GetCustomPropertiesForItem(item, true).m_customPropertyMap_cpVect.Add(propertyName, val); }
public CCPhysicsBody(CCPhysicsShape shape) : this(0f, 0f, CCPoint.Zero) { AddShape(shape); }
public virtual bool HitTest(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { //CC_UNUSED_PARAM(shapeA); //CC_UNUSED_PARAM(shapeB); return true; }
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; }
public virtual bool HitTest(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { //CC_UNUSED_PARAM(shapeA); //CC_UNUSED_PARAM(shapeB); return(true); }
public override bool HitTest(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { if (shapeA.GetGroup() == _group || shapeB.GetGroup() == _group) { return true; } return false; }
public void SetCustomBool(CCPhysicsShape item, String propertyName, bool val) { m_fixturesWithCustomProperties.Add(item); GetCustomPropertiesForItem(item, true).m_customPropertyMap_bool.Add(propertyName, val); }
public CCPhysicsContact(CCPhysicsShape a, CCPhysicsShape b) : this() { Init(a, b); }
public void SetCustomFloat(CCPhysicsShape item, String propertyName, float val) { m_fixturesWithCustomProperties.Add(item); GetCustomPropertiesForItem(item, true).m_customPropertyMap_float.Add(propertyName, (float)val); }
public EventListenerPhysicsContactWithShapes(CCPhysicsShape shapeA, CCPhysicsShape shapeB) { _a = shapeA; _b = shapeB; }
public void SetFixtureName(CCPhysicsShape fixture, String name) { m_fixtureToNameMap.Add(fixture, name); }
/* * @brief add a shape to body * @param shape the shape to be added * @param addMassAndMoment if this is true, the shape's mass and moment will be added to body. the default is true */ public virtual CCPhysicsShape AddShape(CCPhysicsShape shape, bool addMassAndMoment = true) { if (shape == null) return null; // add shape to body if (!_shapes.Exists((s) => s == shape)) { shape.Body = this; // calculate the area, mass, and desity // area must update before mass, because the density changes depend on it. if (addMassAndMoment) { _area += shape.Area; AddMass(shape.Mass); AddMoment(shape.Moment); } if (_world != null) { _world.AddShape(shape); } _shapes.Add(shape); if (_group != cp.NO_GROUP && shape.Group == cp.NO_GROUP) { shape.Group = _group; } } return shape; }
public void DrawShape(CCPhysicsShape shape) { foreach (cpShape item in shape._info.GetShapes()) DrawShape(item); }