IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Discussion :

Gestion de collisions

  1. #1
    Nouveau membre du Club
    Femme Profil pro
    Développeur Web
    Inscrit en
    Novembre 2011
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2011
    Messages : 36
    Points : 30
    Points
    30
    Par défaut Gestion de collisions
    Bonjour,

    Alors voilà, je suis dans la création d'un jeu et je dois faire la collision entre une flèche (image rectangulaire) et une bulle (rectangle qu'on rechange en cercle). Voici la classe bulle:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    #include "bubble.h"
     
    Bubble::Bubble(QWidget *parent, int type,int posX,bool droite,int posY): QLabel(parent)
    {
        this->parent = parent;
        initialiser(type,posX,droite,posY);
        versBas=true;
        ral=true;
    }
     
    void Bubble::initialiser(int type,int posX,bool droite,int posY)
    {
        /*
        string image="../bulle"+type+".gif";
        m_imgBubble = new QPixmap(image);
        setPixmap(*m_imgBubble);
        */
        switch(type)
        {
            case 11:m_imgBubble = new QPixmap("../bulle11.gif");
                    setPixmap(*m_imgBubble);
                    setGeometry(posX, posY, 20, 20);
                    hMax=300;
                    break;
            case 12:m_imgBubble = new QPixmap("../bulle12.gif");
                    setPixmap(*m_imgBubble);
                    setGeometry(posX, posY, 50, 50);
                    hMax=250;
                    break;
            case 13:m_imgBubble = new QPixmap("../bulle13.gif");
                    setPixmap(*m_imgBubble);
                    setGeometry(posX, posY, 75, 75);
                    hMax=150;
                    break;
            case 14:m_imgBubble = new QPixmap("../bulle14.gif");
                    setPixmap(*m_imgBubble);
                    setGeometry(posX, posY, 110, 110);
                    hMax=100;
                    break;
            case 15:m_imgBubble = new QPixmap("../bulle15.gif");
                    setPixmap(*m_imgBubble);
                    setGeometry(posX, posY, 150, 150);
                    hMax=50;
                    break;
            case 16:m_imgBubble = new QPixmap("../bulle16.gif");
                    setPixmap(*m_imgBubble);
                    setGeometry(posX, posY, 200, 200);
                    hMax=30;
                    break;
            default:break;
        }
        versDroite=droite;
     
    }
     
    void Bubble::moveBubble()
    {
        int newX=0;
        int newY=0;
        int px;
        //HORIZONTAL
        if(versDroite)
        {
            //S'il n'est pas au bord
            if((this->x() + this->width()) <= parent->width())
            {
                newX=this->x()+1;
            }
            //S'il est au bord
            else
            {
                newX=this->x()-1;
                versDroite=false;
            }
        }
        else
        {
            if(this->x() >=0)
            {
                newX=this->x()-1;
            }
            else
            {
                newX=this->x()+1;
                versDroite=true;
            }
        }
        //VERTICAL
        if(this->y()<35)
        {
          px=4;
        }
        else if(this->y()>hMax+80)
        {
          px=3;
        }
        else if(this->y()>hMax+20)
        {
           px=2;
        }
        else
        {
           px=1;
        }
        if(versBas)
        {
          if((this->y() + this->height()) <= parent->height()-75)
          {
              newY=this->y()+px;
          }
          else
          {
              newY=this->y()-px;
              versBas=false;
          }
        }
        else
        {
            if(this->y() >=hMax)
            {
                newY=this->y()-px;
            }
            else
            {
                newY=this->y()+px;
                versBas=true;
            }
        }
        this->move(newX,newY);
    }
    La classe cercle:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    #include "cercle.h"
     
    Cercle::Cercle(int x,int y,int r)
    {
        centreX=x;
        centreY=y;
        rayon=r;
     
    }
     
    //Cercle::~Cerle(){}
     
    Cercle::Cercle(const Cercle& c)
    {
       centreX=c.centreX;
       centreY=c.centreY;
       rayon=c.rayon;
    }
     
    Cercle& Cercle::operator=(const Cercle& c)
    {
       if(this!=&c)
       {
           centreX=c.centreX;
           centreY=c.centreY;
           rayon=c.rayon;
       }
       return *this;
    }
     
    void Cercle::setCentreX(const int &x )
    {
       centreX=x;
    }
     
    int Cercle::getCentreX() const
    {
       return centreX;
    }
     
    void Cercle::setCentreY(const int &y )
    {
       centreY=y;
    }
     
    int Cercle::getCentreY() const
    {
       return centreY;
    }
     
    void Cercle::setRayon(const int &r )
    {
       rayon=r;
    }
     
    int Cercle::getRayon() const
    {
       return rayon;
    }
    La classe flèche
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    #include "fleche.h"
    #include "level.h"
    #include<QLabel>
    Fleche::Fleche(QWidget *parent): QLabel(parent)
    {
       this->parent = parent;
     
       m_fleche = new QPixmap("../fleche.gif");
     
       // m_fleche->setPixmap(QPixmap("../fleche.gif"));
       setPixmap(*m_fleche);
        setGeometry(431, 530, 28, 228);
     
        setBase();
     
        timerUp= new QTimer(this);
        QObject::connect(timerUp,SIGNAL(timeout()),this,SLOT(slotUp()));
     
        timerRight = new QTimer(this);
        QObject::connect(timerRight, SIGNAL(timeout()),this, SLOT(slotRight()));
     
        timerLeft = new QTimer(this);
        QObject::connect(timerLeft, SIGNAL(timeout()),this, SLOT(slotLeft()));
     
    }
     
    void Fleche::setBase()
    {
        //repositionnement de la fleche
        setGeometry(431, 530, 28, 228);
     
     
    }
    void Fleche::startMoveUp(int speed)
    {
        timerUp->start(speed);
    }
     
    void Fleche::slotUp()
    {
     
       // m_fleche->move(m_fleche->x(),m_fleche->y()-1);
        this->move(this->x(),this->y()-1);
        this->stopMoveLeft();
        this->stopMoveRight();
     
     
    }
     
    void Fleche::slotRight()
    {
        if((this->x() + this->width()) <= parent->width())
        {
            this->move(this->x()+1,this->y());
        }
    }
     
    void Fleche::slotLeft()
    {
        if(this->x() >=0)
        {
            this->move(this->x()-1,this->y());
        }
    }
     
    void Fleche::startMoveRight(int speed)
    {
        timerRight->start(speed);
    }
     
    void Fleche::stopMoveRight()
    {
        timerRight->stop();
    }
     
    void Fleche::startMoveLeft(int speed)
    {
        timerLeft->start(speed);
    }
     
    void Fleche::stopMoveLeft()
    {
        timerLeft->stop();
    }
    void Fleche::retourPositionPerso(int x,int y)
    {
     
        this->move(x+27,y);
    }
    et la classe Level:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
     
    #include "level.h"
     
    Level::Level(QWidget *parent) :QWidget(parent)
    {
        setFixedSize(800, 600);
        m_imageSonOn=new QPixmap("../sonon.gif");
        m_imageSonOff=new QPixmap("../sonoff.gif");
     
     
        m_labelFond= new QLabel(this);
        m_labelFond->setGeometry(0,0,800,600);
        m_labelFond->setPixmap(QPixmap("../Fond.gif"));
     
        perso = new Personage(m_labelFond);
        fl= new Fleche(m_labelFond);
     
        timerBubble=new QTimer(this);
        QObject::connect(timerBubble, SIGNAL(timeout()),this, SLOT(slotTimeBubble()));
     
        bulle.push_back(new Bubble(m_labelFond,11,105));
        bulle.push_back(new Bubble(m_labelFond,12,120,false));
        bulle.push_back(new Bubble(m_labelFond,13,170));
        bulle.push_back(new Bubble(m_labelFond,14,300,false));
        bulle.push_back(new Bubble(m_labelFond,15,450));
        timerBubble->start(5);
     
        //Petit mur devant le fond, pour la flèche-----//
        m_labelMur=new QLabel(m_labelFond);
        m_labelMur->setGeometry(0,267,800,600);
        m_labelMur->setPixmap(QPixmap("../mur.gif"));
        //-------------------------------------------------//
     
     
        //Move personnage
        timerRight = new QTimer(this);
        QObject::connect(timerRight, SIGNAL(timeout()),this, SLOT(slotRight()));
     
        timerLeft = new QTimer(this);
        QObject::connect(timerLeft, SIGNAL(timeout()),this, SLOT(slotLeft()));
     
    }
     
     
    void Level::slotTimeBubble()
    {
        for(int i=0;i<bulle.size();i++)
        {
            bulle.at(i)->moveBubble();
        }
    }
     
     
     
    //méthode qui gère les évènements clavier lorsqu'une touche est pressée
    void Level::keyPressEvent(QKeyEvent *event)
    {
            switch (event->key())
            {
            case Qt::Key_Right:
                timerRight->start(3);
                fl->startMoveRight(3);
                break;
            case Qt::Key_Left:
                timerLeft->start(3);
                fl->startMoveLeft(3);
                break;
            case Qt::Key_Space:
                perso->setTir();
                fl->startMoveUp(1);
                //fl=new Fleche(m_labelFond);
     
     
            fl->retourPositionPerso(perso->x(),perso->y());
     
                break;
            }
    }
     
    //méthode qui gère les évènements clavier lorsqu'une touche est relevée
    void Level::keyReleaseEvent( QKeyEvent * event )
    {
        switch (event->key())
        {
        case Qt::Key_Right:
            timerRight->stop();
            fl->stopMoveRight();
            break;
        case Qt::Key_Left:
            timerLeft->stop();
            fl->stopMoveLeft();
            break;
        case Qt::Key_Space:
            perso->setBase();
            //fl=new Fleche(m_labelFond);
            break;
        }
    }
     
     
     
     
    void Level::slotRight()
    {
        if((perso->x() + perso->width()) <= this->width())
        {
            perso->move(perso->x()+2,perso->y());
        }
    }
     
    void Level::slotLeft()
    {
        if(perso->x() >=0)
        {
            perso->move(perso->x()-2,perso->y());
        }
    }
    Voilà, si quelqu'un sait m'aider, ca serait GENIALLISIME!

  2. #2
    Responsable Qt & Livres


    Avatar de dourouc05
    Homme Profil pro
    Ingénieur de recherche
    Inscrit en
    Août 2008
    Messages
    26 630
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur de recherche
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2008
    Messages : 26 630
    Points : 188 616
    Points
    188 616
    Par défaut


    C'est bien, mais... quel est ton problème ? Tu nous donnes ton code, en espérant qu'on trouve une solution à ta place à un problème qu'on ne connaît pas ?
    Vous souhaitez participer aux rubriques Qt (tutoriels, FAQ, traductions) ou HPC ? Contactez-moi par MP.

    Créer des applications graphiques en Python avec PyQt5
    Créer des applications avec Qt 5.

    Pas de question d'ordre technique par MP !

  3. #3
    Nouveau membre du Club
    Femme Profil pro
    Développeur Web
    Inscrit en
    Novembre 2011
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2011
    Messages : 36
    Points : 30
    Points
    30
    Par défaut
    Le problème est que j'ignore comment récupérer la position de ma bulle qui est contenue dans un vecteur.

  4. #4
    Membre expérimenté

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2009
    Messages
    1 009
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2009
    Messages : 1 009
    Points : 1 738
    Points
    1 738
    Par défaut
    Euh... pos() ?

    Tu ne nous aide pas beaucoup à t'aider quand même. Ce qu'il faut c'est nous copier juste les quelques lignes de codes concernées (et pas les centaines de tes fichiers au complet), et surtout très important, les déclarations de tes variables parce que là il n'y a que les .cpp, et même si on devine un peu, ça nous prendrait un temps fou à lire tout ton code pour répondre à une question très ciblée en plus (déjà pour comprendre la question, moi je n'ai pas encore compris, si c'est une question technique, algorithmique... Qu'est-ce qui te bloque, qu'est-ce que tu as essayé, quel est le principe...).

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Gestion de collision simple.
    Par ThanosT dans le forum DirectX
    Réponses: 2
    Dernier message: 03/05/2006, 12h47
  2. Importer avec Ogre / Gestion des collisions
    Par alex6891 dans le forum Ogre
    Réponses: 9
    Dernier message: 19/02/2006, 17h43
  3. gestion de collisions delphi2005
    Par NotANumber dans le forum Langage
    Réponses: 2
    Dernier message: 16/10/2005, 20h01
  4. Gestion de collision et OpenGL
    Par kanux dans le forum Algorithmes et structures de données
    Réponses: 9
    Dernier message: 08/01/2005, 21h07
  5. Gestion des collisions - terrains
    Par Dranor dans le forum DirectX
    Réponses: 1
    Dernier message: 26/06/2003, 18h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo