9 [RequireComponent(typeof(TSTransform2D))]
14 private Physics2D.
Shape shape;
25 protected set { shape = value; }
28 [FormerlySerializedAs(
"isTrigger")]
30 private bool _isTrigger;
39 return _body.IsSensor;
49 _body.IsSensor = value;
61 private TSVector2 center;
63 private Vector3 scaledCenter;
65 internal Physics2D.Body _body;
108 public virtual Physics2D.Shape[] CreateShapes() {
109 return new Physics2D.Shape[0];
137 tsTransform = this.GetComponent<TSTransform2D>();
138 tsRigidBody = this.GetComponent<TSRigidBody2D>();
140 if (lossyScale == TSVector.one) {
141 lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
145 public void Update() {
146 if (!Application.isPlaying) {
147 lossyScale = TSVector.Abs(transform.lossyScale.ToTSVector());
151 private void CreateBodyFixture(Physics2D.Body body, Physics2D.Shape shape) {
152 Physics2D.Fixture fixture = body.CreateFixture(shape);
154 if (tsMaterial != null) {
155 fixture.Friction = tsMaterial.
friction;
160 private void CreateBody(Physics2D.World world) {
161 Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
163 if (tsMaterial == null) {
164 tsMaterial = GetComponent<TSMaterial>();
167 Physics2D.Shape shape =
Shape;
169 CreateBodyFixture(body, shape);
171 Physics2D.Shape[] shapes = CreateShapes();
172 for (
int index = 0, length = shapes.Length; index < length; index++) {
173 CreateBodyFixture(body, shapes[index]);
177 if (tsRigidBody == null) {
178 body.BodyType = Physics2D.BodyType.Static;
181 body.BodyType =
TrueSync.Physics2D.BodyType.Kinematic;
183 body.BodyType = Physics2D.BodyType.Dynamic;
187 if (tsRigidBody.
mass <= 0) {
188 tsRigidBody.
mass = 1;
192 body.Mass = tsRigidBody.
mass;
193 body.TSLinearDrag = tsRigidBody.
drag;
198 body.CollidesWith = Physics2D.Category.All;
210 private void CheckPhysics() {
224 Vector3 position = _body != null ? _body.TSPosition.ToVector() : (transform.position +
ScaledCenter.ToVector());
225 Quaternion rotation = _body != null ? Quaternion.Euler(0, 0, (_body.Rotation * FP.Rad2Deg).AsFloat()) : transform.rotation;
227 Gizmos.color = Color.yellow;
229 Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation,
GetGizmosSize());
230 Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
232 Gizmos.matrix *= cubeTransform;
236 Gizmos.matrix = oldGizmosMatrix;
254 return _body != null;
void Awake()
Creates a new TSRigidBody when there is no one attached to this GameObject.
bool isTrigger
If it is only a trigger and doesn't interfere on collisions.
void Initialize(Physics2D.World world)
Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity's ...
FP angularDrag
Angular drag coeficient.
bool IsBodyInitialized
Returns true if the body was already initialized.
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
bool useGravity
If true it uses gravity force.
Simulates physical properties of a body.
virtual void OnDrawGizmos()
Do a base matrix transformation to draw correctly all collider gizmos.
FP friction
Static friction when in contact.
FP restitution
Coeficient of restitution.
TSVector2 Center
Center of the collider shape.
virtual Physics2D.Shape CreateShape()
Creates the shape related to a concrete implementation of TSCollider.
TSRigidBody2D attachedRigidbody
Returns the TSRigidBody attached.
Physics2D.Shape Shape
Shape used by a collider.
abstract void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
FP drag
Linear drag coeficient.
TSVector lossyScale
Holds an first value of the GameObject's lossy scale.
Manages physics simulation.
bool freezeZAxis
If true it freezes Z rotation of the RigidBody (it only appears when in 2D Physics).
bool isKinematic
If true it doesn't get influences from external forces.
TSMaterial tsMaterial
Simulated material.
Represents a physical 2D rigid body.
TSVector2 ScaledCenter
Returns a version of collider's center scaled by parent's transform.
Abstract collider for 2D shapes.
abstract Vector3 GetGizmosSize()
Returns the gizmos size.
IBody2D Body
Returns RigidBody associated to this TSRigidBody.