Last active
October 21, 2015 23:12
-
-
Save birarda/977e04de70d10d76fdd3 to your computer and use it in GitHub Desktop.
baseball.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// baseball.js | |
// examples/toys | |
// | |
// Created by Stephen Birarda on 10/20/15. | |
// Copyright 2015 High Fidelity, Inc. | |
// | |
// Distributed under the Apache License, Version 2.0. | |
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
// | |
var BAT_MODEL = "atp:07bdd769a57ff15ebe9331ae4e2c2eae8886a6792b4790cce03b4716eb3a81c7.fbx"; | |
var BAT_COLLISION_SHAPE = "atp:1211ee12bc8ab0bb744e8582e15e728a00ca70a808550fc46d7284799b9a868a.obj"; | |
var BAT_POSITION = { x: -0.54, y: 1.21, z: 2.57 } | |
var lastTriggerValue = 0.0; | |
function checkTriggers() { | |
var rightTrigger = Controller.getTriggerValue(1); | |
if (rightTrigger == 0) { | |
if (lastTriggerValue > 0) { | |
// the trigger was just released, swing the bat with an angular velocity | |
var MAX_SWING_DEGREES_PER_SEC = 720.0; | |
var newSwing = MAX_SWING_DEGREES_PER_SEC * lastTriggerValue; | |
Entities.editEntity(bat, { | |
angularVelocity: { x: 0.0, y: newSwing, z: 0.0 } | |
}); | |
} | |
} else { | |
// the trigger is being held, set the rotation of the bat to show the wind up | |
var MAX_ROTATION = -75.0; | |
// add a rotation to the bat depending on the current value of the trigger | |
var newRotation = Quat.fromPitchYawRollDegrees(0.0, MAX_ROTATION * rightTrigger, 0.0); | |
Entities.editEntity(bat, { | |
rotation: newRotation, | |
angularVelocity: { x: 0.0, y: 0.0, z: 0.0 } | |
}); | |
} | |
} | |
// add the fresh bat at home plate | |
var bat = Entities.addEntity({ | |
name: 'Bat', | |
type: "Model", | |
modelURL: BAT_MODEL, | |
position: BAT_POSITION, | |
compoundShapeURL: BAT_COLLISION_SHAPE | |
}); | |
// hook the update so we can check controller triggers | |
Script.update.connect(checkTriggers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment