Вот код для Babylon.js playground с управляемой машиной и физикой Havok:
var createScene = function () {
var scene = new BABYLON.Scene(engine);
// Камера
var camera = new BABYLON.UniversalCamera("camera", new BABYLON.Vector3(0, 10, -20), scene);
camera.setTarget(BABYLON.Vector3.Zero());
// Свет
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = 0.7;
// Инициализация физики Havok
var hk = new BABYLON.HavokPlugin();
scene.enablePhysics(new BABYLON.Vector3(0, -9.81, 0), hk);
// Земля
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 50, height: 50}, scene);
ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.7 }, scene);
// Создание машины
var car = BABYLON.MeshBuilder.CreateBox("car", {width: 2, height: 1, depth: 4}, scene);
car.position.y = 2;
car.material = new BABYLON.StandardMaterial("carMat", scene);
car.material.diffuseColor = new BABYLON.Color3(1, 0, 0);
car.physicsImpostor = new BABYLON.PhysicsImpostor(car, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 10, restitution: 0.5 }, scene);
// Создание кубиков
var boxes = [];
for (var i = 0; i < 10; i++) {
var box = BABYLON.MeshBuilder.CreateBox("box" + i, {size: 2}, scene);
box.position.x = Math.random() * 30 - 15;
box.position.y = 1;
box.position.z = Math.random() * 30 - 15;
var boxMat = new BABYLON.StandardMaterial("boxMat" + i, scene);
boxMat.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
box.material = boxMat;
box.physicsImpostor = new BABYLON.PhysicsImpostor(box, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 1, restitution: 0.7 }, scene);
boxes.push(box);
}
// Управление
var inputMap = {};
scene.actionManager = new BABYLON.ActionManager(scene);
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) {
inputMap[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
}));
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {
inputMap[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
}));
// Переменные для управления
var speed = 20;
var turnSpeed = 0.05;
// Обновление каждый кадр
scene.registerBeforeRender(function () {
// Камера следует за машиной
camera.position.x = car.position.x;
camera.position.z = car.position.z - 20;
camera.setTarget(car.position);
// Управление машиной
if (inputMap["ArrowUp"]) {
var forward = new BABYLON.Vector3(
Math.sin(car.rotation.y) * speed,
0,
Math.cos(car.rotation.y) * speed
);
car.physicsImpostor.setLinearVelocity(forward);
}
if (inputMap["ArrowDown"]) {
var backward = new BABYLON.Vector3(
-Math.sin(car.rotation.y) * speed * 0.5,
0,
-Math.cos(car.rotation.y) * speed * 0.5
);
car.physicsImpostor.setLinearVelocity(backward);
}
if (inputMap["ArrowLeft"]) {
car.rotation.y -= turnSpeed;
}
if (inputMap["ArrowRight"]) {
car.rotation.y += turnSpeed;
}
});
return scene;
};
Этот код создает:
- Красную машину, которой можно управлять стрелками
- 10 разноцветных кубиков, случайно расположенных на поле
- Физику Havok для реалистичных столкновений
- Камеру, которая следует за машиной
Управление:
- ↑ - движение вперед
- ↓ - движение назад
- ← → - поворот влево/вправо
Машина может врезаться в кубики, и они будут разлетаться благодаря физике Havok.