content/hifi-content/alan/dev/Scripts/playaRover.js
2022-02-13 20:41:08 +01:00

214 lines
No EOL
6.1 KiB
JavaScript

// playaRover-Spawner.js
// examples
//
// Created by Alan Zimmerman on 3/2/16
// Copyright 2016 High Fidelity, Inc.
//
// Spawns a simple rover/truck thing that can be conducted forwards and backwards and turned left and right
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
/*global print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt, pointInExtents, vec3equal, setEntityCustomData, getEntityCustomData */
HIFI_S3 = "http://hifi-content.s3.amazonaws.com/alan/dev/";
var roverURL = HIFI_S3 + "rover-body.fbx";
var roverCollisionURL = HIFI_S3 + "rover-body.obj";
var roverbackwheelsURL = HIFI_S3 + "rover-backwheels2.fbx";
var roverbackwheelsCollisionURL = HIFI_S3 + "rover-backwheels.obj";
var roverfrontwheelsURL = HIFI_S3 + "rover-frontwheels2.fbx";
var roverfrontwheelsCollisionURL = HIFI_S3 + "rover-frontwheels.obj";
var roverthrottleURL = HIFI_S3 + "rover-throttle.fbx";
var roverthrottleCollisionURL = HIFI_S3 + "rover-throttle.obj";
var DISTANCE_IN_FRONT_OF_ME = 6.0;
var throttlePosition
var roverPosition
function makeRover() {
roverPosition = Vec3.sum(MyAvatar.position,
Vec3.multiplyQbyV(MyAvatar.orientation,
{ x: 0, y: 3.0, z: -DISTANCE_IN_FRONT_OF_ME }));
var rotation = Quat.multiply(MyAvatar.orientation,
Quat.fromPitchYawRollDegrees(0, 0, 0));
rover = Entities.addEntity({
type: "Model",
position: roverPosition,
rotation: rotation,
dynamic: true,
modelURL: roverURL,
shapeType: "compound",
compoundShapeURL: roverCollisionURL,
damping: 0,
linearDamping: 0,
friction: 0.85,
restitution: 0.15,
});
Script.setTimeout(function(){
Entities.editEntity(rover,{
velocity: {x: 0, y: -.01, z: 0},
gravity: {x: 0, y: -7, z: 0}
});
}, 4000);
}
function makeRoverBackWheels() {
var position = Vec3.sum(MyAvatar.position,
Vec3.multiplyQbyV(MyAvatar.orientation,
{ x: -4.15, y: 0.75, z: -DISTANCE_IN_FRONT_OF_ME + 0 }));
var rotation = Quat.multiply(MyAvatar.orientation,
Quat.fromPitchYawRollDegrees(0, 0, 0));
roverBackWheels = Entities.addEntity({
type: "Model",
position: position,
rotation: rotation,
dynamic: true,
modelURL: roverbackwheelsURL,
shapeType: "compound",
compoundShapeURL: roverbackwheelsCollisionURL,
});
}
function makeRoverFrontWheels() {
var position = Vec3.sum(MyAvatar.position,
Vec3.multiplyQbyV(MyAvatar.orientation,
{ x: 4.6, y: 0.7, z: -DISTANCE_IN_FRONT_OF_ME + 0 }));
var rotation = Quat.multiply(MyAvatar.orientation,
Quat.fromPitchYawRollDegrees(0, 0, 0));
roverFrontWheels = Entities.addEntity({
type: "Model",
position: position,
rotation: rotation,
dynamic: true,
modelURL: roverfrontwheelsURL,
shapeType: "compound",
compoundShapeURL: roverfrontwheelsCollisionURL,
});
}
function makeRoverThrottle() {
throttlePosition = Vec3.sum(MyAvatar.position,
Vec3.multiplyQbyV(MyAvatar.orientation,
{ x: 4.25, y: 3.15, z: -DISTANCE_IN_FRONT_OF_ME - 0.15 }));
var rotation = Quat.multiply(MyAvatar.orientation,
Quat.fromPitchYawRollDegrees(0, 0, 0));
roverThrottle = Entities.addEntity({
type: "Model",
name: "roverThrottle",
position: throttlePosition,
rotation: rotation,
dynamic: true,
modelURL: roverthrottleURL,
shapeType: "compound",
compoundShapeURL: roverthrottleCollisionURL,
friction: 0.85,
restitution: 0.15,
});
}
makeRover();
makeRoverBackWheels();
makeRoverFrontWheels();
makeRoverThrottle()
var originalThrottleDistanceToRover= Vec3.distance(roverPosition,throttlePosition)
Script.setInterval(update,100)
function update() {
var vehicleProps= Entities.getEntityProperties(rover);
var front= Quat.getRight(vehicleProps.rotation);
var throttleProps= Entities.getEntityProperties(roverThrottle);
var currentThrottleDistanceToRover= Vec3.distance(vehicleProps.position,throttleProps.position);
var deltaThrottlePosition= currentThrottleDistanceToRover - originalThrottleDistanceToRover;
//print("deltaThrottlePosition "+deltaThrottlePosition)
var newVelocity = Vec3.multiply(front,deltaThrottlePosition * 24)
// print(JSON.stringify(newVelocity))
Entities.editEntity(rover, {velocity: newVelocity});
}
function cleanup() {
Entities.deleteEntity(rover);
Entities.deleteEntity(roverBackWheels);
Entities.deleteEntity(roverFrontWheels);
Entities.deleteEntity(roverThrottle);
}
Script.scriptEnding.connect(cleanup);
/*
var rover;
function init() {
platform = Entities.addEntity({
name: "platform",
type: "Box",
position: { x: 0, y: 0, z: 0 },
dimensions: { x: 10, y: 2, z: 10 },
color: { red: 0, green: 0, blue: 255 },
gravity: { x: 0, y: 0, z: 0 },
visible: true,
locked: false,
lifetime: 6000,
velocity: { x: 1.0, y: 0, z: 0 },
damping: 0,
isDynamic: false
});
if (platform) {
if (MyAvatar.getParentID() != platform) {
MyAvatar.position = { x: 0, y: 3, z: 0 };
MyAvatar.setParentID(platform);
}
}
}
function update(dt) {
}
function shutdown() {
if (platform) {
MyAvatar.setParentID(0);
Entities.deleteEntity(platform);
}
}
Script.setTimeout(init, 3000);
Script.update.connect(update);
Script.scriptEnding.connect(shutdown);*/