Skip to content

Commit

Permalink
Compute wheel angle on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
Shehata committed Jan 16, 2019
1 parent 1a20d10 commit 052d810
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions Apps/Sandcastle/gallery/Time Dynamic Wheels.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
// A velocity vector property will give us the entity's speed and direction at any given time.
var velocityVectorProperty = new Cesium.VelocityVectorProperty(position, false);
var velocityVector = new Cesium.Cartesian3();
// Store the wheel's rotation over time in a SampledProperty.
var wheelAngleProperty = new Cesium.SampledProperty(Number);
var wheelAngle = 0;

var numberOfSamples = 100;
for (var i = 0; i <= numberOfSamples; ++i) {
Expand All @@ -61,15 +58,6 @@
var locationFactor = Math.pow(factor, 2);
var location = Cesium.Cartesian3.lerp(startPosition, endPosition, locationFactor, new Cesium.Cartesian3());
position.addSample(time, location);
// Rotate the wheels based on how fast the vehicle is moving at each timestep.
velocityVectorProperty.getValue(time, velocityVector);
var metersPerSecond = Cesium.Cartesian3.magnitude(velocityVector);
var wheelRadius = 0.52;//in meters.
var circumference = Math.PI * wheelRadius * 2;
var rotationsPerSecond = metersPerSecond / circumference;

wheelAngle += ((Math.PI * 2 * totalSeconds) / numberOfSamples) * rotationsPerSecond;
wheelAngleProperty.addSample(time, wheelAngle);
}

function updateSpeedLabel(time, result) {
Expand All @@ -80,6 +68,40 @@
return kmPerHour + ' km/hr';
}

var scratchJulianDate = new Cesium.JulianDate();

var wheelAngleProperty = new Cesium.CallbackProperty(function (time, result) {
// In order to compute the angle of the wheel at any given time,
// we need to take samples of velocity from the start position
// all the way up to the current time.
var start = viewer.clock.startTime;
var secondsUpToNow = Cesium.JulianDate.secondsDifference(time, start);

if (secondsUpToNow == 0) {
return 0;
}

var timeStep = 0.016;//Take 60 velocity samples per second.
var numberOfAngleSamples = secondsUpToNow / timeStep;

var wheelAngle = 0;
for (var i = 0; i <= numberOfAngleSamples; ++i) {
var factor = (i / numberOfAngleSamples);
var currentTime = Cesium.JulianDate.addSeconds(start, factor * secondsUpToNow, scratchJulianDate);
// Get the velocity at this sampled time.
velocityVectorProperty.getValue(currentTime, velocityVector);
var metersPerSecond = Cesium.Cartesian3.magnitude(velocityVector);
var wheelRadius = 0.52;//in meters.
var circumference = Math.PI * wheelRadius * 2;
var rotationsPerSecond = metersPerSecond / circumference;

wheelAngle += ((Math.PI * 2 * secondsUpToNow) / numberOfAngleSamples) * rotationsPerSecond;
}

return wheelAngle;

}, false);

var rotationProperty = new Cesium.CallbackProperty(function(time, result) {
return Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_X, wheelAngleProperty.getValue(time), result);
}, false);
Expand All @@ -100,8 +122,6 @@
orientation : new Cesium.VelocityOrientationProperty(position), // Automatically set the vehicle's orientation to the direction it's facing.
model : {
uri : '../../../../Apps/SampleData/models/GroundVehicle/GroundVehicle.glb',
minimumPixelSize : 128,
maximumScale : 20000,
runAnimations : false,
nodeTransformations : nodeTransformations
},
Expand Down

0 comments on commit 052d810

Please sign in to comment.