Collision dans sdl

riri785 Messages postés 15 Statut Membre -  
riri785 Messages postés 15 Statut Membre -
Bonjour a tous,

je débute dans la sdl et j'ai de gros (énorme)problème pour la collision, je voudrais savoir comment faire pour que quand zozor rencontre b il y aille une collision:

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *zozor = NULL , *b = NULL;
SDL_Rect positionZozor;
SDL_Rect positionb;
SDL_Event event;
int continuer = 1;

SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Gestion des évènements en SDL", NULL);

b = SDL_LoadBMP("icon.bmp");
zozor = SDL_LoadBMP("cb.bmp");
SDL_SetColorKey(zozor, SDL_SRCCOLORKEY, SDL_MapRGB(zozor->format, 0, 0, 255));

/* On centre Zozor à l'écran */
positionZozor.x = ecran->w / 2 - zozor->w / 2;
positionZozor.y = ecran->h / 2 - zozor->h / 2;

positionb.x = 0;
positionb.y = 0;
SDL_EnableKeyRepeat(10, 10);
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:

switch(event.key.keysym.sym)
{
case SDLK_UP: // Flèche haut
positionZozor.y--;
break;
case SDLK_DOWN: // Flèche bas
positionZozor.y++;
break;
case SDLK_RIGHT: // Flèche droite
positionZozor.x++;
break;
case SDLK_LEFT: // Flèche gauche
positionZozor.x--;
break;
}

break;
}

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255)); /* On efface l'écran */
SDL_BlitSurface(zozor, NULL, ecran, &positionZozor);
SDL_BlitSurface(b, NULL, ecran, &positionb);
SDL_Flip(ecran); /* On met à jour l'affichage */
}

SDL_FreeSurface(zozor);
SDL_Quit();

return EXIT_SUCCESS;
}

SVP répondez je suis vraiment a cour d'idée la...

5 réponses

  1. freesta Messages postés 644 Statut Membre 26
     
    déclare ta structure AABB avant le main

    a+

    N'attribuez jamais à la malveillance ce qui s'explique très bien par l'incompétence.Le logiciel, c'est comme le sexe, c'est meilleur quand c'est libre/gratuit
    1
  2. freesta Messages postés 644 Statut Membre 26
     
    ne saute pas des étape si tu poursuit les cours sur le site du zerro, il vont te montrer comment faire un mini jeux avec mario et la tu a tout dedans pour la gestion de colision
    0
  3. riri785 Messages postés 15 Statut Membre
     
    Oui c'est justement la que je ne comprend plus la collision.
    0
  4. riri785 Messages postés 15 Statut Membre
     
    bon j'ai un peut avancer depuis maintenant j'ai sa :

    #include <stdlib.h>
    #include <stdio.h>
    #include <SDL/SDL.h>

    #define true 1
    #define false 0

    int main(int argc, char *argv[])
    {{
    SDL_Surface *ecran = NULL , *box1 = NULL , *box2 = NULL;
    SDL_Rect positionZozor;
    SDL_Rect positionb;
    SDL_Event event;
    int continuer = 1;
    SDL_Rect AABB;

    SDL_Init(SDL_INIT_VIDEO);
    ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Gestion des évènements en SDL", NULL);

    box2 = SDL_LoadBMP("icon.bmp");
    box1 = SDL_LoadBMP("cb.bmp");
    SDL_SetColorKey(box1, SDL_SRCCOLORKEY, SDL_MapRGB(box1->format, 0, 0, 255));

    /* On centre Zozor à l'écran */
    positionZozor.x = ecran->w / 2 - box1->w / 2;
    positionZozor.y = ecran->h / 2 - box1->h / 2;

    positionb.x = 0;
    positionb.y = 0;
    SDL_EnableKeyRepeat(10, 10);

    struct AABB
    {
    int x;
    int y;
    int w;
    int h;
    };

    struct AABB box;

    box.x = 50;
    box.y = 500;
    box.w = 800;
    box.h = 101;

    while (continuer)
    {
    SDL_WaitEvent(&event);
    switch(event.type)
    {
    case SDL_QUIT:
    continuer = 0;
    break;
    case SDL_KEYDOWN:

    switch(event.key.keysym.sym)
    {
    case SDLK_UP: // Flèche haut
    positionZozor.y--;
    break;
    case SDLK_DOWN: // Flèche bas
    positionZozor.y++;
    break;
    case SDLK_RIGHT: // Flèche droite
    positionZozor.x++;
    break;
    case SDLK_LEFT: // Flèche gauche
    positionZozor.x--;
    break;
    }

    break;
    }

    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255)); /* On efface l'écran */
    SDL_BlitSurface(box1, NULL, ecran, &positionZozor);
    SDL_BlitSurface(box2, NULL, ecran, &positionb);
    SDL_Flip(ecran); /* On met à jour l'affichage */
    }

    SDL_FreeSurface(box1);
    SDL_Quit();

    return EXIT_SUCCESS;
    }

    return 0;}

    bool Collision(AABB box1,AABB box2)
    {
    if((box2.x >= box1.x + box1.w) // trop à droite
    || (box2.x + box2.w <= box1.x) // trop à gauche
    || (box2.y >= box1.y + box1.h) // trop en bas
    || (box2.y + box2.h <= box1.y)) // trop en haut
    return false;
    else
    return true;
    }

    mais il met comme problème :

    C:\Users\harry\Documents\testsdl - Copie\main.cpp|117|error: 'AABB' was not declared in this scope|
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. riri785 Messages postés 15 Statut Membre
     
    Un grand merci pour la réponse maintenant j'ai plus de message d'erreur mais... sa ne marche toujours pas, svp aider moi je n'arrive vraiment pas...

    voila se que j'ai:

    #include <stdlib.h>
    #include <stdio.h>
    #include <SDL/SDL.h>

    #define true 1
    #define false 0

    struct AABB
    {
    int x;
    int y;
    int w;
    int h;
    };
    int main(int argc, char *argv[])
    {{
    SDL_Surface *ecran = NULL , *box1 = NULL , *box2 = NULL;
    SDL_Rect positionZozor;
    SDL_Rect positionb;
    SDL_Event event;
    int continuer = 1;
    SDL_Rect AABB;

    SDL_Init(SDL_INIT_VIDEO);
    ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Gestion des évènements en SDL", NULL);

    box2 = SDL_LoadBMP("icon.bmp");
    box1 = SDL_LoadBMP("cb.bmp");
    SDL_SetColorKey(box1, SDL_SRCCOLORKEY, SDL_MapRGB(box1->format, 0, 0, 255));

    /* On centre Zozor à l'écran */
    positionZozor.x = ecran->w / 2 - box1->w / 2;
    positionZozor.y = ecran->h / 2 - box1->h / 2;

    positionb.x = 0;
    positionb.y = 0;
    SDL_EnableKeyRepeat(10, 10);

    struct AABB box;

    box.x = 50;
    box.y = 500;
    box.w = 800;
    box.h = 101;

    while (continuer)
    {
    SDL_WaitEvent(&event);
    switch(event.type)
    {
    case SDL_QUIT:
    continuer = 0;
    break;
    case SDL_KEYDOWN:

    switch(event.key.keysym.sym)
    {
    case SDLK_UP: // Flèche haut
    positionZozor.y--;
    break;
    case SDLK_DOWN: // Flèche bas
    positionZozor.y++;
    break;
    case SDLK_RIGHT: // Flèche droite
    positionZozor.x++;
    break;
    case SDLK_LEFT: // Flèche gauche
    positionZozor.x--;
    break;
    }

    break;
    }

    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255)); /* On efface l'écran */
    SDL_BlitSurface(box1, NULL, ecran, &positionZozor);
    SDL_BlitSurface(box2, NULL, ecran, &positionb);
    SDL_Flip(ecran); /* On met à jour l'affichage */
    }

    SDL_FreeSurface(box1);
    SDL_FreeSurface(box2);
    SDL_Quit();

    return EXIT_SUCCESS;
    }

    return 0;}

    bool Collision(AABB box1,AABB box2)
    {
    if((box2.x >= box1.x + box1.w) // trop à droite
    || (box2.x + box2.w <= box1.x) // trop à gauche
    || (box2.y >= box1.y + box1.h) // trop en bas
    || (box2.y + box2.h <= box1.y)) // trop en haut
    return false;
    else
    return true;
    }
    0