- Home /
Get point on surface of Collider2D from inside collider.
Hey there,
I'm trying to do some 2D Verlet Integration physics. It's mostly working, but one hurdle that's popped up is trying to merge it with existing physics objects. The effect I'm going for is to treat Unity's colliders as static geometry that my Verlet objects are not allowed to intersect. The trouble is, to do this I need to construct a constraint for how to resolve the case where a Verlet node IS inside a collider, and how to move it out. The simplest and most effective result would be "move the node to the nearest point on the surface of the Collider". Sounds simple enough, and at first glance it seems like Collider2D.ClosestPoint
would do the job.
However, with ClosestPoint
, if the point specified is already inside the collider, that point is just returned without being changed. This of course is completely useless for this use case.
I've tried a few methods to resolve this, such as using an iterator to find a point near to the surface, but outside the collider, and finding the closest point to that, but it's somewhat unstable. I've also tried constructing my own function to find this point, but this is quite slow, and complicated to build.
I'm wondering if there's any sensible way to achieve this. Does anyone have any ideas? Thanks!
Answer by lgarczyn · Dec 04, 2019 at 07:00 PM
I think that if you use Collider.Raycast
backward (not Physics.Raycast) so that the ray passes through both the center of mass and your point from the outside of the collider, you might get an approximation of your nearest point. However I believe the math for the nearest point depends on the collider type, and can only be generalized through some algorythm slowly approaching the target value.
Of course it would be better to avoid such problem by using raycasts to prevent a collision from happened in the first place.
Unfortunately, this doesn't work. Collider.Raycast
may do this, but Collider2D.Raycast
behaves differently, and acts as a ray from the centre of the collider, ignoring the collider provided.
Also, since this is a Verlet simulation, "prevent collisions" isn't an appropriate method, since that's not how Verlet simulations work.
A raycast from inside the collider will always ignore it, that's why I told you to invert it.
Let A be your center of mass, B the point you wish to move.
Let A + (B-A) * 100
be the start of your raycast, and (A-B)
the direction.
The former can also be written Vector2.LerpUnclamped(A, B, 100)
That won't work either. Collider2D.Raycast
doesn't allow you to set the position of the start of the raycast - it always goes from the centre of the collider. There is no way, as far as I know, to perform a raycast against a specific collider when using 2D physics.
Your answer
Follow this Question
Related Questions
Trigger is not executed 1 Answer
Inexact rebound when colliding into corner of two walls. 0 Answers
What exactly is RelativeVelocity? 1 Answer
Workarounds for Higher Control When Using Physics2D? 0 Answers