Collision between two objects



  • 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()

    }
    

    }



  • 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



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



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



  • 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.



  • @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!



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.