Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Collision between two objects

Collision between two objects

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    patcs
    wrote on last edited by
    #1

    Hi, everyone.
    Im doing a game similar to angry birds, but now my problem is that i dont know how recognize when the bird and the box have collided and then the box move. Im trying do it with a timer(id collide box) but I can't start it just when the bird and the box are in the same position, it always start before the collision. I dont know if it will be possible in other way.

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import QtQml 2.2

    Window {

    //definición de la pantalla de juego como pantalla completa.
    id: window
    visible: true
    visibility: "FullScreen"

    Item{
    
    
        Timer {
            id:move
    
            interval: 10
            repeat: true
            running: false
    

    //definición de las variables tita, velocidad inicial y gravedad
    property double tita: -Math.PI/3
    property int vi: 30
    property double g: 3

    //cuando choca contra las paredes rebota con una velocidad de signo contrario

            onTriggered:{
    
    
                if(bird.x > 1300)
                    bird.vx = -bird.vx;
                if (bird.y >= 650)
                    bird.vy = -bird.vy;
                if(bird.x < 0)
                    bird.vx = -bird.vx;
                if(bird.y< 0)
                    bird.vy = -bird.vy;
    

    //movimiento del pollito con un tiro parabólico
    bird.vy = bird.vy - (g/2)
    bird.x= bird.x +(bird.vx)
    bird.y= bird.y -(bird.vy)

            }
    
        }
    
    }
    

    //fondo del juego
    Background{
    id: background
    }

    //posición y velocidad inicial del pollito.
    Bird{
    id: bird
    x: 100
    y: 550
    property double vx: move.viMath.cos(move.tita)
    property double vy: move.vi
    Math.sin(-move.tita)

    //el tiempo que va a tardar en moverse en cada iteracción en el eje x
    Behavior on x{
    NumberAnimation{
    duration: 10

            }
        }
    

    //el tiempo que va a tardar en moverse en cada iteracción en el eje y
    Behavior on y{
    NumberAnimation{
    duration:10
    }
    }

    }
    

    //posición del velocimetro
    Speedometer{
    id: speedometer
    x:-100
    y:620
    }

    //definición de las características de la flecha del indicador de velocidad
    Needle{
    id: needle
    x:-100
    y:620

       transform: Rotation{
            id: rotation1
    
             origin.x: 125; origin.y: 125
    
             RotationAnimation on angle { //rotación desde 0 a -90 grados
                 id: counterclockwise
                 from: 0
                 to: -90
                 running: false
                 duration: 2000
                 direction: RotationAnimation.Counterclockwise
                 loops: Animation.Infinite
             }
    
             RotationAnimation on angle{ //rotación desde los -90 hasta los 0 grados
                 id:clockwise
                 from: -90
                 to: 0
                 running: false
                 duration: 2000
                 direction: RotationAnimation.Clockwise
                 loops: Animation.Infinite
    
             }
        }
    
    
    
               Timer{ //la rotación de la aguja en cada diracción va a tardar 2 segundos
                   id:moveNeedle
                   interval: 2000
                   repeat: true
                   running: true
                   property bool titaNeedle: false
    
                   onTriggered: { //cambio de sentido (al contrario de las agujas del reloj)
                       if(titaNeedle == false){
                          counterclockwise.running = true
                           titaNeedle = true
                           clockwise.running = false
    
    
                       }else{ //cambio de sentido (en el sentido de las agujas del reloj)
                        titaNeedle = false
                           clockwise.running = true
                           counterclockwise.running = false
    
                       }
    
    
                   }
    
    
    
                }
    
               Timer{
                   id: angleNeedle
                   running: true
                   repeat: true
                   interval: 1
                   property double angle: 0
                   onTriggered: {
                       if(counterclockwise.running == true){
                            angle = angle +(-90/2000)
                            console.log(angle)
                       }
                       else if(clockwise.running == true){
                            angle = angle -(-90/2000)
                            console.log(angle)
                       }
    
                   }
               }
    
       }
    
    
    Box{
        id:box
        x:1100
        y:625
        property double vx: 0
        property double vy: 0
    
    
    }
    
    
    
    
    Timer {
        id:collideBox
    
        interval: 10
        repeat: true
        running: false
    

    //definición de las variable gravedad

        property double g: 9.8
    
    
    
        onTriggered:{
    
    
            if(bird.x == box.x)
                box.vx = bird.vx;
            if (bird.y == box.y)
                box.vy = bird.vy
    

    //movimiento del caja con un tiro parabólico
    box.vy = box.vy - (g/2)
    box.x= box.x +(box.vx)
    box.y= box.y -(box.vy)

        }
    
    }
    

    }

    //definición de la acción que se va a realizar cuando se pulse el ratón
    MouseArea{
    id: mouse
    anchors.fill: parent
    onClicked: {

           move.running = true
           }
    
    
    
        }
    

    //botón posicionado arriba y a la izquierda para poder salir de pantalla completa

    Button {
    text: "Salir"
    onClicked: window.close()

    }
    

    }

    1 Reply Last reply
    0
    • LorenzL Offline
      LorenzL Offline
      Lorenz
      wrote on last edited by
      #2

      Hi patcs,

      I strongly recommend to use a dedicated Game Engine like V-Play for tasks like these. It will make your life so much easier.

      Using V-Play, V-Play, you can use the BoxCollider item and have a look at a full example that will show you how to handle collisions (especially with boxes) properly.

      Additionally, the Box2D physics engine, that is built into V-Play, will help you create a more realistic parabolic trajectory.

      Cheers,
      Lorenz

      Developer @ V-Play Engine - http://v-play.net/qt

      V-Play simplifies

      • Game Development with Qt
      • Mobile App Dev with Qt esp. iOS & Android

      What others say
      V-Play scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

      1 Reply Last reply
      2
      • P Offline
        P Offline
        patcs
        wrote on last edited by
        #3

        Thanks, Lorenz.
        I could use V-Play with QML?

        1 Reply Last reply
        1
        • P Offline
          P Offline
          patcs
          wrote on last edited by
          #4

          Ok, I saw that I can!
          Im going to try, thanks a lot, I was going crazy!

          1 Reply Last reply
          1
          • LorenzL Offline
            LorenzL Offline
            Lorenz
            wrote on last edited by
            #5

            You're lucky, V-Play is completely based on development with QML :)

            Drop us a line at support@v-play.net if you have any questions.

            Developer @ V-Play Engine - http://v-play.net/qt

            V-Play simplifies

            • Game Development with Qt
            • Mobile App Dev with Qt esp. iOS & Android

            What others say
            V-Play scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

            1 Reply Last reply
            1
            • P Offline
              P Offline
              patcs
              wrote on last edited by
              #6

              @Lorenz I downloaded V-Play but when I try use for example BoxCollider It send a error: Type BoxCollider unavailable.

              I have to say you that I open the project that I was doing in Qml language so, I dont know if I have import some more libreries apart from VPlay 2.0

              thanks!

              LorenzL 1 Reply Last reply
              0
              • P patcs

                @Lorenz I downloaded V-Play but when I try use for example BoxCollider It send a error: Type BoxCollider unavailable.

                I have to say you that I open the project that I was doing in Qml language so, I dont know if I have import some more libreries apart from VPlay 2.0

                thanks!

                LorenzL Offline
                LorenzL Offline
                Lorenz
                wrote on last edited by
                #7

                Hi,

                It's best to ask V-Play specific Questions directly on our forums.

                Check out this post by on how to add V-Play to an existing project:
                https://v-play.net/developers/forums/t/can-we-use-v-play-components-in-a-qt-quick-controls-2-application#post-16126

                Best,
                Lorenz

                Developer @ V-Play Engine - http://v-play.net/qt

                V-Play simplifies

                • Game Development with Qt
                • Mobile App Dev with Qt esp. iOS & Android

                What others say
                V-Play scored #1 in Cross-Platform App Development Tools Report - see why: https://goo.gl/rgp3rq

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved