Forums

OverviewV-Play 1 SupportGravity in Space › Reply To: Gravity in Space

OverviewV-Play 1 SupportGravity in Space › Reply To: Gravity in Space
#6149

Alex
V-Play Team

Hi,

I am not aware of any super easy solution to this. However, I prepared a small example which you maybe can take as a starting point to achieve your desired behavior.

Player.qml

import VPlay 1.0
import QtQuick 1.1

EntityBase {
  id: player
  entityType: "player"

  width: 25
  height: 25

  Rectangle {
    width: player.width
    height: player.height
    x: -player.width/2
    y: -player.height/2
    color: "blue"
  }

  BoxCollider {
    id: collider
    width: player.width
    height: player.height
    x: -player.width/2
    y: -player.height/2
    bodyType: Body.Static
  }

  function getPosition() {
    return collider.body.getWorldCenter()
  }
}

Object.qml

import VPlay 1.0
import QtQuick 1.1

EntityBase {
  id: object
  entityType: "object"

  width: 4
  height: 4

  Rectangle {
    width: object.width
    height: object.height
    x: -object.width/2
    y: -object.height/2
    color: "red"
  }

  BoxCollider {
    id: collider
    width: object.width
    height: object.height
    x: -object.width/2
    y: -object.height/2
  }

  function getPosition() {
    return collider.body.getWorldCenter()
  }

  function applyGravityImpuls(forward) {
    collider.applyLinearImpulse(forward,getPosition())
  }
}

GravityLogic.js

var player
var objects = new Array
var force

function addObject(entityId) {
  objects.push(entityManager.getEntityById(entityId))
}

function applyGravity() {
  for(var i = 0; i < objects.length; i++) {
    var object = objects[i]
    var objectPosition = object.getPosition()
    var playerPosition = player.getPosition()
    // the distance could be useful if you want to apply the gravity only within a radius around the player
    var distance = Math.sqrt(Math.pow(Math.abs(objectPosition.x-playerPosition.x),2)+Math.pow(Math.abs(objectPosition.y-playerPosition.y),2))
    var atanY = playerPosition.y - objectPosition.y;
    var atanX = playerPosition.x - objectPosition.x;
    var angle = Math.atan2(atanY, atanX);
    var impulseX = force*Math.cos(angle)
    var impulseY = force*Math.sin(angle)
    object.applyGravityImpuls(Qt.point(impulseX,impulseY))
  }
}

main.qml

import VPlay 1.0
import QtQuick 1.1
import "GravityLogic.js" as Gravity

GameWindow {
  EntityManager {
    id: entityManager
    entityContainer: scene
  }
  Scene {
    id: scene
    property alias player: player

    PhysicsWorld {
      id: world
    }

    Player {
      id: player
      x: scene.width/2
      y: scene.height/2
    }

    Timer {
      id: gravityTimer
      interval: 100
      repeat: true
      running: false
      onTriggered: {
        Gravity.applyGravity()
      }
    }

    Component.onCompleted: {
      Gravity.player = player
      Gravity.force = 0.5
      for(var i = 0; i <50; i++) {
        var entityId = entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("Object.qml"),{"x":Math.random()*scene.width, "y":Math.random()*scene.height})
        Gravity.addObject(entityId)
      }
      gravityTimer.start()
    }
  }
}

Is this what you were looking for?

Cheers,
Alex

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