@MelvMay
Though actually while I have you, I was curious if you had any insight on this even though itās not technically related to 2D Physics.
I made this thread because Iām currently not using rigidbodies but am looking to switch to it for the various benefits including the built-in interpolation. Iām currently using transform.position for movement and Physics2D.SyncTransforms() after each of my āstepsā in FixedUpdate so the colliders sync with the transform changes so the subsequent collision raycasts can properly detect the newly positioned colliders of each step. (And for visual interpolation I have a custom transform interpolation script that lerps transform positions in the Updates between fixedupdates)
But one issue that prevents me from switching to using rigidbody2d.position is that Iām currently using a set of child transforms in order to use their positions as the start positions and end positions of Physics2D.Linecast(start, end)
since child transforms automatically move/rotate/scale properly in relation to the parent object that is moved by transform.position += . The problem is if I switch to instead use rigidbody2d.position +=, am I right to believe that that is no longer possible unless I continue to still move the transforms alongside it? As illustrated here:
Given the number of positions I need to track for my linecastsā start and end positions, manually calculating each new start and end position for each linecast for each substep the object gets moved in FixedUpdate has in my experience been significantly worse performance-wise compared to being able to simply use the child transforms (as shown above) since they automatically internally update any position/rotation changes from the parent transform they inherit from.
The closest I came to in terms of matching that performance was by using the transformās transformation matrix myself, but even that was worse than whatever happens internally to child transforms. But since using rigidbody2d.position doesnāt also instantly move the transform, the transformās transformation matrix would be unusable anyway to my understanding.
This is the only idea I have for that. Where after every rigidbody2d.position move Iād run a script like thisās UpdateSensors() to update those sensor positions
public class SensorTest : MonoBehaviour
{
private Vector3 _center = new Vector3(0, 0);
public Vector3 Center { get; private set; }
private Vector3 _bottomLeft = new Vector3(-1, -1);
public Vector3 BottomLeft { get; private set; }
private Vector3 _bottomRight = new Vector3(1, -1);
public Vector3 BottomRight { get; private set; }
private Vector3 _topLeft = new Vector3(-1, 1);
public Vector3 TopLeft { get; private set; }
private Vector3 _topRight = new Vector3(1, 1);
public Vector3 TopRight { get; private set; }
public void UpdateSensors(Rigidbody2D rb)
{
Quaternion rotation = Quaternion.Euler(0, 0, rb.rotation);
Vector3 position = rb.position;
Matrix4x4 matrix = Matrix4x4.TRS(position, rotation, Vector3.one); //not sure how I'd properly do scale with a rigidbody2d instead of a transform
Center = matrix.MultiplyPoint3x4(_center);
BottomLeft = matrix.MultiplyPoint3x4(_bottomLeft);
BottomRight = matrix.MultiplyPoint3x4(_bottomRight);
TopLeft = matrix.MultiplyPoint3x4(_topLeft);
TopRight = matrix.MultiplyPoint3x4(_topRight);
}
}
Sorry if that was hard to follow, but do you have any ideas for that?
Actually now after doing tests, using rigidbody + updating the raycast sensor positions with the Matrix4x4.TRS
using each new rigidbody.position
and Quaternion.Euler(0, 0, rigidbody.rotation)
now seems to be much faster unless I set up the test wrong.
I was originally doing the test with 500 instantiations of either the rigidbody.position script or transform.position script, and had them all spawn on the same position while I moved around. And the rigidbody version hung from the profiler showing Physics2D.Simulate>Physics2D.FindNewContacts despite all my collisions in the collision matrix being disabled which I thought was odd. But after staggering them on spawn so theyāre not on top of each other, the rigidbody version is much faster. (Though Iām still not sure why them overlapping would call Physics2D.FindNewContacts if theyāre not supposed to be able to detect one another anyway with the each checkbox in the collision matrix being disabled)