Search Unity

CircleCast vs Physics Simulation corner vertex normal discrepancy

Discussion in '2D' started by doganaktay, Aug 27, 2020.

  1. doganaktay

    doganaktay

    Joined:
    Apr 1, 2020
    Posts:
    6
    Hey all,

    EDIT 2: I've just (living under a rock) found out about simulating separate physics scenes and that seems like the best choice here for precise simulation of relevant trajectory. So if you think that this is indeed the best way for me to move forward, then I would appreciate it if you could provide some guidance about porting over large number of colliders to the second scene at runtime as my scenes are procedurally generated and thus can't be cloned prior to runtime.

    I'd like to ask those who (always) know more than me a basic-ish question regarding how to handle a certain case.

    It breaks down like this:
    - I have a 2D scene, a maze if you will
    - The maze is procedurally generated and the walls are rectangles with box collider 2Ds placed on them
    - The player can shoot a projectile in any direction so long as there is enough distance between him and the closest obstacle to fit a circle projectile of the appropriate width
    - each different projectile (scriptable objects) has a variable width and a max bounce count but they are all circles with circle colliders on them
    - I am using circlecasts w a radius of projectileWidth / 2 to generate contact points (upto allowed max count) on the walls. I am reflecting the circlecast based on the incoming vector and the surface normal. Each new point of circlecast collision is offset a tiny amount in the direction of the contact surface normal before casting in the next direction in order to not re-register the collision with said surface. Then a procedural mesh is generated for this trajectory
    - The player will then fire the projectile (a dynamic rigidbody2D, currently with no angular force or angular drag), which I would like to exactly follow the circlecast trajectory that is shown. This does not need to account for unforeseen collisions with other objects in the way, so it is alright if another object got in the projectile's path so it could not complete its initial trajectory.

    Now, onto the edge case and my conundrum:
    - The predicted trajectory and the actual path that the circular projectile take match up exactly except when the collision point is the corner of a box collider on the wall. I assume that the corner vertex collision normal is an interpolation between the two adjacent side normals. And so i'm guessing that the difference in the normals received by the two methods (both the simulated collisions w circlecast and the actual projectile) are not the same due to floating point precision?
    - So, when the contact point is a corner, the path that the precalculated trajectory takes and the path that the projectile take often diverge, and I know that it is because they receive different normals from the corner that is hit (checked with Debug rays).

    EDIT: actually, upon closer observation, the slight drift also exists on the regular wall collisions, just to a much smaller degree

    So then my question is:
    - Is there an intelligent way to resolve this without taking the dynamic rigidbody2D out of the loop, so that the launched projectile follows the exact precalculated path (once again barring any outside influence like enemies or obstacles of other kinds)?


    I can think of a few work arounds, but they all inevitably either lead to more calculations, or ditching the dynamic rigidbody for a kinematic one and having it follow the preset path. I have also tried to fix this with rounded edges on the box collider and got the same result (though maybe that is still the way to go and I did not set it up correctly)

    An elucidation of why this is the case with the corner vertex normals not matching up on a circlecast and a projectile with identical directions, and hopefully a helping hand in regards to resolving my issue would be much appreciated.

    Cheers,
    Dogan
     
    Last edited: Aug 28, 2020