# Circle to Circle Collision of moving Sprites

i needed help attaching circle1 Sprite to circle2 Sprite on collision onto the same point where they are getting collided, and both circles are moving with Action. Like this,

Attaching means, when circle1 and circle2 sprites get collided, i am removing circle1 Sprite from scene and creating new sprite at that same position of circle1.

I have implemented following code for collision .... float dx = circle1.x - circle2.x; float dy = circle1.y - circle2.y; float distance = Math.sqrt(dx * dx + dy * dy);

if (distance < circle1.radius + circle2.radius) { // collision detected // removing older sprite // creating of new sprite }

Its working perfectly when one circle sprite in motion and another is static.

But when both are in motion, at some points ( collision point like bottom-left/ up-right of circle2 ) its get attached little over circle2 sprite and it happens at random times so i couldn't able to detect the error.

I want to attach new Sprite at exact point where collision occurred like this,

Anybody have done it before..

The problem you're encountering is common during collision resolution in physics systems.

You'll need to do some collision penetration resolution before you start removing/creating sprites.

This usually means finding how much the two objects overlap:
penetration = (player.position - enemy.position).length() + abs(player.radius - enemy.radius)
Then move them away from each other:
player.position += (player.position - enemy.position).unit() * penetration * 0.5 enemy.position -= (player.position - enemy.position).unit() * penetration * 0.5

I assume that player and enemy's position is a Vector3 with length and unit functions. You may have to adjust who is += and -=. Also, you have an abs function somewhere.

The 0.5 will move both objects away 50% of the distance, this is usually adjusted to reflect the relative weight of each object (eg. player.weight / (player.weight + enemy.weight).

• I really appreciate your answer, but the game i developing do not allows me to move second sprite( enemy circle ), somehow i have to adjust player shooting sprite only. In game , enemy sprites are moving randomly and player sprite circle is shooting from left to right on same y-axis. I can calculate collapsed area in respect of x-axis position, but when sprites – Aniruddha Ratnaparkhi Aug 14 '18 at 9:00
• i wanted to calculate collapsed area according to y-axis also .. to place/attach shooter sprite at collision point – Aniruddha Ratnaparkhi Aug 14 '18 at 9:24