CarChallenge Demo
import QtQuick 2.0
import Felgo 4.0
EntityBase {
id: entity
entityType: "rocket"
Component.onCompleted: {
console.debug("Rocket.onCompleted, width:", width);
applyForwardImpulse();
}
property real angleDeg
rotation: angleDeg
BoxCollider {
id: boxCollider
width: 50
height: 20
anchors.centerIn: parent
density: 0.003
friction: 0.4
restitution: 0.5
body.bullet: true
body.fixedRotation: true
property var lastWall: null
fixture.onBeginContact: (other, contactNormal) => {
var fixture = other;
var body = other.getBody();
var otherEntity = body.target
var collidingType = otherEntity.entityType
if(collidingType === "car" ||
collidingType === "rocket") {
entity.removeEntity()
return
}
if(otherEntity === lastWall) {
return;
}
lastWall = otherEntity
var normalAngle = 180 / Math.PI * Math.atan2(contactNormal.y, contactNormal.x)
var angleDiff = normalAngle - entity.rotation
var newAngle = entity.rotation + 2 * angleDiff + 180
entity.rotation = newAngle
boxCollider.body.linearVelocity = Qt.point(0,0)
applyForwardImpulse();
}
}
Image {
id: image
source: Qt.resolvedUrl("../../assets/img/rocket_green.png")
anchors.centerIn: parent
width: boxCollider.width
height: boxCollider.height
}
function applyForwardImpulse() {
var power = 1500
var rad = entity.rotation / 180 * Math.PI
var localForward = Qt.point(power * Math.cos(rad), power * Math.sin(rad))