Hi all,
I’m hoping to get some help the correct settings (physics world gravity, linear impulse on the ball) for a bouncing ball. I want to have a circle shape that bounces in the middle of the screen once every second and there is no ‘floor’ for the ball. My difficulty is that I’m unsure of the units of measurements for the PhysicsWorld gravity and the applyLinearImpulse() function for a collider body. This could be similar to the FlappyBird example, however the difference is that I have a measurement to follow (e.g. the ball must return to it’s starting position in exactly 1 second (or thereabouts), and bounce again). There’s also the constraint that it starts bouncing after the user has clicked a button. This is a simple example, however I’ve noted that setting the restitution(bounce)/friction for a body isn’t suitable to what I actually want to achieve, so I need to get this effect through gravity and applying a linear impulse.
Here’s an example:
import Felgo 3.0
import QtQuick 2.0
GameWindow {
id: gameWindow
activeScene: scene
screenWidth: 960
screenHeight: 640
EntityManager {
id: entityManager
entityContainer: scene
}
Scene {
id: scene
width: 480
height: 320
Timer {
id: timer
interval: 1000; repeat: false; running: false
onTriggered: ball.bounce()
}
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
StyledButton {
id: startButtonn
text: physicsWorld.running ? "Stop" : "Start"
anchors {top: parent.top; right: parent.right}
onClicked: physicsWorld.running = ! physicsWorld.running
}
EntityBase {
id: ball
height: 10
width: 10
x: parent.width / 2
y: parent.height / 2
anchors.centerIn: parent
Rectangle {
id: shape
anchors.fill: parent
color: "white"
radius: 10
}
CircleCollider {
id: collider
radius: shape.radius
bodyType: Body.Dynamic
anchors.centerIn: parent
}
function bounce() {
// reset linear velocity
collider.body.linearVelocity = Qt.point(0,0);
// create the impulse vector and apply it
var localForwardVector = collider.body.toWorldVector(Qt.point(0, -50));
collider.body.applyLinearImpulse(localForwardVector, collider.body.getWorldCenter());
}
}
PhysicsWorld {
id: physicsWorld
gravity.y: 5
running: false
onRunningChanged: {
if (running)
timer.restart()
else
timer.stop();
}
}
}
}
You can see the collider doing something, however the shape of the ball doesn’t move at all. Could someone please point out the what I might be missing here? Thanks a bunch!
-
This topic was modified 7 years ago by
cplusplusistough@gmail.com.