/** * @brief Create the internal shape used to represent a PolygonCollider. **/ public override SyncFrame.Physics2D.Shape[] CreateShapes() { if (_points == null || _points.Length == 0) { return(null); } TSVector2 lossy2D = new TSVector2(lossyScale.x, lossyScale.y); SyncFrame.Physics2D.Vertices v = new Physics2D.Vertices(); for (int index = 0, length = _points.Length; index < length; index++) { v.Add(TSVector2.Scale(_points[index], lossy2D)); } List <SyncFrame.Physics2D.Vertices> convexShapeVs = SyncFrame.Physics2D.BayazitDecomposer.ConvexPartition(v); SyncFrame.Physics2D.Shape[] result = new Physics2D.Shape[convexShapeVs.Count]; for (int index = 0, length = result.Length; index < length; index++) { result[index] = new SyncFrame.Physics2D.PolygonShape(convexShapeVs[index], 1); } return(result); }
private void CreateBodyFixture(Physics2D.Body body, Physics2D.Shape shape) { Physics2D.Fixture fixture = body.CreateFixture(shape); if (tsMaterial != null) { fixture.Friction = tsMaterial.friction; fixture.Restitution = tsMaterial.restitution; } }
private void CreateBody(Physics2D.World world) { Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world); if (tsMaterial == null) { tsMaterial = GetComponent <TSMaterial>(); } Physics2D.Shape shape = Shape; if (shape != null) { CreateBodyFixture(body, shape); } else { Physics2D.Shape[] shapes = CreateShapes(); for (int index = 0, length = shapes.Length; index < length; index++) { CreateBodyFixture(body, shapes[index]); } } if (tsRigidBody == null) { body.BodyType = Physics2D.BodyType.Static; } else { if (tsRigidBody.isKinematic) { body.BodyType = SyncFrame.Physics2D.BodyType.Kinematic; } else { body.BodyType = Physics2D.BodyType.Dynamic; body.IgnoreGravity = !tsRigidBody.useGravity; } if (tsRigidBody.mass <= 0) { tsRigidBody.mass = 1; } body.FixedRotation = tsRigidBody.freezeZAxis; body.Mass = tsRigidBody.mass; body.TSLinearDrag = tsRigidBody.drag; body.TSAngularDrag = tsRigidBody.angularDrag; } body.IsSensor = isTrigger; body.CollidesWith = Physics2D.Category.All; _body = body; }
private static object OverlapGeneric(Physics2D.Shape shape, TSVector2 position, Physics2D.BodySpecialSensor sensorType, int layerMask) { Physics2D.World world = (Physics2D.World)Physics2DWorldManager.instance.GetWorld(); Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world); body.CreateFixture(shape); body.BodyType = Physics2D.BodyType.Static; body.IsSensor = true; body.CollidesWith = Physics2D.Category.All; body.SpecialSensor = sensorType; body.SpecialSensorMask = layerMask; body.Position = position; world.RemoveBody(body); world.ProcessRemovedBodies(); if (body._specialSensorResults.Count > 0) { if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce) { return(Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[0]).GetComponent <TSCollider2D>()); } else { TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count]; for (int i = 0; i < body._specialSensorResults.Count; i++) { result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent <TSCollider2D>(); } return(result); } } return(null); }