Circle to Circle Collision of moving Sprites

Hey there, 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,
image

Attaching means, when both 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,
image

I cannot use PhysicsWorld or PhysicsBody, i am doing this all by calculations.
Anybody have done this … help me out.

1 Like

I’m not completely sure. But I think, it’s due to using Actions for movements. Try using game loop for movements.

For only 1 circle in motion, it’s working, probably, because you’re removing it from the scene, which is causing the Action attached to it also getting removed. So, Action is not creating problem. But with 2 circles, the circle which is not removed from scene might be causing issue due to Action.

Either try removing both circles and re-add into the scene at same positions. Or, use game loop. :slight_smile:

1 Like

this link may be help you . http://www.jeffreythompson.org/collision-detection/poly-line.php

1 Like

I’m guess your are right … maybe due to Action i’am couldn’t able to get perfect shooting circle position after collision.
But hey, you know its like multiple circles are moving already and i’am shooting circles from left and then adding those shooting circles to moving one at colliding point as i described in question.

Actually i can reduce collapsed area when sprites are in same axis( x-axis ), but when shooter hits sprite from upper or lower part, i couldn’t able to calculate collapsed are by means of y-axis which i also have to reduce … that’s where fun begins , cause i couldn’t find a way to calculate collapsed area according to y-axis.

could you help me on this … and yes i’am using game loop :slightly_smiling_face:

Blockquote

What is “collapsed area”? I couldn’t understand. Is it overlapped area?

If you’re able to find point of contact of 2 circles, remove both the circles from the scene and add a node with above 2 circles as child and calculate their location wrt parent. Also, the position of this parent node must be mean of positions of circles when they got in touch.

I mean you can try using “game loop” for movements of circles, like changing x and y, instead of Actions for movements.

I personally would have used Physics engine, because suppose when 2 circles are already in touch and 3rd circles comes and hit one of them, then ideally those 2 circles when rotate as per their center of mass, or atleast they should rotate with some force. And calculating motion of 2 circles due to 3rd circle would be complicated.

Hey, thanks … your help is really appreciated.
I have found another solution for this problem which considers angle between center of colliding circle and point of contact, by which i can easily calculated attaching circle point.

This is what i did,

float dx = circle1.x - circle2.x
float dy = circle1.y - circle2.y
float distance = Math.sqrt(dx * dx + dy * dy)

float overlappedArea = ( circle1.radius + circle2.radius ) - distance

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

// Point of Contact, this only works when both sprites have same radius
float contactPoint = Point( (circle1.x + circle2.x)*0.5f , (circle1.y + circle2.y)*0.5f )

// Calculate Angle Between Center and Contact point
float angle = atan2( circle2.x - contactPoint.x, circle2.y - contactPoint.y)

// creating of new sprite
After finding angle you can create new sprite on same angle at distance you wanted by means of sin/cos
}