public PhyFlipper(FlipperBehavior flipper) : base(PhyType.Flipper) { Mass = flipper.data.Mass * 3.0f; RotationDirection = flipper.data.StartAngle > flipper.data.EndAngle ? -1 : 1; _height = flipper.data.Height; _startAngle = flipper.data.StartAngle * Mathf.PI / 180.0f; _endAngle = flipper.data.EndAngle * Mathf.PI / 180.0f; SetupRigidBody(flipper.data.Mass, _AddFlipperCylinders(flipper)); SetProperties( Mass, flipper.data.Friction, flipper.data.Elasticity * 100.0f); base.matrix = Matrix4x4.TRS( flipper.gameObject.transform.localPosition, flipper.gameObject.transform.localRotation, UnityEngine.Vector3.one ).ToBullet(); base.name = flipper.name; base.entity = Entity.Null; }
CollisionShape _AddFlipperCylinders(FlipperBehavior flipper) { float r1 = flipper.data.BaseRadius; float r2 = flipper.data.EndRadius; float h = flipper.data.Height; float l = flipper.data.FlipperRadius; var hh = h * 0.5f; // half height var cs = new BulletSharp.CompoundShape(); cs.AddChildShape( Matrix.Translation(0, 0, hh), new CylinderShapeZ(r1, r1, hh)); cs.AddChildShape( Matrix.Translation(0, -l, hh), new CylinderShapeZ(r2, r2, hh)); // we can't add Triangle Mesh Shape to Compound Shape. Add one or two boxes float hbl = new Vector2(l, r1 - r2).magnitude * 0.5f; Vector3 n = new Vector3(l, r1 - r2, 0); n.Normalize(); Vector3 beg = new Vector3(0, 0, hh) + n * (r1 - r2); Vector3 beg2 = new Vector3(-beg.X, beg.Y, beg.Z); Vector3 end = new Vector3(0, -l, hh); float angle = math.atan2(n.Y, n.X); bool onlyFront = true; bool rev = (flipper.data.StartAngle < 0 | flipper.data.StartAngle > 180); if (!onlyFront || rev) { cs.AddChildShape( Matrix.RotationZ(-angle) * Matrix.Translation((beg + end) * 0.5f), new BoxShape(Mathf.Min(r1, r2), hbl, hh)); } if (!onlyFront || !rev) { cs.AddChildShape( Matrix.RotationZ(angle) * Matrix.Translation((beg2 + end) * 0.5f), new BoxShape(Mathf.Min(r1, r2), hbl, hh)); } return(cs); }
protected override void OnEnable() { base.OnEnable(); _flipper = (FlipperBehavior)target; }