Is there a reason why you’re not letting the physics engine bounce the ball around?. If you add a physics material to the ball and set the material’s friction to 0 and the bounce to 1 then the ball will indefinitely bounce around on its own.
Yes, I know… the problem is that I do not want that the bounces against the bricks corners behave realistic because sometimes the angles are too small and the ball ends bouncing horizontally after some time… so I decided to control how the bounce will behave when a collision is detected.
Doing some more tests I have found something even weirder because I cannot see any contact between the colliders here but OnCollisionEnter2D was triggered.
When you get a collision callback on a Dynamic Rigidbody2D, it’s to tell you that it’s already happened including a collision response. It’s not a callback to say a collision happened, it did nothing and it should be at the collision point.
If you want to detect collisions and perform your own responses then that’s what a Kinematic body-type is for. A Dynamic body-type is when you want a collisions response.
So use a Kinematic body and use queries to detect collisions; plenty of those available including the ability to cast the whole Rigidbody2D or a specific collider in any direction.
If you don’t hit then you can move. If you do hit then you can move to the collision point and adjust your velocity accordingly. This is Kinematic motion.
It’s because you’re using the Continuous collision detection mode which is more speculative and reliable but can result in early collisions. If you want the ball to only reflect after it has visibly hit a block then you’ll need to use the Discrete collision detection mode, but this mode can sometimes result in small rapid objects passing through other objects undetected.
2D physics doesn’t have speculative collision detection. Continuous is exactly that, it’ll find the exact point of collision. It’s not related to the above.
I just did a little test and when using Continuous collision detection it does look like the ball is deflected before it actually hits a wall but I suspect this could be a result of the interpolation as it appears to hit the wall when interpolation is disabled… And reducing Time.scale makes the early deflection very obvious.
Interpolation is nothing to do with collision detection, it’s not even related to physics as it’s pure Transform/Visuals only outside of the simulation step. The body is always at the same position, with interpolation the Transform will be moving from the old to the current body position.
Continuous will just make sure that the contact is exactly where you’d expect it.
Mathematically/physically the ball does hit the wall but visually it often doesn’t appear this way because of the interpolation smoothing out the movement.
I’m really not sure why you’re trying to convince me of how this works, I wrote the code. TBH I don’t really see what it has to do with the OPs question.
The physics stages are to:
Calculate Contacts
Solve everything including Contacts
Integrate Position/Velocity
This means that after after integration, it might be visually overlapping/in-contact. The contact for that hasn’t been calculated yet so during the next simulation step the contact is found, solved and a callback happens. Contacts are up-front. They are not calculated at the start and the end.
As I said above, the motion here should be controlled with a Kinematic body and queries because from what I can see, no collision response is required.