+ 3
Python collision
Does anyone know how to detect the collision for more than 2 objects? Because usually to detect collision, the distance between 2 objects need to be calculated. However, when there are more than 2 objects, how to test for the collision for all the objects together? Perhaps anyone has some good tips or idea to share it with me?
8 Answers
+ 5
Hi Ooi Zong Qi
I am not very good in python as opposed to JS but the general idea is the same, hence I will be pointing out the main steps
Create an array to store all the objects
Start a for loop to go through all the elements of the array
Inside that for loop, create another loop (nested loop!) which will also run through all the elements of the array
Inside that nested loop, check for collisions for the element which the first loop is at against all the elements in the second inner loop
That should loop through all the elements against themselves!!
Hope if works and if it doesn't don't hesitate to DM me for questions and Good luck on your coding adventure âđ©!
+ 5
hello Ooi Zong Qi , I really like the way how Vachila mentioned.
But just for more idea read this
https://stackoverflow.com/questions/46005749/detect-collision-of-more-than-3-gameobjects-at-the-same-time-in-unity
+ 4
Depends on what module you use in python ;pygame tkinter turtle pyqt etc...and the shape object area you want to check for collision for example rectangle shapes have x and y positions etc...
+ 3
Yes, Ooi Zong Qi, your distance formula checks with 2 x and 2 y coordinates right?
Make sure that the inner loop is checking the object the outside loop is currently at in it's loop cycle and the object the inner loop is looping at, perhaps by using 2 different indexers, perhaps something like this;
//outer loop
for( i = 0; i < objectsArray.length; --i){
//inner loop
for(j = 0; j < objectsArray.length; --j){
//this compares the objects I'm the outer loop to the ones in the inner loop
getDistance( objectsArray[ i ].x,
objectsArray[ i ].y, objectsArray[ j ].x, objectsArray[ j ].y
}
}
Hope you get the concept, it's all in JavaSricpt đ
+ 2
Vachila64â oh, that sounds so helpful. So I just have to create 2 loops and in the 2nd nested loop, I will have to put my distance formula in it?
+ 2
sorry, made a few typos, it's correct now!
+ 2
Mirielle really appreciated with your helps and comments. I think its my own problem, I feel rushed and I couldn't really calm down and intepret all the helps and comments that I have received. I guess I will just try my best to solve my issue.
Programming turns out to be harder than what I expect though...
+ 1
Thank you for those who tried to help me, especially Vachila64â. You guys are really awesome and I have finally figure it out. I will be sharing my codes here as a reference.
For example,
I have x and y coordinates of 3 things and I would like to detect the collision between all the objects, my algorithm in Python is as below:
xvalues = [1, 2, 3]
yvalues = [4, 5, 6]
for i in range(3):
for j in range(3):
if i==j:
pass
else:
distance = sqrt(((xvalues[i] - xvalues[j])**2) + ((yvalues[i] - yvalues[j])**2))