Tutorial :Collision-Detection methods in C++



Question:

I am new to c++ and I have been practicing collision in a small game program that does nothing and I just can't get the collision right

So I use images loaded into variables

background = oslLoadImageFile("background.png", OSL_IN_RAM, OSL_PF_5551);  sprite = oslLoadImageFile("sprite.png", OSL_IN_RAM, OSL_PF_5551);  bush = oslLoadImageFile("bush.png", OSL_IN_RAM, OSL_PF_5551);  

While there are variables stored like

sprite->x = 3;    if ( (sprite->x + spritewidth > bush->x) && (sprite->x < bush->x + bushwidth) && (sprite->y + spriteheight > bush->y) && (sprite->y < bush->y + bushheight) )   {           bushcol = 1;                 }  else  {          bushcol = 0;        }  

So when i press a button

if (osl_keys->held.down)  {  if (bushcol == 1)   {  sprite->y = bush->y + 38;  }  else  {  sprite->y += 3;  }  }  if (osl_keys->held.up)  {  if (bushcol == 1)   {  sprite->y = bush->y - 23;  }  else  {   sprite->y -= 3;  }  }  if (osl_keys->held.right)  {  if (bushcol == 1)   {  sprite->x = bush->x - 28;  }  else  {   sprite->x += 3;  }  }  if (osl_keys->held.left)  {  if (bushcol == 1)   {  sprite->x = bush->x + 28;  }  else  {   sprite->x -= 3;  }  }  

i was thinking of things like

sprite->y = bushheight - 24;  

but it doesnt work

Any suggestions?


Solution:1

I think you have the basic idea. Just check your work. Here is a simple version which compiles:

#import <stdlib.h>    typedef struct {      // I'm going to say x, y, is in the center      int x;      int y;      int width;      int height;  } Rect;    Rect newRect(int x, int y, int w, int h) {      Rect r = {x, y, w, h};      return r;  }    int rectsCollide(Rect *r1, Rect *r2) {      if (r1->x + r1->width/2 < r2->x - r2->width/2) return 0;      if (r1->x - r1->width/2 > r2->x + r2->width/2) return 0;      if (r1->y + r1->height/2 < r2->y - r2->height/2) return 0;      if (r1->y - r1->height/2 > r2->y + r2->height/2) return 0;      return 1;  }    int main() {      Rect r1 = newRect(100,200,40,40);      Rect r2 = newRect(110,210,40,40);      Rect r3 = newRect(150,250,40,40);        if (rectsCollide(&r1, &r2))          printf("r1 collides with r2\n");      else          printf("r1 doesnt collide with r2\n");        if (rectsCollide(&r1, &r3))          printf("r1 collides with r3\n");      else          printf("r1 doesnt collide with r3\n");    }  


Solution:2

I'd suggest making a function solely for the purpose of bounding box colision detection. It could look like

IsColiding(oslImage item1, oslImage item2)   {     /* Perform check */  }   

in which you perform the check if there is a collision between image 1 and image 2. As for the algorithm you're trying to use check out this wikipedia for example AABB bounding box

Especially this part:

In the case of an AABB, this tests becomes a simple set of overlap tests in terms of the unit axes. For an AABB defined by M,N against one defined by O,P they do not intersect if (Mx>Px) or (Ox>Nx) or (My>Py) or (Oy>Ny) or (Mz>Pz) or (Oz>Nz).


Solution:3

First of all i suppose you mean

sprite->y = bush->**y** - 3;

second i dont know what platform you are using, but often times the y-coordinates are inverted. i.e., y=0 corresponds to top of the screen. In that case your comparisons might not work.

third collision checking can quickly become complicated when you add rotation and non-rectangular objects to it. You should consider using CGAL or some other computational geometry algorithms library, which can handle polygon intersection.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »