Extracting Necessary data from Unity

Hello,

I’m working on a research project and got bugged in a corner, if anyone can give some guidelines it would be very appreciated. The values I’m trying to extract are in right hand system.

So lets say I have a Cube and there are forces applied to it (like water and buoyancy or gravity and
elasticity/resilience). I need to know what is the velocity, acceleration, angular velocity and angular acceleration and rotation matrix of the Cube. I accept 2 relative points World(W) and Body(B).
So if I got everything about Unity right. velocity is simple:
x’(B),y’(B),z’(B) - which is exactly rigidbody.velocity Vector3.
same goes for angular Velocity it would be equal to rigidbody.angularVelocity Vector3.

Next come the accelerations - since Unity does not provide a Vector for those (or does it?) I assumed I need to calculate them.
So a acceleration Vector would be a function derived from last object velocity:
Vector3 acceleration = (ownerRB.velocity - lastVelocity) / Time.fixedDeltaTime;
Same for angular acceleration
Vector3 angularAcceleration = (ownerRB.angularVelocity - lastAngularVelocity) / Time.fixedDeltaTime;

I’m assuming that speeds are m/s and radians/s while accelerations m/s2 and radians/s2
Please correct me if I’m wrong anywhere.

So now comes the biggest pinch I have :
I need a Matrix of directional cosines aka World to Body Rotation Matrix which is a 3x3 matrix.
What would be the most effective way to obtain this? I see that unity provides something like
rigidbody.transform.worldToLocalMatrix - however it seems it is not helpful.
So the next method I assume would be best is to calculate this from eulerAngles (which is a pain)

Assuming that Xb = Rw->b * Xw and Rw->b^T = Rw->b^-1 = Rb->w

So My Matrix would look like the below(? I’m absolutley not sure about it)
m00 = cos(z)*cos(x)+sin(z)*sin(y)*sin(x)
m01 = cos(x)*sin(z)*sin(y)-sin(x)*cos(z)
m02 = cos(y)*sin(z)
m10 = cos(y)*sin(x)
m11 = cos(x)*cos(y)
m12 = -sin(y)
m20 = sin(x)*cos(z)*sin(y)-sin(z)*cos(x)
m21 = sin(z)*sin(x)+cos(x)*cos(z)*sin(y)
m22 = cos(y)*cos(z)

If anyone could help with getting around those transformation this would be appreciated.

Assuming you want a matrix like worldToLocalRotateMatrix instead of the existing rotation+translation(+scale?) worldToLocalMatrix, I guess you could do Matrix4x4.TRS(Vector3.zero, transform.rotation, Vector3.one)?

1 Like

The thing is the deeper I go the less I’m certain of what I need. In hardware the documentation it is described as:

The rotation matrix or matrix of directional cosines used to transform a vector from World co-ordinates to a vector in Body co-ordinates by vector multiplication. The rotation matrix is used because it is axes convention
independent. The client software can use any system of re-orientation (eg. Euler or otherwise) and still the rotation matrix will be the same. The only condition is that the following expressions must hold true:
Xb = Rw->b * Xw and Rw->b^T = Rw->b^-1 = Rb->w

an example of this would be If an object is aligned with the world axis its rotation matrix would be:
1 0 0
0 1 0
0 0 1
Which is true for both my and yours attempts at the problem (thou they go a little sideways when the simulation starts)

While the graphical representation of the problem looks a little like this.
2617857--183735--upload_2016-5-1_20-57-52.png

Well… that does sound an awful lot like a world->local rotation matrix :).

Have you tried transposing the matrix? Unity matrices are in column major order, iirc academics use row major order often. If you’re passing it to an external program, this would be a thing. If you’re accessing the .m01 etc yourself later, not so much.