Fix held block bobbing being jerky when leaving the ground. (Thanks WolfgangNS)

This commit is contained in:
UnknownShadow200 2015-12-25 11:11:57 +11:00
parent c52534923f
commit 1116c22a66
4 changed files with 36 additions and 12 deletions

View File

@ -11,7 +11,7 @@ namespace ClassicalSharp {
} }
public float legXRot, armXRot, armZRot; public float legXRot, armXRot, armZRot;
public float bobYOffset, tilt, walkTime, swing; public float bobYOffset, tilt, walkTime, swing;
protected float walkTimeO, walkTimeN, swingO, swingN; protected internal float walkTimeO, walkTimeN, swingO, swingN;
/// <summary> Calculates the next animation state based on old and new position. </summary> /// <summary> Calculates the next animation state based on old and new position. </summary>
protected void UpdateAnimState( Vector3 oldPos, Vector3 newPos, double delta ) { protected void UpdateAnimState( Vector3 oldPos, Vector3 newPos, double delta ) {

View File

@ -199,7 +199,7 @@ namespace ClassicalSharp {
bool right = IsMousePressed( MouseButton.Right ); bool right = IsMousePressed( MouseButton.Right );
InputHandler.PickBlocks( true, left, middle, right ); InputHandler.PickBlocks( true, left, middle, right );
if( !HideGui ) if( !HideGui )
BlockHandRenderer.Render( e.Time ); BlockHandRenderer.Render( e.Time, t );
} else { } else {
SelectedPos.SetAsInvalid(); SelectedPos.SetAsInvalid();
} }
@ -242,6 +242,7 @@ namespace ClassicalSharp {
ParticleManager.Tick( ticksPeriod ); ParticleManager.Tick( ticksPeriod );
Animations.Tick( ticksPeriod ); Animations.Tick( ticksPeriod );
AudioPlayer.Tick( ticksPeriod ); AudioPlayer.Tick( ticksPeriod );
BlockHandRenderer.Tick( ticksPeriod );
ticksThisFrame++; ticksThisFrame++;
ticksAccumulator -= ticksPeriod; ticksAccumulator -= ticksPeriod;
} }

View File

@ -175,6 +175,7 @@ namespace ClassicalSharp {
reader.Remove( 1 ); // remove opcode reader.Remove( 1 ); // remove opcode
lastOpcode = (PacketId)opcode; lastOpcode = (PacketId)opcode;
Action handler = handlers[opcode]; Action handler = handlers[opcode];
Console.WriteLine( lastOpcode );
if( handler == null ) if( handler == null )
throw new NotImplementedException( "Unsupported packet:" + (PacketId)opcode ); throw new NotImplementedException( "Unsupported packet:" + (PacketId)opcode );

View File

@ -31,7 +31,7 @@ namespace ClassicalSharp.Renderers {
game.Events.HeldBlockChanged += HeldBlockChanged; game.Events.HeldBlockChanged += HeldBlockChanged;
} }
public void Render( double delta ) { public void Render( double delta, float t ) {
if( game.Camera.IsThirdPerson || !game.ShowBlockInHand ) if( game.Camera.IsThirdPerson || !game.ShowBlockInHand )
return; return;
game.Graphics.Texturing = true; game.Graphics.Texturing = true;
@ -42,7 +42,7 @@ namespace ClassicalSharp.Renderers {
type = (byte)game.Inventory.HeldBlock; type = (byte)game.Inventory.HeldBlock;
if( playAnimation ) if( playAnimation )
DoAnimation( delta ); DoAnimation( delta );
PerformViewBobbing(); PerformViewBobbing( t );
SetupMatrices(); SetupMatrices();
if( game.BlockInfo.IsSprite[type] ) if( game.BlockInfo.IsSprite[type] )
@ -113,7 +113,7 @@ namespace ClassicalSharp.Renderers {
angleX = 0; angleX = 0;
animPeriod = period; animPeriod = period;
animSpeed = Math.PI / period; animSpeed = Math.PI / period;
if( updateLastType ) if( updateLastType )
lastType = (byte)game.Inventory.HeldBlock; lastType = (byte)game.Inventory.HeldBlock;
fakeP.Position = Vector3.Zero; fakeP.Position = Vector3.Zero;
@ -134,19 +134,21 @@ namespace ClassicalSharp.Renderers {
playAnimation = true; playAnimation = true;
} }
void PerformViewBobbing() { float bobTimeO, bobTimeN;
if( !game.ViewBobbing || !game.LocalPlayer.onGround ) return; void PerformViewBobbing( float t ) {
if( !game.ViewBobbing ) return;
LocalPlayer p = game.LocalPlayer; 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 // (0.5 + 0.5cos(2x)) is smoother than abs(cos(x)) at endpoints
double verTime = Math.Cos( p.curWalkTime * 2f ); double verTime = Math.Cos( bobTime * 2 );
float horAngle = 0.2f * (float)horTime; float horAngle = 0.2f * (float)horTime;
float verAngle = 0.2f * (float)(0.5 + 0.5 * verTime) * p.curSwing; float verAngle = 0.2f * (float)(0.5 + 0.5 * verTime) * p.curSwing;
if( horAngle > 0 ) if( horAngle > 0 )
fakeP.Position.X += horAngle; fakeP.Position.X += horAngle;
else else
fakeP.Position.Z += horAngle; fakeP.Position.Z += horAngle;
fakeP.Position.Y -= verAngle; fakeP.Position.Y -= verAngle;
} }
@ -155,6 +157,26 @@ namespace ClassicalSharp.Renderers {
SetAnimationSwitchBlock(); 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() { public void Dispose() {
game.Events.HeldBlockChanged -= HeldBlockChanged; game.Events.HeldBlockChanged -= HeldBlockChanged;
} }