Hello processing community. I want to make a mini game where you control a ellipse and have to avoid other ellipse. I first wanted the “opponents” to repel each other. For this I initialized 10 ellipses in an array. But when I try to use the dist function to see if the radius overlaps, it only shows that the dist function doesn’t expect a float parameters. Are there other solutions or can you convert your array in some way so that it is still possible to use dist.
The problem with your code is that you are trying to pass in an array as a parameter to the “dist()” function, but the “dist()” function expects individual x and y coordinates as parameters. To fix this issue, you can simply use the values from the x and y arrays in the “dist()” function instead of passing in the entire array.
For example, to calculate the distance between the first and second ellipse in your array, you can use the following code:
if(dist(x[0], y[0], x[1], y[1]) < r)
You can then use a loop to check the distance between each ellipse in the array and apply the necessary changes to the x and y speeds if the distance is less than the radius.
Note: part of this message was generated with ChatGPT
Thanks, I’m a lot closer now. The problem that now exists is that if, for example, I let the 2 ellipse bounce off the rest, it is completely frozen. But the others are completely normal and bounce off the 2 ellipse. But if I now want to let all of them bounce, of one another if(dist(x[j], y[j], x[j], y[j]) < r), the problem with the freeze applys onto all ellipses.
This code checks for a collision between each pair of balls and reverses their speed if they are colliding. It also ensures that a ball does not collide with itself by checking if i and j are the same. You can add this code inside your gegner() function.
A more object oriented approach would be to create a Ball class with a method that takes an array of Balls and checks for collisions against all of them.
Note: part of this message was generated with ChatGPT
I just realised that there is an algorithmic logic error in the double loop code presented by @sableRalph
Consider a system with 4 balls this table identifies the collision tests
+ = collision test
- = no collision test
i =
0 1 2 3
0 - + + +
j 1 + - + +
= 2 + + - +
3 + + + -
Notice that for any pairs of balls where i != j two tests are performed and the speed will be multiplied by -1 twice, in other words multiplied by 1 so no change of direction
This type of problem is well suited to OO programming but this is not a good idea because
you end up sending the same array to every ball thus having the same problem that I have just described.
Personally I would perform the collision detection outside the ball class (in the main sketch or another class used to manage the application.