/** * @brief Initializes internal properties based on whether there is a {@link TSCollider} attached. **/ public void Initialize() { if (initialized) { return; } tsCollider = GetComponent <TSCollider>(); if (transform.parent != null) { tsParent = transform.parent.GetComponent <TSTransform>(); } if (!_serialized) { UpdateEditMode(); } if (tsCollider != null) { if (tsCollider.IsBodyInitialized) { tsCollider.Body.TSPosition = _position + scaledCenter; tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation); } } else { StateTracker.AddTracking(this); } initialized = true; }
public TSRaycastHit Raycast(TSRay ray, FP maxDistance, RaycastCallback callback = null) { IBody hitBody; TSVector hitNormal; FP hitFraction; TSVector origin = ray.origin; TSVector direction = ray.direction; if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction)) { if (hitFraction <= maxDistance) { GameObject other = PhysicsManager.instance.GetGameObject(hitBody); TSRigidBody bodyComponent = other.GetComponent <TSRigidBody>(); TSCollider colliderComponent = other.GetComponent <TSCollider>(); TSTransform transformComponent = other.GetComponent <TSTransform>(); return(new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, ray.direction, hitFraction)); } } else { direction *= maxDistance; if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction)) { GameObject other = PhysicsManager.instance.GetGameObject(hitBody); TSRigidBody bodyComponent = other.GetComponent <TSRigidBody>(); TSCollider colliderComponent = other.GetComponent <TSCollider>(); TSTransform transformComponent = other.GetComponent <TSTransform>(); return(new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, direction, hitFraction)); } } return(null); }
private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation) { ICollider[] tsColliders = go.GetComponentsInChildren <ICollider>(); if (tsColliders != null) { for (int index = 0, length = tsColliders.Length; index < length; index++) { PhysicsManager.instance.AddBody(tsColliders[index]); } } TSTransform rootTSTransform = go.GetComponent <TSTransform>(); if (rootTSTransform != null) { rootTSTransform.Initialize(); rootTSTransform.position = position; rootTSTransform.rotation = rotation; } TSTransform[] tsTransforms = go.GetComponentsInChildren <TSTransform>(); if (tsTransforms != null) { for (int index = 0, length = tsTransforms.Length; index < length; index++) { TSTransform tsTransform = tsTransforms[index]; if (tsTransform != rootTSTransform) { tsTransform.Initialize(); } } } TSTransform2D rootTSTransform2D = go.GetComponent <TSTransform2D>(); if (rootTSTransform2D != null) { rootTSTransform2D.Initialize(); rootTSTransform2D.position = new TSVector2(position.x, position.y); rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z; } TSTransform2D[] tsTransforms2D = go.GetComponentsInChildren <TSTransform2D>(); if (tsTransforms2D != null) { for (int index = 0, length = tsTransforms2D.Length; index < length; index++) { TSTransform2D tsTransform2D = tsTransforms2D[index]; if (tsTransform2D != rootTSTransform2D) { tsTransform2D.Initialize(); } } } }
public TSRaycastHit(TSRigidBody rigidbody, TSCollider collider, TSTransform transform, TSVector normal, TSVector origin, TSVector direction, FP fraction) { this.rigidbody = rigidbody; this.collider = collider; this.transform = transform; this.normal = normal; this.point = origin + direction * fraction; this.distance = fraction * direction.magnitude; }
/** * @brief Creates a new {@link TSRigidBody} when there is no one attached to this GameObject. **/ public void Awake() { tsTransform = this.GetComponent <TSTransform>(); tsRigidBody = this.GetComponent <TSRigidBody>(); if (lossyScale == TSVector.one) { lossyScale = TSVector.Abs(transform.localScale.ToTSVector()); } }
internal void Update(GameObject otherGO, Contact c) { if (this.gameObject == null) { this.gameObject = otherGO; this.collider = this.gameObject.GetComponent <TSCollider>(); this.rigidbody = this.gameObject.GetComponent <TSRigidBody>(); this.transform = this.collider.tsTransform; } if (c != null) { if (contacts[0] == null) { contacts[0] = new TSContactPoint(); } this.relativeVelocity = c.CalculateRelativeVelocity(); contacts[0].normal = c.Normal; contacts[0].point = c.p1; } }
/** * @brief Moves game object based on provided translation vector and a relative {@link TSTransform}. * * The game object will move based on TSTransform's forward vector. **/ public void Translate(TSVector translation, TSTransform relativeTo) { this.position += TSVector.Transform(translation, TSMatrix.CreateFromQuaternion(relativeTo.rotation)); }
/** * @brief Moves game object based on provided axis values and a relative {@link TSTransform}. * * The game object will move based on TSTransform's forward vector. **/ public void Translate(FP x, FP y, FP z, TSTransform relativeTo) { Translate(new TSVector(x, y, z), relativeTo); }
/** * @brief Rotates game object to point forward vector to a target position. * * @param other TSTrasform used to get target position. **/ public void LookAt(TSTransform other) { LookAt(other.position); }