Jump to content
  • Advertisement
Sign in to follow this  
fguihen

how to test each charachter/object against eachother for colisions?

This topic is 4918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i was trying to create my collision detection within my Charachter class, but im told that i should have a seperate class for it . is this true? also, if i have an array of charachters, when im checking them against eachother , how would i go about it? i was doing it like this:
foreach(Person p in Persons)
{
if(this.xPos < p.xPos)...
}
but this code has to be in the charachter class to work as it contains the "this" keyword. but i was having problems as each time i compared a person, i wasnt sure if i was comparing it against itself, as it was contained in the array passed to it, if that makes sense. any help would be great.

Share this post


Link to post
Share on other sites
Advertisement
What language is that? C#? The code looks fine, although you may want to watch out for the situation where p == this.

I don't see why you can't keep it in your Character class. One problem is that you potentially end up checking each pair of characters twice, but if you only do it when a character moves it probably isn't much of an issue.

Share this post


Link to post
Share on other sites
> i was trying to create my collision detection within my Charachter class, but
> im told that i should have a seperate class for it . is this true?
Not really.

> also, if i have an array of charachters, when im checking them against
> eachother , how would i go about it? i was doing it like this:
GDN has an extensive array of tutorials on the subject, that are much better than anything I can say. =)

> but this code has to be in the charachter class to work as it contains the
> "this" keyword.
Well, you could probably change it to not include the this keyword, right?

> but i was having problems as each time i compared a person, i
> wasnt sure if i was comparing it against itself, as it was contained in the
> array passed to it, if that makes sense. any help would be great.
You need to check if it's the person itself. Since if()s are relatively inexpensive, you'd do something like:

for ( person = 0; person < count ( array ); person++ ) {
for ( other_person = 0; other_person < count ( array ); other_person++ ) {
if ( person != other_person ) {
(Do collision detection here.)
}
}
}

Yes, I know there's a better way of doing it, but that's a lot simpler.

.bas

Share this post


Link to post
Share on other sites
Although there's nothing technically wrong with putting all of the collision detection inside the personclass for the algorithm you're using, it's probably going to be a lot easier to do some of it outside the class for even slightly more efficient algorithms. The algorithm you're currently using both checks each person against itself (there is no point to this and may very well cause many bugs) and actually performs each detection twice (waisted processing power).

Here's what I'd do:


for(int i = 0; i < persons.Length; i++)
{
for(int j = i + 1; j < persons.Length; j++)
{
persons.HandlePossibleCollision(person[j]);
}
}



The HandlePossibleCollisions checks to see if its person collided with the passed person, and if so, handles the collision.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!