Is there different ways of detecting collision in 2d games?

Currently I use x,y type of colision detection egif(x_pos > otherxpos)

{

// do this

}

etc...

is there any other ways of detecting collision like intersecting or something. Help would be appreciated as my current collision detection contains a lot of if() functions.

Thanks

[435 byte] By [Chris1234554a] at [2007-11-26 21:38:34]
# 1

Not really, but you can make a method to do it for you. Here's a suggestion, make a Collidable interface like this:

public interface Collidable

{

public boolean collidesWith(Collidable c);

public Shape getBounds();

}

Then you could have each object that should collide with something else implement the Collidable interface.

public class Enemy implements Collidable

{

//...

public boolean collidesWith(Collidable c)

{

//your implementation here

}

public Shape getBounds()

{

//your implementation here

}

}

Does that make sense?

CaptainMorgan08a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 2
Not really. What does the get bounds do?
Chris1234554a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 3
> Not really. What does the get bounds do?It returns a Shape object, like a Rectangle or Ellipse.
CaptainMorgan08a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 4
my objects have xpos and y pos that is in the top left of the picture representing a player. So it is automatically a square around it. Will this work?
Chris1234554a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 5

So your getBounds() method could look like this:

public Shape getBounds()

{

return new Rectangle(xPos, yPos, width, height);

}

CaptainMorgan08a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 6
What goes in both of the //your implementation herei dont know which one does which.
Chris1234554a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 7
There are two ways of detecting collision. One is to use geometric objects; the other is to look for overlapping pixels. The latter is generally slower but IMO gives a better game experience.
YAT_Archivista at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 8

> is there any other ways of detecting collision like

> intersecting or something. Help would be appreciated

> as my current collision detection contains a lot of

> if() functions.

well most games have a lot of if's

in any case, your collision algorithm is only 4 of them

CarrieHunta at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 9
Isn't it 8 because you have to do if(x_pos > npc) and then if(x_pos + pic_width > npc)
Chris1234554a at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 10

No. pic_width should be non-negative, so if (x_pos>npc) then you already know that (x_pos+pic_width>npc).

If you have two intervals [min1,max1] and [min2,max2] then they overlap unless the first is entirely before the second (max1<min2) or the second is entirely before the first (max2><min1).>

YAT_Archivista at 2007-7-10 3:21:40 > top of Java-index,Other Topics,Java Game Development...
# 11

> No. pic_width should be non-negative, so if

> (x_pos>npc) then you already know that

> (x_pos+pic_width>npc).

I didn't explain well enough because I don't think you understood me.

I meant

if(x_pos < npcx + npcwidth && x_pos > npcx || x_pos + playerwidth > npc && x_pos + playerwidth < npcx + npcwidth)

{

gameover();

}

Hope you understand it a bit more. Using 4 ifs for detection will only base it around the top left of a picture. So what happens if the object hits the top right of the picture. Thats what the part after the || detects.

This code just detects the top of the players pic being hit. It is part of my asteroids type game.

Am I right or am I just being complicated lol.

Chris1234554a at 2007-7-10 3:21:41 > top of Java-index,Other Topics,Java Game Development...
# 12

Your two intervals are [x_pos,x_pos+playerwidth] and [npcx,npcx+npcwidth]. So the collision test is if (!(x_pos+playerwidth<=npcx) && !(npcx+npcwidth<=x_pos)) gameover();

where the <= is because you seem, for some reason, to want to use exclusive bounds. You can simplify slightly to if ((x_pos+playerwidth>npcx) && (npcx+npcwidth>x_pos)) gameover();

YAT_Archivista at 2007-7-10 3:21:41 > top of Java-index,Other Topics,Java Game Development...