8 [RequireComponent(typeof(TSTransform2D))]
13 private Physics2D.
Shape shape;
24 protected set { shape = value; }
38 private TSVector2 center;
40 private Vector3 scaledCenter;
42 internal Physics2D.Body _body;
108 tsTransform = this.GetComponent<TSTransform2D>();
109 tsRigidBody = this.GetComponent<TSRigidBody2D>();
112 lossyScale =
TSVector.Abs(transform.localScale.ToTSVector());
116 public void Update() {
117 if (!Application.isPlaying) {
118 lossyScale =
TSVector.Abs(transform.lossyScale.ToTSVector());
122 private void CreateBody(Physics2D.World world) {
123 Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
124 Physics2D.Fixture fixture = body.CreateFixture(
Shape);
126 if (tsMaterial == null) {
127 tsMaterial = GetComponent<TSMaterial>();
130 if (tsMaterial != null) {
131 fixture.Friction = tsMaterial.
friction;
135 if (tsRigidBody == null) {
136 body.BodyType = Physics2D.BodyType.Static;
139 body.BodyType =
TrueSync.Physics2D.BodyType.Kinematic;
141 body.BodyType = Physics2D.BodyType.Dynamic;
145 if (tsRigidBody.
mass <= 0) {
146 tsRigidBody.
mass = 1;
150 body.Mass = tsRigidBody.
mass;
154 body.CollidesWith = Physics2D.Category.All;
166 private void CheckPhysics() {
180 Vector3 position = _body != null ? _body.TSPosition.ToVector() : (transform.position +
ScaledCenter.ToVector());
181 Quaternion rotation = _body != null ? Quaternion.Euler(0, 0, (_body.Rotation * FP.Rad2Deg).AsFloat()) : transform.rotation;
183 Gizmos.color = Color.yellow;
185 Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation,
GetGizmosSize());
186 Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
188 Gizmos.matrix *= cubeTransform;
192 Gizmos.matrix = oldGizmosMatrix;
210 return _body != null;
void Awake()
Creates a new TSRigidBody when there is no one attached to this GameObject.
FP y
The Y component of the vector.
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 ...
abstract Physics2D.Shape CreateShape()
Creates the shape related to a concrete implementation of TSCollider.
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.
static readonly TSVector one
A vector with components (1,1,1);
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.
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...
Represents an interface to 2D bodies.
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).
FP x
The X component of the vector.
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.