Forums

OverviewFelgo 2 Support (Qt 5) › Collision between two objects

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #16186

    Patricia

    Hi everyone,

    Im doing a simply game and I have a problem with the collision. I want that when I throw the bird and this one collision with a box, the box move like a real life when something crash with other thing.

    I let you the part of my code that I modify.

    MyBox.qml FILE:

    import QtQuick 2.0

    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import Felgo 3.0
    
    
    EntityBase {
      id: boxElement
    
    
      entityType: "mybox"
      width: 100
      height: 100
    
    
      MultiResolutionImage {
        id: box
        width: boxElement.width
        height: boxElement.height
        source: "img/box.png"
      }
    
    
      BoxCollider {
        id: collider
        width: box.width
        height: box.height
        bodyType: Body.Static
        collisionTestingOnlyMode: true
      }
    }
    
    Bird.qml FILE--->
    
    import QtQuick 2.0
    import Felgo 3.0
    
    
    
    
    
    
    
    
    EntityBase {
      id: birdElement
      entityType: "bird"
      property int salud: 3
    
    
    
    
    
    
         MultiResolutionImage{
          id: bird
          height: 75
          width: 75
    
    
          source: "img/pollito.png"
        }
    
    
    
    
    
    
        BoxCollider {
        id: birdCollider
        collisionTestingOnlyMode: true
    
    
        bodyType: Body.Dynamic
        height: bird.height
        width: bird.width
      }
    }
    Main.qml FILE:
    
    
     PhysicsWorld {
            id: world
            opacity: 0
            updatesPerSecondForPhysics: 60
        }
    
    Thank you!
    #16187

    Alex
    Felgo Team

    Hi Patricia,

    if you want the Box to be influenced by collisions, both collisionTestingOnlyMode and Body.Static won’t quite work for you, as you might can guess from their names. E.g. from the documentation “A static body is a body which isn’t affected by world forces it does not react to collisions.”

    So in general, if you want 2 bodies to be displaced by a collision, you need both bodies to by dynamic. Static bodies are used for walls or ground, collisionTestingOnlyMode bodies are e.g. used for sensors to check if a ball passed through a gate.

    If you have gravity in your game, but want the dynamic Box to float in the air as if it was static, you can set the gravityScale property to 0.

    Hope you get the idea and this answer was helpful!

    Cheers,
    Alex

    #16188

    Patricia

    Ok, now the bird recognize that there is a box and crash with it, but the box doesnt move, how I can fix it? and that the box move like it would be in a real life?

     

    thank you!

    #16189

    Patricia

    I added in MyBox.qml File—->

    import QtQuick 2.0

    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import Felgo 3.0
    
    
    EntityBase {
      id: boxElement
    
    
      entityType: "mybox"
      width: 100
      height: 100
    
    
      MultiResolutionImage {
        id: box
        width: boxElement.width
        height: boxElement.height
        source: "img/box.png"
      }
    
    
      BoxCollider {
        id: collider
        width: box.width
        height: box.height
        bodyType: Body.Dynamic
        gravityScale: 1
        restitution:1
        friction: 1
      }
    }
    Now the box move but, not fall because of the gravity. it keeps flying.
    #16190

    Alex
    Felgo Team

    Are both colliders set to bodyType: Body.Dynamic ?

    Also there are several properties you can use to change the way bodies interact like restitiution, density, friction, damping…

    You can find more info about this in the documentation of the physics components like https://felgo.com/doc/vplay-boxcollider/ (the link “List of all members, including inherited members” is usually pretty helpful)

    EDIT: I see you just posted code while I wrote this answer. Did you specify any gravity in your PhysicsWorld component? Also you can use the fixture.onBeginContact signal of colliders to change the properties, e.g. enable/disable the gravityScale.

    Cheers,
    Alex

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support