CarChallenge 3D Demo
import QtQuick 2.0
import Felgo 3.0
import Qt3D.Core 2.0
import Qt3D.Render 2.0
EntityBase {
id: car
entityType: "car"
property alias inputActionsToKeyCode: twoAxisController.inputActionsToKeyCode
property alias image: image
property alias controller: twoAxisController
Component.onCompleted: {
console.debug("car.onCompleted()")
console.debug("car.x:", x)
var mapped = mapToItem(world.debugDraw, x, y)
console.debug("car.x world:", mapped.x)
}
Image {
id: image
source: "../../assets/img/car_red.png"
anchors.centerIn: parent
width: boxCollider.width
height: boxCollider.height
property list<Item> imagePoints: [
Item {x: image.width/2+30}
]
}
Render3D {
id: carModel
source: "../../assets/3d/kart2-noShadow.obj"
texture: variationType === "carRed" ? "../../assets/3d/kart_col.tif" : "../../assets/3d/kart_blue_col.tif"
rotation: parent.rotation + 270
rotationAxis: Qt.vector3d(0,0,1)
rotationOffset: 270
rotationAxisOffset: Qt.vector3d(1,0,0)
scaleOffset: 0.8
xOffset: 6*scaleOffset
yOffset: 18*scaleOffset
}
Render3D {
xOffset: 3
yOffset: 5
zOffset: -0.2
rotation: parent.rotation
rotationAxis: Qt.vector3d(0,0,1)
rotationOffset: 270
rotationAxisOffset: Qt.vector3d(1,0,0)
texture: "../../assets/3d/car_shadow.jpg"
components: [renderTransform, material, planeMesh]
}
PlaneMesh {
id: planeMesh
width: boxCollider.width
height: boxCollider.height
}
TwoAxisController {
id: twoAxisController
onInputActionPressed: handleInputAction(actionName)
}
BoxCollider {
id: boxCollider
width: 60
height: 40
anchors.centerIn: parent
density: 0.02
friction: 0.4
restitution: 0.5
body.bullet: true
body.linearDamping: 10
body.angularDamping: 15
force: Qt.point(twoAxisController.yAxis*8000 * world.pixelsPerMeter, 0)
torque: twoAxisController.xAxis*2000 * world.pixelsPerMeter * world.pixelsPerMeter
Component.onCompleted: {
console.debug("car.physics.x:", x)
var mapped = mapToItem(world.debugDraw, x, y)
console.debug("car.physics.x world:", mapped.x)
}
fixture.onBeginContact: {
var fixture = other
var body = other.getBody()
var component = body.target
var collidingType = component.entityType
console.debug("car contact with: ", other, body, component)
console.debug("car collided entity type:", collidingType)
console.debug("car contactNormal:", contactNormal, "x:", contactNormal.x, "y:", contactNormal.y)
}
}
function handleInputAction(action) {
if( action === "fire") {
console.debug("creating weapon at current position x", car.x, "y", car.y)
console.debug("image.imagePoints[0].x:", image.imagePoints[0].x, ", image.imagePoints[0].y:", image.imagePoints[0].y)
var imagePointInWorldCoordinates = mapToItem(level,image.imagePoints[0].x, image.imagePoints[0].y)
console.debug("imagePointInWorldCoordinates x", imagePointInWorldCoordinates.x, " y:", imagePointInWorldCoordinates.y)
entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("Rocket.qml"), {"x": imagePointInWorldCoordinates.x, "y": imagePointInWorldCoordinates.y, "rotation": car.rotation})