I need to rotate a cube in my scene with a specific initial speed within a specified timeframe.
The end angle of the cube must match the starting angle.
To account for potential variations, I am allowing for a deviation of up to 5% in the timeframe.
Currently, here is the status of my project: http://jsfiddle.net/5NWab/1/.
Everything is working as expected at the moment. However, if I alter the timeframe, for example to '3000': http://jsfiddle.net/5NWab/2/.
The crucial move() method of my cube is as follows:
Reel.prototype.move = function (delta) {
if (this.velocity < 0 && this.mesh.rotation.x == 0) {
return false;
}
// Create smooth end rotation
if (this.velocity < 0 && this.mesh.rotation.x != 0) {
this.mesh.rotation.x += Math.abs(delta * this.speedUp * 0.5 * this.timeSpan);
if (Math.abs(this.mesh.rotation.x - 2 * Math.PI) < 0.1) {
this.mesh.rotation.x = 0;
}
}
else {
this.mesh.rotation.x += delta * this.velocity;
this.time -= delta;
this.velocity = this.speedUp * this.time;
}
}
I am struggling to find a solution or method to achieve the desired outcome.
The challenge would be much easier if the delta
variable remained constant.
Ideally, it should be around 60fps = 1000/60
due to my use of requestAnimationFrame()
.
I came across this question which may provide some insight.
I believe the code needs to either
slow down the rotation before reaching the desired angle, especially if the final angle is slightly greater than the intended starting angle.
or speed up the rotation after surpassing the desired angle, particularly if the final angle is slightly less than the intended starting angle.
But how should it behave if the angle is exactly halfway from the desired one (e.g. 180° or PI)?
To clarify, here are the variables I know and those I am still unsure about:
Known:
- Initial speed
- Time interval
- Starting angle (usually 0)
The goal is for the cube to return to its initial angle/position by the end of the rotation. Given the fluctuating FPS count, adjusting the time interval may be necessary to achieve the desired position for the cube.