I was looking to port my 2023.1 project to the more stable 2022.3 LTS, but in the process I came to realize that 2022.3 doesn’t have rb2D.localToWorldMatrix which I use extensively in my project. I can obviously replace this by creating my own matrix using the rb2d position and rotation, but after profiling it’s still 30% slower than rb2D.localToWorldMatrix even after optimizing the matrix generation the most I possibly can. My test took into account any caching that could be occurring, so the difference isn’t because of that. I can’t figure out why the built-in matrix is so much faster than creating my own. This is how I am making the matrix
public static Matrix4x4 CreateMatrix(Vector2 position, float angle)
{
float rad = angle * Deg2Rad;
float cos = (float)Math.Cos(rad);
float sin = (float)Math.Sin(rad);
return new Matrix4x4(
new Vector4(cos, sin, 0, 0),
new Vector4(-sin, cos, 0, 0),
new Vector4(0, 0, 1, 0),
new Vector4(position.x, position.y, 0, 1)
);
}
Matrix4x4 builtin = rb2D.localToWorldMatrix; //30% faster than mine
Matrix4x4 mine = CreateMatrix(rb2D.position, rb2D.rotation);
I’m assuming it’s not possible to port it, but I figured I’d ask.
@MelvMay Sorry to tag you but I figured you’d be the one person who knows the exact answer to this.
Thanks!