Skip to content

Commit 052d810

Browse files
author
Shehata
committed
Compute wheel angle on the fly
1 parent 1a20d10 commit 052d810

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Apps/Sandcastle/gallery/Time Dynamic Wheels.html

+34-14
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
// A velocity vector property will give us the entity's speed and direction at any given time.
4949
var velocityVectorProperty = new Cesium.VelocityVectorProperty(position, false);
5050
var velocityVector = new Cesium.Cartesian3();
51-
// Store the wheel's rotation over time in a SampledProperty.
52-
var wheelAngleProperty = new Cesium.SampledProperty(Number);
53-
var wheelAngle = 0;
5451

5552
var numberOfSamples = 100;
5653
for (var i = 0; i <= numberOfSamples; ++i) {
@@ -61,15 +58,6 @@
6158
var locationFactor = Math.pow(factor, 2);
6259
var location = Cesium.Cartesian3.lerp(startPosition, endPosition, locationFactor, new Cesium.Cartesian3());
6360
position.addSample(time, location);
64-
// Rotate the wheels based on how fast the vehicle is moving at each timestep.
65-
velocityVectorProperty.getValue(time, velocityVector);
66-
var metersPerSecond = Cesium.Cartesian3.magnitude(velocityVector);
67-
var wheelRadius = 0.52;//in meters.
68-
var circumference = Math.PI * wheelRadius * 2;
69-
var rotationsPerSecond = metersPerSecond / circumference;
70-
71-
wheelAngle += ((Math.PI * 2 * totalSeconds) / numberOfSamples) * rotationsPerSecond;
72-
wheelAngleProperty.addSample(time, wheelAngle);
7361
}
7462

7563
function updateSpeedLabel(time, result) {
@@ -80,6 +68,40 @@
8068
return kmPerHour + ' km/hr';
8169
}
8270

71+
var scratchJulianDate = new Cesium.JulianDate();
72+
73+
var wheelAngleProperty = new Cesium.CallbackProperty(function (time, result) {
74+
// In order to compute the angle of the wheel at any given time,
75+
// we need to take samples of velocity from the start position
76+
// all the way up to the current time.
77+
var start = viewer.clock.startTime;
78+
var secondsUpToNow = Cesium.JulianDate.secondsDifference(time, start);
79+
80+
if (secondsUpToNow == 0) {
81+
return 0;
82+
}
83+
84+
var timeStep = 0.016;//Take 60 velocity samples per second.
85+
var numberOfAngleSamples = secondsUpToNow / timeStep;
86+
87+
var wheelAngle = 0;
88+
for (var i = 0; i <= numberOfAngleSamples; ++i) {
89+
var factor = (i / numberOfAngleSamples);
90+
var currentTime = Cesium.JulianDate.addSeconds(start, factor * secondsUpToNow, scratchJulianDate);
91+
// Get the velocity at this sampled time.
92+
velocityVectorProperty.getValue(currentTime, velocityVector);
93+
var metersPerSecond = Cesium.Cartesian3.magnitude(velocityVector);
94+
var wheelRadius = 0.52;//in meters.
95+
var circumference = Math.PI * wheelRadius * 2;
96+
var rotationsPerSecond = metersPerSecond / circumference;
97+
98+
wheelAngle += ((Math.PI * 2 * secondsUpToNow) / numberOfAngleSamples) * rotationsPerSecond;
99+
}
100+
101+
return wheelAngle;
102+
103+
}, false);
104+
83105
var rotationProperty = new Cesium.CallbackProperty(function(time, result) {
84106
return Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_X, wheelAngleProperty.getValue(time), result);
85107
}, false);
@@ -100,8 +122,6 @@
100122
orientation : new Cesium.VelocityOrientationProperty(position), // Automatically set the vehicle's orientation to the direction it's facing.
101123
model : {
102124
uri : '../../../../Apps/SampleData/models/GroundVehicle/GroundVehicle.glb',
103-
minimumPixelSize : 128,
104-
maximumScale : 20000,
105125
runAnimations : false,
106126
nodeTransformations : nodeTransformations
107127
},

0 commit comments

Comments
 (0)