How can I make a new ball from colliding?

In the example of “bouncy bubbles” that collide with each other, I would like to program a new ball to be created at that point when two balls collide. What should I do?

bouncing bubbles -> https://processing.org/examples/bouncybubbles.html

You need to use some math. Collisions between 2 points of the same size happen at the distance between two points/2 therefore if you know where a point is you can simply add that last calculation to one of the points, then you just call create new point and add it to the points array.

If they aren’t the same size than it will be (radius of point 1 + radius of point 2)/2

I know that, but I don’t know where to add the code ‘balls.add();’ Should I put it in the collide function in the ball class? I really don’t know what to do.

2020년 8월 5일 (수) 오후 7:46, paul goux via Processing Foundation <processingfoundation1@discoursemail.com>님이 작성:

Doesn’t necessarily matter where you add it so long as it allows proper timing, ie should be done before the end of draw, and you should probably not do it after all collisions have been checked because your new points might have caused collisions which now won’t be checked until they are initialised and therefore they will be running one frame behind.

Don’t have the code to hand ATM but if you can locate the collide code I would probably put it there.

How can I write the code when I want to add more class in the class function code?

2020년 8월 5일 (수) 오후 8:53, paul goux via Processing Foundation <processingfoundation1@discoursemail.com>님이 작성:

Don’t have the example to hand ATM but there should be a points array in the sketch which keeps track of the points, after checking collisions using the function from the class just add the relevant point

1 Like