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); }
/** * @brief Add a new RigidBody to the world. * * @param jRigidBody Instance of a {@link TSRigidBody}. **/ public void AddBody(ICollider iCollider) { if (!(iCollider is TSCollider)) { Debug.LogError("You have a 2D object but your Physics 2D is disabled."); return; } TSCollider tsCollider = (TSCollider)iCollider; if (tsCollider._body != null) { //already added return; } TSRigidBody tsRB = tsCollider.GetComponent <TSRigidBody>(); TSRigidBodyConstraints constraints = tsRB != null ? tsRB.constraints : TSRigidBodyConstraints.None; tsCollider.Initialize(); world.AddBody(tsCollider._body); gameObjectMap[tsCollider._body] = tsCollider.gameObject; if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>() != null) { TSCollider parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>(); world.AddConstraint(new ConstraintHierarchy(parentCollider.Body, tsCollider._body, (tsCollider.GetComponent <TSTransform>().position + tsCollider.ScaledCenter) - (parentCollider.GetComponent <TSTransform>().position + parentCollider.ScaledCenter))); } tsCollider._body.FreezeConstraints = constraints; }
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()); } }
public void Start() { if (!Application.isPlaying) { return; } Initialize(); rb = GetComponent <TSRigidBody> (); }
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; } }