Collision detection techniques in C ++

I am new to C ++ and I have been practicing collision in a small game program that does nothing and I just cannot get collision to the right

So I am using 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);

      

Although there are variables stored as

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 the 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 thought about things like

sprite->y = bushheight - 24;

      

but it doesn't work

Any suggestions?

0


a source to share


3 answers


I think you have the basic idea. Just check your work. Here's a simple version that 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");

}

      

+1


a source


I suggest making a function solely for the purpose of detecting box blocking. It might look like

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

      

in which you are checking if there is a collision between image 1 and image 2. Regarding the algorithm you are trying to use see this wikipedia for example the bounding box AABB



Especially this part:

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

+2


a source


First of all, I suppose you mean

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

second i dont know which platform you are using but often when the y coordinates are inverted. that is, y = 0 corresponds to the top of the screen. In this case, your comparisons may not work.

the third collision check can quickly get complicated when you add rotations and non-rectangular objects to it. You should consider using CGAL or another computational geometry algorithm library that can handle the intersection of polygons.

-1


a source







All Articles