Hey again,
I am trying to implement physics into my game but I am having some trouble…
So basically I have a sprite of a player walking across the screen and I want them to vertically raise with a lot of velocity on contact with a cannon. My problem is getting physicsWorld and colliders working, as well as gravity.
I’ve coded in physicsWorld into one of my QML files, in this case I put it in the gameScene.qml which is this,
import QtQuick 2.7
import QtQuick.Window 2.2
import Felgo 3.0
import "entities"
Item {
width:Screen.width
height:Screen.height-10
focus: true
Keys.onPressed: {
if (event.key===Qt.Key_Right){
event.accepted = true;
walk.x=(walk.x) +7
}
if (event.key===Qt.Key_Left){
event.accepted = true;
walk.x=(walk.x) -7
}
}
Flickable{
width:Screen.width
height:Screen.height
contentHeight: Screen.height *4
contentWidth: Screen.width
interactive: true
boundsBehavior: Flickable.StopAtBounds
Image{
id: box
anchors.fill: parent
source: "artwork/rect.png"
sourceSize.width: Screen.width
sourceSize.height: Screen.height*4
}
AnimatedSprite {
id: walk
width: 100
height: 200
source: "artwork/WalkingManSpriteSheet.png"
frameCount: 8
frameRate: 6
frameWidth: 41
frameHeight:49
loops: 100
x: 200
y: 50
}
Image{
id:twitter
source: "artwork/twitter.png"
x:500
y:200
}
AnimatedSprite {
id: cannon
width: 300
height: 425
source: "artwork/cannon.png"
frameCount: 12
frameRate: 8
frameWidth: 128
frameHeight:155
loops: 100
x:300
y:90
}
}
}
I dont get any error codes but i am not able to see colliders, what am i doing wrong? I tried referencing examples and tutorials, thanks in advance.
P.S. – I made a separate folder called entities that contains the player.qml which is the AnimatedSprite with id: walk, but once i do that and delete the sprite code from gameScene.qml it wont show up at all but no errors in the output….? Even though i have the import statement for entities. This is the player.qml in case that will help with anything.
import Felgo 3.0
import QtQuick 2.0
EntityBase {
id: player
entityType: "player"
property alias collider: collider
property alias horizontalVelocity: collider.linearVelocity.x
AnimatedSprite {
id: walk
width: 100
height: 200
anchors.centerIn: parent
source: "artwork/WalkingManSpriteSheet.png"
frameCount: 8
frameRate: 6
frameWidth: 41
frameHeight:49
loops: 100
x: 200
y: 50
}
BoxCollider {
id: collider
height: parent.height
width: 30
anchors.horizontalCenter: parent.horizontalCenter
// this collider must be dynamic because we are moving it by applying forces and impulses
bodyType: Body.Dynamic // this is the default value but I wanted to mention it ;)
fixedRotation: true // we are running, not rolling...
bullet: true // for super accurate collision detection, use this sparingly, because it's quite performance greedy
sleepingAllowed: false
// apply the horizontal value of the TwoAxisController as force to move the player left and right
force: Qt.point(controller.xAxis*170*32,0)
// limit the horizontal velocity
onLinearVelocityChanged: {
if(linearVelocity.x > 170) linearVelocity.x = 170
if(linearVelocity.x < -170) linearVelocity.x = -170
}
}
}