From 39e67a182e363b1cf095058307ac3489ebbee1ad Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 14 Feb 2015 08:53:38 +1100 Subject: [PATCH] Rewrite player animations. --- Entities/LocalPlayer.cs | 4 ++-- Entities/NetPlayer.cs | 4 ++-- Entities/Player.cs | 50 ++++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Entities/LocalPlayer.cs b/Entities/LocalPlayer.cs index 7be21301b..44c26b39a 100644 --- a/Entities/LocalPlayer.cs +++ b/Entities/LocalPlayer.cs @@ -321,7 +321,7 @@ namespace ClassicalSharp { PhysicsTick( xMoving, zMoving ); nextPos = Position; Position = lastPos; - UpdateAnimState( lastPos, nextPos ); + UpdateAnimState( lastPos, nextPos, delta ); tickCount++; if( renderer != null ) { Bitmap bmp; @@ -341,7 +341,7 @@ namespace ClassicalSharp { renderer = new PlayerRenderer( this, Window ); Window.AsyncDownloader.DownloadSkin( SkinName ); } - SetCurrentAnimState( tickCount, t ); + SetCurrentAnimState( t ); renderer.Render( deltaTime ); } diff --git a/Entities/NetPlayer.cs b/Entities/NetPlayer.cs index 158706af1..a92cdf944 100644 --- a/Entities/NetPlayer.cs +++ b/Entities/NetPlayer.cs @@ -74,7 +74,7 @@ namespace ClassicalSharp { } tickCount++; UpdateCurrentState(); - UpdateAnimState( oldState.pos, newState.pos ); + UpdateAnimState( oldState.pos, newState.pos, delta ); } void UpdateCurrentState() { @@ -95,7 +95,7 @@ namespace ClassicalSharp { YawDegrees = Utils.InterpAngle( oldState.yaw, newState.yaw, t ); PitchDegrees = Utils.InterpAngle( oldState.pitch, newState.pitch, t ); - SetCurrentAnimState( tickCount, t ); + SetCurrentAnimState( t ); renderer.Render( deltaTime ); } } diff --git a/Entities/Player.cs b/Entities/Player.cs index a04083de6..837cca7ec 100644 --- a/Entities/Player.cs +++ b/Entities/Player.cs @@ -66,36 +66,44 @@ namespace ClassicalSharp { public float leftLegXRot, leftArmXRot, leftArmZRot; public float rightLegXRot, rightArmXRot, rightArmZRot; - protected float animStepO, animStepN, runO, runN; + protected float walkTimeO, walkTimeN, swingO, swingN; - protected void UpdateAnimState( Vector3 oldPos, Vector3 newPos ) { - animStepO = animStepN; - runO = runN; + protected void UpdateAnimState( Vector3 oldPos, Vector3 newPos, double delta ) { + walkTimeO = walkTimeN; + swingO = swingN; float dx = newPos.X - oldPos.X; float dz = newPos.Z - oldPos.Z; double distance = Math.Sqrt( dx * dx + dz * dz ); - float animSpeed = distance > 0.05 ? (float)distance * 3 : 0; - float runDist = distance > 0.05 ? 1 : 0; - runN += ( runDist - runN ) * 0.3f; - animStepN += animSpeed; + bool moves = distance > 0.05; + + if( moves ) { + walkTimeN += (float)distance * 2 * (float)( 20 * delta ); + swingN += (float)delta * 3; + if( swingN > 1 ) swingN = 1; + } else { + swingN -= (float)delta * 3; + if( swingN < 0 ) swingN = 0; + } } - protected void SetCurrentAnimState( int tickCount, float t ) { - float run = Utils.Lerp( runO, runN, t ); - float anim = Utils.Lerp( animStepO, animStepN, t ); - float time = tickCount + t; + const float armMax = (float)( 90 * Math.PI / 180.0 ); + const float legMax = (float)( 80 * Math.PI / 180.0 ); + const float idleMax = (float)( 3 * Math.PI / 180.0 ); + const float idleXPeriod = (float)( 2 * Math.PI / 5f ); + const float idleZPeriod = (float)( 2 * Math.PI / 3.5f ); + protected void SetCurrentAnimState( float t ) { + float swing = Utils.Lerp( swingO, swingN, t ); + float walkTime = Utils.Lerp( walkTimeO, walkTimeN, t ); + float idleTime = (float)( Window.accumulator ); + float idleXRot = (float)( Math.Sin( idleTime * idleXPeriod ) * idleMax ); + float idleZRot = (float)( idleMax + Math.Cos( idleTime * idleZPeriod ) * idleMax ); - rightArmXRot = (float)( Math.Cos( anim * 0.6662f + Math.PI ) * 1.5f * run ); - leftArmXRot = (float)( Math.Cos( anim * 0.6662f ) * 1.5f * run ); - rightLegXRot = (float)( Math.Cos( anim * 0.6662f ) * 1.4f * run ); - leftLegXRot = (float)( Math.Cos( anim * 0.6662f + Math.PI ) * 1.4f * run ); - - float idleZRot = (float)( Math.Cos( time * 0.09f ) * 0.05f + 0.05f ); - float idleXRot = (float)( Math.Sin( time * 0.067f ) * 0.05f ); + leftArmXRot = (float)( Math.Cos( walkTime ) * swing * armMax ) - idleXRot; + rightArmXRot = -leftArmXRot; + rightLegXRot = (float)( Math.Cos( walkTime ) * swing * legMax ); + leftLegXRot = -rightLegXRot; rightArmZRot = idleZRot; leftArmZRot = -idleZRot; - rightArmXRot += idleXRot; - leftArmXRot -= idleXRot; } public void SetModel( string modelName ) {