/** * @brief Instantiates a new prefab in a deterministic way. * * @param prefab GameObject's prefab to instantiate. * @param position Position to place the new GameObject. * @param rotation Rotation to set in the new GameObject. **/ public static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation) { if (instance != null && instance.lockstep != null) { GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject; AddGameObjectOnSafeMap(go); foreach (MonoBehaviour bh in go.GetComponentsInChildren <MonoBehaviour>()) { if (bh is ITrueSyncBehaviour) { instance.queuedBehaviours.Add(instance.NewManagedBehavior((ITrueSyncBehaviour)bh)); } } InitializeGameObject(go, position, rotation); return(go); } return(null); }
private void UpdatePlayMode() { if (tsParent != null) { _localPosition = tsParent.InverseTransformPoint(position); TSMatrix matrix = TSMatrix.CreateFromQuaternion(tsParent.rotation); _localRotation = TSQuaternion.CreateFromMatrix(TSMatrix.Inverse(matrix)) * rotation; } else { _localPosition = position; _localRotation = rotation; } if (rb != null) { transform.position = position.ToVector(); transform.rotation = rotation.ToQuaternion(); transform.localScale = localScale.ToVector(); /*if (rb.interpolation == TSRigidBody.InterpolateMode.Interpolate) { * transform.position = Vector3.Lerp(transform.position, position.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR); * transform.rotation = Quaternion.Lerp(transform.rotation, rotation.ToQuaternion(), Time.deltaTime * DELTA_TIME_FACTOR); * transform.localScale = Vector3.Lerp(transform.localScale, localScale.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR); * return; * } else if (rb.interpolation == TSRigidBody.InterpolateMode.Extrapolate) { * transform.position = (position + rb.tsCollider.Body.TSLinearVelocity * Time.deltaTime * DELTA_TIME_FACTOR).ToVector(); * transform.rotation = Quaternion.Lerp(transform.rotation, rotation.ToQuaternion(), Time.deltaTime * DELTA_TIME_FACTOR); * transform.localScale = Vector3.Lerp(transform.localScale, localScale.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR); * return; * }*/ } transform.position = position.ToVector(); transform.rotation = rotation.ToQuaternion(); transform.localScale = localScale.ToVector(); _scale = transform.lossyScale.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; } }
public virtual void GetBoundingBox(ref TSMatrix orientation, out TSBBox box) { TSVector zero = TSVector.zero; zero.Set(orientation.M11, orientation.M21, orientation.M31); this.SupportMapping(ref zero, out zero); box.max.x = orientation.M11 * zero.x + orientation.M21 * zero.y + orientation.M31 * zero.z; zero.Set(orientation.M12, orientation.M22, orientation.M32); this.SupportMapping(ref zero, out zero); box.max.y = orientation.M12 * zero.x + orientation.M22 * zero.y + orientation.M32 * zero.z; zero.Set(orientation.M13, orientation.M23, orientation.M33); this.SupportMapping(ref zero, out zero); box.max.z = orientation.M13 * zero.x + orientation.M23 * zero.y + orientation.M33 * zero.z; zero.Set(-orientation.M11, -orientation.M21, -orientation.M31); this.SupportMapping(ref zero, out zero); box.min.x = orientation.M11 * zero.x + orientation.M21 * zero.y + orientation.M31 * zero.z; zero.Set(-orientation.M12, -orientation.M22, -orientation.M32); this.SupportMapping(ref zero, out zero); box.min.y = orientation.M12 * zero.x + orientation.M22 * zero.y + orientation.M32 * zero.z; zero.Set(-orientation.M13, -orientation.M23, -orientation.M33); this.SupportMapping(ref zero, out zero); box.min.z = orientation.M13 * zero.x + orientation.M23 * zero.y + orientation.M33 * zero.z; }
internal void InverseTransform(ref TSVector position, ref TSMatrix orientation) { TSVector.Subtract(ref this.max, ref position, out this.max); TSVector.Subtract(ref this.min, ref position, out this.min); TSVector tSVector; TSVector.Add(ref this.max, ref this.min, out tSVector); tSVector.x *= FP.Half; tSVector.y *= FP.Half; tSVector.z *= FP.Half; TSVector tSVector2; TSVector.Subtract(ref this.max, ref this.min, out tSVector2); tSVector2.x *= FP.Half; tSVector2.y *= FP.Half; tSVector2.z *= FP.Half; TSVector.TransposedTransform(ref tSVector, ref orientation, out tSVector); TSMatrix tSMatrix; TSMath.Absolute(ref orientation, out tSMatrix); TSVector.TransposedTransform(ref tSVector2, ref tSMatrix, out tSVector2); TSVector.Add(ref tSVector, ref tSVector2, out this.max); TSVector.Subtract(ref tSVector, ref tSVector2, out this.min); }
public static void CreateFromAxisAngle(ref TSVector axis, FP angle, out TSMatrix result) { FP x = axis.x; FP y = axis.y; FP z = axis.z; FP x2 = FP.Sin(angle); FP x3 = FP.Cos(angle); FP fP = x * x; FP fP2 = y * y; FP fP3 = z * z; FP fP4 = x * y; FP fP5 = x * z; FP fP6 = y * z; result.M11 = fP + x3 * (FP.One - fP); result.M12 = fP4 - x3 * fP4 + x2 * z; result.M13 = fP5 - x3 * fP5 - x2 * y; result.M21 = fP4 - x3 * fP4 - x2 * z; result.M22 = fP2 + x3 * (FP.One - fP2); result.M23 = fP6 - x3 * fP6 + x2 * x; result.M31 = fP5 - x3 * fP5 + x2 * y; result.M32 = fP6 - x3 * fP6 - x2 * x; result.M33 = fP3 + x3 * (FP.One - fP3); }
/** * @brief Applies the provided force in the body. * * @param force A {@link TSVector} representing the force to be applied. **/ public void AddForce(TSVector force) { AddForce(force, ForceMode.Force); }
/// <summary> /// Subtracts two vectors. /// </summary> /// <param name="value1">The first vector.</param> /// <param name="value2">The second vector.</param> /// <returns>The difference of both vectors.</returns> #region public static JVector operator -(JVector value1, JVector value2) public static TSVector operator -(TSVector value1, TSVector value2) { TSVector result; TSVector.Subtract(ref value1, ref value2, out result); return(result); }
/// <summary> /// Adds two vectors. /// </summary> /// <param name="value1">The first vector.</param> /// <param name="value2">The second vector.</param> /// <returns>The sum of both vectors.</returns> #region public static JVector operator +(JVector value1, JVector value2) public static TSVector operator +(TSVector value1, TSVector value2) { TSVector result; TSVector.Add(ref value1, ref value2, out result); return(result); }
public static TSVector Lerp(TSVector from, TSVector to, FP percent) { return(from + (to - from) * percent); }
/// <summary> /// Calculates the dot product of two vectors. /// </summary> /// <param name="value1">The first vector.</param> /// <param name="value2">The second vector.</param> /// <returns>Returns the dot product of both.</returns> #region public static FP operator *(JVector value1, JVector value2) public static FP operator *(TSVector value1, TSVector value2) { return(TSVector.Dot(ref value1, ref value2)); }
/** * @brief Simulates the provided tourque in the body. * * @param torque A {@link TSVector} representing the torque to be applied. **/ public void AddTorque(TSVector torque) { tsCollider.RigidBody.TSApplyTorque(torque); }
/** * @brief Changes orientation to look at target position. * * @param target A {@link TSVector} representing the position to look at. **/ public void LookAt(TSVector target) { rotation = TSQuaternion.CreateFromMatrix(TSMatrix.CreateFromLookAt(position, target)); }
public static void Dot(ref TSVector vector1, ref TSVector vector2, out FP result) { result = (vector1.x * vector2.x) + (vector1.y * vector2.y) + (vector1.z * vector2.z); }
// Projects a vector onto a plane defined by a normal orthogonal to the plane. public static TSVector ProjectOnPlane(TSVector vector, TSVector planeNormal) { return(vector - Project(vector, planeNormal)); }
/// <summary> /// Calculates the dot product of two vectors. /// </summary> /// <param name="vector1">The first vector.</param> /// <param name="vector2">The second vector.</param> /// <returns>Returns the dot product of both vectors.</returns> #region public static FP Dot(JVector vector1, JVector vector2) public static FP Dot(TSVector vector1, TSVector vector2) { return(TSVector.Dot(ref vector1, ref vector2)); }
/// <summary> /// Calculates the dot product of both vectors. /// </summary> /// <param name="vector1">The first vector.</param> /// <param name="vector2">The second vector.</param> /// <returns>Returns the dot product of both vectors.</returns> public static FP Dot(ref TSVector vector1, ref TSVector vector2) { return((vector1.x * vector2.x) + (vector1.y * vector2.y) + (vector1.z * vector2.z)); }
/// <summary> /// Transforms a vector by the transposed of the given Matrix. /// </summary> /// <param name="position">The vector to transform.</param> /// <param name="matrix">The transform matrix.</param> /// <param name="result">The transformed vector.</param> public static void TransposedTransform(ref TSVector position, ref TSMatrix matrix, out TSVector result) { FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M12)) + (position.z * matrix.M13); FP num1 = ((position.x * matrix.M21) + (position.y * matrix.M22)) + (position.z * matrix.M23); FP num2 = ((position.x * matrix.M31) + (position.y * matrix.M32)) + (position.z * matrix.M33); result.x = num0; result.y = num1; result.z = num2; }
/// <summary> /// Transforms a vector by the given matrix. /// </summary> /// <param name="position">The vector to transform.</param> /// <param name="matrix">The transform matrix.</param> /// <param name="result">The transformed vector.</param> public static void Transform(ref TSVector position, ref TSMatrix matrix, out TSVector result) { result.x = ((position.x * matrix.M11) + (position.y * matrix.M21)) + (position.z * matrix.M31); result.y = ((position.x * matrix.M12) + (position.y * matrix.M22)) + (position.z * matrix.M32); result.z = ((position.x * matrix.M13) + (position.y * matrix.M23)) + (position.z * matrix.M33); }
/// <summary> /// Gets a vector with the maximum x,y and z values of both vectors. /// </summary> /// <param name="value1">The first value.</param> /// <param name="value2">The second value.</param> /// <param name="result">A vector with the maximum x,y and z values of both vectors.</param> public static void Max(ref TSVector value1, ref TSVector value2, out TSVector result) { result.x = (value1.x > value2.x) ? value1.x : value2.x; result.y = (value1.y > value2.y) ? value1.y : value2.y; result.z = (value1.z > value2.z) ? value1.z : value2.z; }
/** * @brief Applies the provided force in the body. * * @param force A {@link TSVector} representing the force to be applied. * @param position Indicates the location where the force should hit. **/ public void AddForceAtPosition(TSVector force, TSVector position) { AddForceAtPosition(force, position, ForceMode.Force); }
/// <summary> /// 初始化物体身上的Ts脚本 /// </summary> /// <param name="go"></param> /// <param name="position"></param> /// <param name="rotation"></param> private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation) { //将物体身上所有Collider注册到物理管理器 var tsColliders = go.GetComponentsInChildren <ICollider>(); if (tsColliders != null) { for (int index = 0, length = tsColliders.Length; index < length; index++) { PhysicsManager.instance.AddBody(tsColliders[index]); } } //初始化物体和子物体身上所有tsTransform var rootTSTransform = go.GetComponent <TSTransform>(); if (rootTSTransform != null) { rootTSTransform.Initialize(position, rotation); } var tsTransforms = go.GetComponentsInChildren <TSTransform>(); if (tsTransforms != null) { for (int index = 0, length = tsTransforms.Length; index < length; index++) { var tsTransform = tsTransforms[index]; if (tsTransform != rootTSTransform) { tsTransform.Initialize(); } } } //初始化物体和子物体身上所有tsTransform2D var rootTSTransform2D = go.GetComponent <TSTransform2D>(); if (rootTSTransform2D != null) { rootTSTransform2D.Initialize(); rootTSTransform2D.position = new TSVector2(position.x, position.y); rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z; } var tsTransforms2D = go.GetComponentsInChildren <TSTransform2D>(); if (tsTransforms2D != null) { for (int index = 0, length = tsTransforms2D.Length; index < length; index++) { var tsTransform2D = tsTransforms2D[index]; if (tsTransform2D != rootTSTransform2D) { tsTransform2D.Initialize(); } } } }
/** * @brief Returns the velocity of the body at some position in world space. **/ public TSVector GetPointVelocity(TSVector worldPoint) { TSVector directionPoint = position - tsCollider.RigidBody.TSPosition; return(TSVector.Cross(tsCollider.RigidBody.TSAngularVelocity, directionPoint) + tsCollider.RigidBody.TSLinearVelocity); }
public TSVector InverseTransformVector(TSVector vector) { return(InverseTransformVector(new TSVector4(vector.x, vector.y, vector.z, FP.Zero)).ToTSVector()); }
/** * @brief Simulates the provided relative tourque in the body. * * @param torque A {@link TSVector} representing the relative torque to be applied. **/ public void AddRelativeTorque(TSVector torque) { tsCollider.RigidBody.TSApplyRelativeTorque(torque); }
/// <summary> /// Multiply a vector with a factor. /// </summary> /// <param name="value1">The vector to multiply.</param> /// <param name="scaleFactor">The scale factor.</param> /// <param name="result">Returns the multiplied vector.</param> public static void Multiply(ref TSVector value1, FP scaleFactor, out TSVector result) { result.x = value1.x * scaleFactor; result.y = value1.y * scaleFactor; result.z = value1.z * scaleFactor; }
/** * @brief Moves the body to a new position. **/ public void MovePosition(TSVector position) { this.position = position; }
/// <summary> /// Multiplies each component of the vector by the same components of the provided vector. /// </summary> public void Scale(TSVector other) { this.x = x * other.x; this.y = y * other.y; this.z = z * other.z; }
// Returns the angle in degrees between /from/ and /to/. This is always the smallest public static FP Angle(TSVector from, TSVector to) { return(TSMath.Acos(TSMath.Clamp(Dot(from.normalized, to.normalized), -FP.ONE, FP.ONE)) * TSMath.Rad2Deg); }
/// <summary> /// Divides a vector by a factor. /// </summary> /// <param name="value1">The vector to divide.</param> /// <param name="scaleFactor">The scale factor.</param> /// <param name="result">Returns the scaled vector.</param> public static void Divide(ref TSVector value1, FP scaleFactor, out TSVector result) { result.x = value1.x / scaleFactor; result.y = value1.y / scaleFactor; result.z = value1.z / scaleFactor; }