Calculate velocity on collider points that move with SetPosition()

So I have a polygon collider on a sprite shape, and that sprite shape has some of its nodes move back and forth in a coroutine (I am unsure how to add videos atm sorry). The polygon collider updates with the shape (a bit late, but I don’t think that’s the cause).

The issue is that I have a ball using unity physics (CircleCollider2D, Rigidbody2D) that falls onto the moving object, and am hoping that the movement of the object would add velocity to the ball, but the ball acts as through the shape is an unmoving wall and receives no velocity from colliding with it.

I am aware this is because the shape is moving its spline points with SetPosition() which only causes the polygon collider to update its position without keeping any speed from that translation, so no velocity can be translated into collisions with other objects. Just in case you need to know, the shape is using Rigidbody2D (static, others didn’t seem to work either) and PolygonCollider2D.

This is to ask: how do I add the correct velocity to the ball, regardless of which part of the collider it would hit? for example, if the top right corner of a square moved upwards, one ball was on top of it on the right and one on the left, despite touching the same collider, the ball on the right would gain more velocity. And also move slightly left, if I understand physics correctly. How do I calculate this? The shape is moving using SpriteShapeController.SetPosition(). Does anyone have any ideas? I’m kind of at a loss for how to go about this honestly…

Just thinking out loud here.

Physics objects have a center of mass, a velocity and an angular velocity. The collision has a contact point, which you can convert to an offset relative to the center of mass. If you evaluate that offset, and that offset plus one step of angular velocity, you should be able to convert spin into a reasonable approximation of that contact point’s world velocity.

I think I have kind of an idea what you mean here, but I have no idea what one step of angular velocity is. Or why the objects angular velocity would be indicative of the world velocity of that point, as collisions with zero angular velocity involved is entirely possible. Also unsure what spin is, if not angular velocity.

Doing this isn’t exactly going to be great for performance but doing it a little won’t matter too much. Colliders are not meant to be animated so all the shapes inside the PolygonCollider2D are recreated each time you change it along with the fact that it has to decompose the outline into real physics shapes. Also, ensure that you give this its own Rigidbody2D with a body-type of Kinematic if you’re moving it or Static if you’re not.

Because you recreate them, the outline isn’t moving, you’re just recreating new shapes. Don’t confuse what is “logically” happening with what is actually happening. When you recreate these shapes, they’ll simply cause overlaps to things close or in contact with them and the solver will simply moving them out of overlap; this doesn’t affect velocity as it’s a corrective action, not part of physics dynamics i.e. movement/rotation.

The sprite-shape updates the PolygonCollider2D but that change, the same as if you did it, is instant so I don’t know what you mean by a “bit late”.

I would suggest that before you move the sprite-shape, you ask what’s in contact with it. That way, when you move it you can calculate what that effective move is in terms of a pseudo velocity. You then have a velocity that you can apply to things that were in contact with it. You should only apply this velocity however is the movement is in the direction of the contact which is as simple as doing a dot-product.