So i have been working on this issue for a while and I’m just stuck. I have 2 objects at (0,0,0) except they are rotated at different degrees.
If I have ObjectA that has a Euler rotation of (0,0,0) and ObjectB has a Euler rotation of (0,90,0) then I use this code to get a relative force on B to be enacted on ObjectA
public static float3 RotateAround(float3 position, float3 pivotPoint, quaternion rot)
{
return math.mul(rot, (position - pivotPoint)) + pivotPoint;
}
/// <summary>
/// Rotates a transform around the given Pivot point by the given rotation.
/// </summary>
/// <param name="transform"></param>
/// <param name="pivotPoint"></param>
/// <param name="rot"></param>
/// <returns></returns>
public static LocalToWorld RotateAround(LocalToWorld transform, float3 pivotPoint, quaternion rot)
{
return new LocalToWorld
{
Value = new float4x4(math.mul(rot, transform.Rotation),
math.mul(rot, (transform.Position - pivotPoint)) + pivotPoint
)
};
}
/// <summary>
/// Rotates a transform around the given Pivot point by the given angle on the given axis.
/// </summary>
/// <param name="transform"></param>
/// <param name="pivotPoint"></param>
/// <param name="axis"></param>
/// <param name="angle"></param>
public static void RotateAround(Transform transform, Vector3 pivotPoint, Vector3 axis, float angle)
{
Quaternion rot = Quaternion.AngleAxis(angle, axis);
transform.position = rot * (transform.position - pivotPoint) + pivotPoint;
transform.rotation = rot * transform.rotation;
}
/// <summary>
/// Rotates a transform around the given Pivot point by the given angle on the given axis.
/// </summary>
/// <param name="transform"></param>
/// <param name="pivotPoint"></param>
/// <param name="axis"></param>
/// <param name="angle"></param>
/// <returns></returns>
public static LocalToWorld RotateAround(LocalToWorld transform, float3 pivotPoint, float3 axis, float angle)
{
quaternion rot = quaternion.AxisAngle(axis, angle);
return new LocalToWorld
{
Value = new float4x4(
math.mul(rot, transform.Rotation),
math.mul(rot, (transform.Position - pivotPoint)) + pivotPoint)
};
}
//NOTE ObjectB has a float3 position and a quaternion rotation
float3 m_lastCalculatedForce = (ObjectB.position - ObjectB_oldPosition);
float forceMagnitude = Unity.Mathematics.math.length(p.m_lastCalculatedForce);
float3 unitForceVector = p.m_lastCalculatedForce / forceMagnitude;
float3 newVector = TransformsExtensions.RotateAround(unitForceVector, float3.zero, math.inverse(ObjectB.rotation));
Debug.Log(p.m_lastCalculatedForce + "::" + forceMagnitude + "\n" +
"::" + unitForceVector + ":::" + math.length(unitForceVector) + "\n"
+ newVector + ":::" + math.length(newVector));
p.m_lastCalculatedForce = newVector * forceMagnitude;
This works only on the Y axis. the X and X rotation axis somehow messes everything up.
Meaning i ObjectB is (90,0,0) and thie code runs then the force is in the wrong direction.
Any ideas?