我正在尝试创建逼真的老虎机动画,在该动画中,“旋转”(实际上是循环翻译)会逐渐变慢,直到在指定的老虎机图像处停止。在我当前的实现中,它实际上确实停在了正确的图像上,但是它通过旋转经过它,然后反转方向并最终降落在正确的位置来实现。
displacement = initial_velocity*delta_time + 1/2*acceleration*delta_time^2
推导
acceleration = (2*total_displacement - 2*initial_velocity*delta_time) / delta_time^2
。
private getAnimationDisplacement(): number {
if(!this.spinInProgress) return 0
// Time
const totalDuration = this.machine.spinDuration // ms
const curTime = new Date()
const dt = curTime.getTime() - (this.lastFrameTime || this.spinInProgress.startTime).getTime() // ms
this.lastFrameTime = curTime
// Calculate total (final) displacement for current spin
const spinCount = 1
const spinOffset = spinCount * this.spinInProgress.slotFaces // number of faces
const faceOffset = this.spinInProgress.getFaceOffset().offset // pixels
const totalOffset = new FaceOffset(spinOffset + faceOffset) // pixels
const totalDisplacement = totalOffset.offset * this.getFaceHeight() // pixels
// Acceleration
const acceleration = (2*totalDisplacement - 2*INIT_VELOCITY * totalDuration) / Math.pow(totalDuration, 2) // pixels / ms^2
// Diplacement
this.displacement += this.velocity * dt
// Velocity
this.velocity += acceleration * dt
// Modulo by max displacement causes animation to loop
return this.displacement % this.maxDisplacement
}
我无法弄清楚这个标准运动方程是如何朝着正确的方向,然后向相反的方向运动,最终到达正确的位置。我正在努力避免怪异的反向动画。任何见解将不胜感激。