• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Collision Detection

## Recommended Posts

First off, I'd like to appologize for my annoying previous posts, I know some people have gotten a little frustrated with me. But, I'd like to have a little assistance for a good collision detection algorithim. I made one myself, but it sure does look like that the ships are jumping around. I have already programmed AI for my ships. there are a few ways I can think of improving. The current way I have my AI, I have the ships move, then correct later. Now, I think that I could make a temporary yInc and xInc, and set those to -5, 5 etc, then at the end of the method, move them, and if they turn out to collide with any other ships, undo it. but this is the current code I am using.
  void CheckForEnemyContact() { for (int i=0; i<5; i++) { for(int i2=0;i2<5;i2++) { if(i!=i2) { if(contact(&enemies[i].ship, &enemies[i2].ship)) { if(enemies[i].ship.x>enemies[i2].ship.x && enemies[i].ship.x < enemies[i2].ship.x+enemies[i2].ship.width) enemies[i].ship.x=enemies[i2].ship.x+enemies[i].ship.width; if(enemies[i].ship.y>enemies[i2].ship.y && enemies[i].ship.y < enemies[i2].ship.y+enemies[i2].ship.height) enemies[i].ship.y=enemies[i2].ship.y-enemies[i].ship.height;; } } } } } 
Sorry about the messiness of the post Sponge Factory --Muzlack Edited by - Muzlack on February 18, 2002 8:21:27 PM

##### Share on other sites
First step:

for (int i=0; i<5; i++)
{
for(int i2=i+1;i2<5;i2++)
{

That means you don''t have to check to see is i=i2.

As for the rest of it there''s a few good articles around, I suggets you search on google.

- seb

##### Share on other sites
quote:
Original post by BSXrider
First step:

for (int i=0; i<5; i++)
{
for(int i2=i+1;i2<5;i2++)
{

you mean:
for( int i = 0 ; .... {
for( int i2 = 0 ; i2 < i ... {
}
for( i2 = i+1 ; i2 < 5 ...

cheers

(: I am what I am, more or less

##### Share on other sites
Do I?

I think they both do exactly the same... though it''s beyond me why you have a third for statement tacked on the end?

- seb

##### Share on other sites
The way it works right now, they jump around alot when they come in contact with each other, is that yinc and xinc idea good? I think that would work. I''ll change my code to your for statement. Took me a while to understand why that would work, but I understand now. So any suggestions on how to undo these moves? is the yinc and xinc idea good so that they''ll move at the end of the ai function rather than in the middle so the undoing would be easy?
Thanks again!
[/source]

Sponge Factory
--Muzlack

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627682
• Total Posts
2978614

• 13
• 12
• 10
• 12
• 22