From 1116c22a66c8341065a1745a47bcfec3889f7cba Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 25 Dec 2015 11:11:57 +1100 Subject: [PATCH] Fix held block bobbing being jerky when leaving the ground. (Thanks WolfgangNS) --- ClassicalSharp/Entities/AnimatedEntity.cs | 2 +- ClassicalSharp/Game/Game.cs | 3 +- ClassicalSharp/Network/NetworkProcessor.cs | 1 + ClassicalSharp/Rendering/BlockHandRenderer.cs | 42 ++++++++++++++----- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/ClassicalSharp/Entities/AnimatedEntity.cs b/ClassicalSharp/Entities/AnimatedEntity.cs index 2ea2f5c94..9e1a09720 100644 --- a/ClassicalSharp/Entities/AnimatedEntity.cs +++ b/ClassicalSharp/Entities/AnimatedEntity.cs @@ -11,7 +11,7 @@ namespace ClassicalSharp { } public float legXRot, armXRot, armZRot; public float bobYOffset, tilt, walkTime, swing; - protected float walkTimeO, walkTimeN, swingO, swingN; + protected internal float walkTimeO, walkTimeN, swingO, swingN; /// Calculates the next animation state based on old and new position. protected void UpdateAnimState( Vector3 oldPos, Vector3 newPos, double delta ) { diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index f5b160bc1..7a2c29601 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -199,7 +199,7 @@ namespace ClassicalSharp { bool right = IsMousePressed( MouseButton.Right ); InputHandler.PickBlocks( true, left, middle, right ); if( !HideGui ) - BlockHandRenderer.Render( e.Time ); + BlockHandRenderer.Render( e.Time, t ); } else { SelectedPos.SetAsInvalid(); } @@ -242,6 +242,7 @@ namespace ClassicalSharp { ParticleManager.Tick( ticksPeriod ); Animations.Tick( ticksPeriod ); AudioPlayer.Tick( ticksPeriod ); + BlockHandRenderer.Tick( ticksPeriod ); ticksThisFrame++; ticksAccumulator -= ticksPeriod; } diff --git a/ClassicalSharp/Network/NetworkProcessor.cs b/ClassicalSharp/Network/NetworkProcessor.cs index 08d4f19d8..86068ab05 100644 --- a/ClassicalSharp/Network/NetworkProcessor.cs +++ b/ClassicalSharp/Network/NetworkProcessor.cs @@ -175,6 +175,7 @@ namespace ClassicalSharp { reader.Remove( 1 ); // remove opcode lastOpcode = (PacketId)opcode; Action handler = handlers[opcode]; + Console.WriteLine( lastOpcode ); if( handler == null ) throw new NotImplementedException( "Unsupported packet:" + (PacketId)opcode ); diff --git a/ClassicalSharp/Rendering/BlockHandRenderer.cs b/ClassicalSharp/Rendering/BlockHandRenderer.cs index 660b481f7..3ddec1b4f 100644 --- a/ClassicalSharp/Rendering/BlockHandRenderer.cs +++ b/ClassicalSharp/Rendering/BlockHandRenderer.cs @@ -31,7 +31,7 @@ namespace ClassicalSharp.Renderers { game.Events.HeldBlockChanged += HeldBlockChanged; } - public void Render( double delta ) { + public void Render( double delta, float t ) { if( game.Camera.IsThirdPerson || !game.ShowBlockInHand ) return; game.Graphics.Texturing = true; @@ -42,7 +42,7 @@ namespace ClassicalSharp.Renderers { type = (byte)game.Inventory.HeldBlock; if( playAnimation ) DoAnimation( delta ); - PerformViewBobbing(); + PerformViewBobbing( t ); SetupMatrices(); if( game.BlockInfo.IsSprite[type] ) @@ -113,7 +113,7 @@ namespace ClassicalSharp.Renderers { angleX = 0; animPeriod = period; animSpeed = Math.PI / period; - + if( updateLastType ) lastType = (byte)game.Inventory.HeldBlock; fakeP.Position = Vector3.Zero; @@ -134,19 +134,21 @@ namespace ClassicalSharp.Renderers { playAnimation = true; } - void PerformViewBobbing() { - if( !game.ViewBobbing || !game.LocalPlayer.onGround ) return; + float bobTimeO, bobTimeN; + void PerformViewBobbing( float t ) { + if( !game.ViewBobbing ) return; LocalPlayer p = game.LocalPlayer; + float bobTime = Utils.Lerp( bobTimeO, bobTimeN, t ); - double horTime = Math.Sin( p.curWalkTime ) * p.curSwing; + double horTime = Math.Sin( bobTime ) * p.curSwing; // (0.5 + 0.5cos(2x)) is smoother than abs(cos(x)) at endpoints - double verTime = Math.Cos( p.curWalkTime * 2f ); - float horAngle = 0.2f * (float)horTime; + double verTime = Math.Cos( bobTime * 2 ); + float horAngle = 0.2f * (float)horTime; float verAngle = 0.2f * (float)(0.5 + 0.5 * verTime) * p.curSwing; - if( horAngle > 0 ) + if( horAngle > 0 ) fakeP.Position.X += horAngle; - else + else fakeP.Position.Z += horAngle; fakeP.Position.Y -= verAngle; } @@ -155,6 +157,26 @@ namespace ClassicalSharp.Renderers { SetAnimationSwitchBlock(); } + bool stop = false; + public void Tick( double delta ) { + Player p = game.LocalPlayer; + float bobTimeDelta = p.walkTimeN - p.walkTimeO; + bobTimeO = bobTimeN; + + if( game.LocalPlayer.onGround ) stop = false; + if( stop ) return; + bobTimeN += bobTimeDelta; + if( game.LocalPlayer.onGround ) return; + + // Keep returning the held block back to centre position. + if( Math.Sign( Math.Sin( bobTimeO ) ) == Math.Sign( Math.Sin( bobTimeN ) ) ) + return; + // Stop bob time at next periodic '0' angle. + double left = Math.PI - (bobTimeO % Math.PI); + bobTimeN = (float)(bobTimeO + left); + stop = true; + } + public void Dispose() { game.Events.HeldBlockChanged -= HeldBlockChanged; }