Forums

OverviewV-Play 2 Support (Qt 5) › physics acceleration object over time

Tagged: 

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #11254

    Bas

    hi I want to speed an object over time.

    in this case it is a ball and a gametype like pong.

    i want to move the ball quicker over time, but with all the other physics in tact like friction etc.

    how can i achieve this?

    #11255

    Alex
    V-Play Team

    Hi Bas,

    the force property of the colliders will be the way to go, which is used to apply a force to a body every frame.

    Cheers,
    Alex

    #11256

    Bas

    thanx Alex,

     

    but how do i aply this to a ball in a pong like game?

    when i apply this, i get a drunken ball :)

    #11257

    Bas
    EntityBase {
        id: entity
        entityType: "ball"
    
        property int speed: 400
    
        CircleCollider {
            id: circleCollider
            property alias entity:entity
            radius: 172>>1
            // Entity does not have width and height, therefore we center the CircleCollider in the entity.
    //        anchors.centerIn: parent
            // set friction between 0 and 1
            fixture.friction: 0.3
            // restitution is bounciness
            fixture.restitution: 1.0
            fixture.density: 0.3
    //        body.angularDamping: 1
    //        body.angularVelocity: 0
            force: Qt.point(1000000.0,1000000.0)
            // The ball is a fast and small object therefore, we set the bullet flag to true to get better physic results
            // during collision detection.
            bullet: true
    //        body.fixedRotation: true
        }
    

     

    #11258

    Alex
    V-Play Team

    What’s wrong about a drunken ball? 😉

    Well if you take the current velocity into calculation you could achieve a good result? I didn’t test it yet but using something like

    Qt.point(circleCollider.linearVelocity.x/10,circleCollider.linearVelocity.y/10)

    could make sense? The idea would be to accelerate the ball by a 10th of its own speed every frame.

    Cheers,
    Alex

    #11259

    Bas

    hahaha, drunken ball is cool!

    hmm i am new to physics here so is it an initial force which can be set in the collider?

    i use the code of the pong tutorial online.

    but when i apply the force the ball is not going any faster.

    i use friction as well for the bouce rotation.

     

    #11269

    Alex
    V-Play Team

    Oh well, the pong tutorial is outdated and not yet part of the official documentation at the moment.

    The problem that you are facing in that old tutorial is, the ball is not really bouncing with pure physics, but instead on every collision it is stopped and the new velocity is calculated and applied to the ball. So you won’t see the effect of this small acceleration, because the next collision is happening and again the ball is stopped and the new velocity is calculated, always with the speed variable that is set to a fixed value of 400. I added a small project which shows the acceleration I suggested.

    import VPlay 2.0
    import QtQuick 2.0
    
    GameWindow {
      id: gameWindow
      activeScene: scene
      width: 960
      height: 640
    
      EntityManager {
        entityContainer: scene
      }
    
      Component.onCompleted: {
        ball.collider.applyLinearImpulse(Qt.point(70,34));
      }
    
      Scene {
        id: scene
        width: 480
        height: 320
    
        PhysicsWorld {
    
        }
    
        BoxCollider {
          width: 10
          height: parent.height
          bodyType: Body.Static
          anchors.right: parent.left
        }
        BoxCollider {
          width: 10
          height: parent.height
          bodyType: Body.Static
          anchors.left: parent.right
        }
        BoxCollider {
          width: parent.width
          height: 10
          bodyType: Body.Static
          anchors.bottom: parent.top
        }
        BoxCollider {
          width: parent.width
          height: 10
          bodyType: Body.Static
          anchors.top: parent.bottom
        }
    
        EntityBase {
          id: ball
          x: 50
          y: 50
          property alias collider: collider
          Rectangle {
            color: "#f00"
            width: 10
            height: 10
            radius: 5
            anchors.centerIn: parent
          }
    
          CircleCollider {
            id: collider
            radius: 5
            restitution: 1
            friction: 0
            bullet: true
            force: Qt.point(linearVelocity.x/10,linearVelocity.y/10)
            anchors.centerIn: parent
          }
        }
      }
    }

    In the pong tutorial, you could increase the value of the speed variable on every collision and this would make the ball faster.

    Cheers,
    Alex

    • This reply was modified 2 years, 8 months ago by  Alex.
    #11276

    Bas

    hi Alex,

     

    thanx for that, but i need rotation of the ball (in fact it isn’t really a ball) so i turned friction on.

    and there is only an initial force applied to the ball but when i alter some params i get the drunken ball again :)

     

    import VPlay 2.0
    import QtQuick 2.0
    
    GameWindow {
      id: gameWindow
      activeScene: scene
      width: 960
      height: 640
    
      EntityManager {
        entityContainer: scene
      }
    
      Component.onCompleted: {
        ball.collider.applyLinearImpulse(Qt.point(20,14));
      }
    
      Scene {
        id: scene
        width: 480
        height: 320
    
        PhysicsWorld {
    
        }
    
        BoxCollider {
          width: 10
          height: parent.height
          bodyType: Body.Static
          anchors.right: parent.left
        }
        BoxCollider {
          width: 10
          height: parent.height
          bodyType: Body.Static
          anchors.left: parent.right
        }
        BoxCollider {
          width: parent.width
          height: 10
          bodyType: Body.Static
          anchors.bottom: parent.top
        }
        BoxCollider {
          width: parent.width
          height: 10
          bodyType: Body.Static
          anchors.top: parent.bottom
        }
    
        EntityBase {
          id: ball
          x: 50
          y: 50
          property alias collider: collider
          Rectangle {
            color: "#f00"
            width: 10
            height: 10
            radius: 5
            anchors.centerIn: parent
          }
    
          CircleCollider {
            id: collider
            radius: 5
            restitution: 1
            friction: 0.2
            density: 0.03
            bullet: true
            force: Qt.point(linearVelocity.x/10,linearVelocity.y/10)
            anchors.centerIn: parent
          }
        }
      }
    }
    

     

    #11283

    Alex
    V-Play Team

    Hi Bas,

    well that will get more tricky, as you will have to take the rotation of the ball into account then when calculating forces. Also remember, if you have friction, you won’t have perfect bounces, so for a pong game friction is maybe not very suitable. Do you need the rotation to be 100% accurate or is it just a visual thing?

    Cheers,
    Alex

    #11284

    Bas

    hi Alex,

    it is more a visual thing,

    but would be nice that it is correct in a way that the rotation will be reversed when bouncing in oposite direction.

    the rotation does not need to affect speed after bounce.

     

    #11285

    Alex
    V-Play Team

    So I added a suggestion for the rotation, in this case it’s just a property binding on the velocity property of a MovementAnimation that is constantly rotating the rectangle of the entity, so it rotates in the direction it is traveling, and also rotates faster if the ball travels faster (at least in x direction):

    import VPlay 2.0
    import QtQuick 2.0
    
    GameWindow {
      id: gameWindow
      activeScene: scene
      width: 960
      height: 640
    
      EntityManager {
        id: entityManager
        entityContainer: scene
      }
    
      Component.onCompleted: {
        ball.collider.applyLinearImpulse(Qt.point(50,50), ball.collider.body.getWorldCenter());
      }
    
      Scene {
        id: scene
        width: 480
        height: 320
    
        PhysicsWorld {
    
        }
    
        BoxCollider {
          width: 10
          height: parent.height
          bodyType: Body.Static
          anchors.right: parent.left
        }
        BoxCollider {
          width: 10
          height: parent.height
          bodyType: Body.Static
          anchors.left: parent.right
        }
        BoxCollider {
          width: parent.width
          height: 10
          bodyType: Body.Static
          anchors.bottom: parent.top
        }
        BoxCollider {
          width: parent.width
          height: 10
          bodyType: Body.Static
          anchors.top: parent.bottom
        }
    
        EntityBase {
          id: ball
          x: 50
          y: 50
          property alias collider: collider
          Rectangle {
            id: rect
            color: "#f00"
            width: 10
            height: 10
          }
    
          MovementAnimation {
            target: rect
            property: "rotation"
            velocity: collider.linearVelocity.x // this is the property binding to alter the rotation
            running: true
          }
    
          CircleCollider {
            id: collider
            radius: 5
            restitution: 1
            friction: 0
            bullet: true
            force: Qt.point(linearVelocity.x/10,linearVelocity.y/10)
            fixture.onEndContact: {
              // you could also calculate the rotation velocity here, after a contact, based on any values you prefer
              // this would give you even more control
            }
          }
        }
      }
    }
    

    Cheers,
    Alex

    #11325

    Bas

    hi Alex,

    i aplied the force to a ball like object and i noticed that after a while the speed is not increasing anymore.

    is it limited?

     

    #11326

    Alex
    V-Play Team

    Hi,

    no there is no limit, just the fact that the ball is pretty much traveling the whole scene in a single frame at some point makes accurate acceleration not possible anymore at some point.

    If I add

    onLinearVelocityChanged: {
      console.debug("velocity " + linearVelocity.x + " | " + linearVelocity.y)
    }

    to the collider, I see that at a linearVelocity of around 1400 – 1600 pixels/second it starts to swing up and down. That’s probably because it is traveling so fast, that the force is applied in one direction before the property binding is updated with the direction change, which then actually slows the ball in that particular frame. But that only happens at a very high speed, which I don’t expect to be reached in a game anway?

    Cheers,
    Alex

    #11328

    Bas

    i added this also and got 1880 when boucing vertically with les horizontal velocity.

    The game is running at 1920 x 1080 resolution and the ball sprite is 180 pixels square.

    when the game speed is at its max, it is still quite easy to handle (arkanoid variant game)

    can i adjust other parameters so that it wil go faster?

     

    #11334

    Alex
    V-Play Team

    Hi,

    and you are also using 1920 x 1080 as the size of your scene? Why are you not keeping the scene at a smaller logical size and use the scene scaling mechanism that V-Play has integrated instead? Box2D is made for physical simulations at a smaller scale, see this link to an example of a similar problem.

    import VPlay 2.0
    import QtQuick 2.0
    
    GameWindow {
      id: gameWindow
      activeScene: scene
      width: 1920
      height: 1080
    
      EntityManager {
        id: entityManager
        entityContainer: scene
      }
    
      Component.onCompleted: {
        ball.collider.applyLinearImpulse(Qt.point(200,200), ball.collider.body.getWorldCenter());
      }
    
      Scene {
        id: scene
        width: 480 // this is a fourth of the gameWindow width
        height: 270 // this is a fourth of the gameWindow height
        // the scene will be scaled to fit into the gameWindow
    
        PhysicsWorld {
    
        }
    
        EntityBase {
          id: leftWall
          width: 10
          height: parent.height
          anchors.right: parent.left
          BoxCollider {
            anchors.fill: parent
            bodyType: Body.Static
          }
        }
        EntityBase {
          id: rightWall
          width: 10
          height: parent.height
          anchors.left: parent.right
          BoxCollider {
            anchors.fill: parent
            bodyType: Body.Static
          }
        }
        EntityBase {
          id: topWall
          width: parent.width
          height: 10
          anchors.bottom: parent.top
          BoxCollider {
            anchors.fill: parent
            bodyType: Body.Static
          }
        }
        EntityBase {
          id: bottomWall
          width: parent.width
          height: 10
          anchors.top: parent.bottom
          BoxCollider {
            anchors.fill: parent
            bodyType: Body.Static
          }
        }
    
        EntityBase {
          id: ball
          x: 50
          y: 50
          property alias collider: collider
          Rectangle {
            id: rect
            color: "#f00"
            width: 10
            height: 10
          }
    
          MovementAnimation {
            target: rect
            property: "rotation"
            velocity: collider.linearVelocity.x // this is the property binding to alter the rotation
            running: true
          }
    
          CircleCollider {
            id: collider
            radius: 5
            restitution: 1
            friction: 0
            bullet: true
            force: Qt.point(linearVelocity.x/10,linearVelocity.y/10)
            onLinearVelocityChanged: {
              console.debug("velocity " + linearVelocity.x + " | " + linearVelocity.y)
            }
          }
        }
      }
    }
    

    Cheers,
    Alex

Viewing 15 posts - 1 through 15 (of 15 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