From ccb1dcd7f53099f656c1731baa1b947da7d867e4 Mon Sep 17 00:00:00 2001 From: alyosha-tas Date: Mon, 28 May 2018 13:14:40 -0400 Subject: [PATCH] GBHawk: improve Tilt controls --- .../Nintendo/GBHawk/GBHawk.ISettable.cs | 4 +- .../Nintendo/GBHawk/GBHawkControllers.cs | 38 ++++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.ISettable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.ISettable.cs index 695c1a1481..b6acb3d8d0 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.ISettable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawk.ISettable.cs @@ -64,7 +64,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk public enum ControllerType { Default, - Kirby + Tilt } [JsonIgnore] @@ -79,7 +79,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk set { if (value == ControllerType.Default) { Port1 = GBHawkControllerDeck.DefaultControllerName; } - else { Port1 = "Gameboy Controller + Kirby"; } + else { Port1 = "Gameboy Controller + Tilt"; } _GBController = value; } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs index 2a224ba58e..3ef5ffaf4d 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs @@ -106,15 +106,15 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk } } - [DisplayName("Gameboy Controller + Kirby")] - public class StandardKirby : IPort + [DisplayName("Gameboy Controller + Tilt")] + public class StandardTilt : IPort { - public StandardKirby(int portNum) + public StandardTilt(int portNum) { PortNum = portNum; Definition = new ControllerDefinition { - Name = "Gameboy Controller + Kirby", + Name = "Gameboy Controller + Tilt", BoolButtons = BaseDefinition .Select(b => "P" + PortNum + " " + b) .ToList(), @@ -125,6 +125,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk public int PortNum { get; } + public float theta, phi, theta_prev, phi_prev; + public ControllerDefinition Definition { get; } public byte Read(IController c) @@ -171,19 +173,34 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk // therefore this control scheme gives decreasing sensitivity in X as Y rotation inscreases public ushort ReadAccX(IController c) { - double theta = c.GetFloat(Definition.FloatControls[1]) * Math.PI / 180.0; - double phi = c.GetFloat(Definition.FloatControls[0]) * Math.PI / 180.0; + theta_prev = theta; + phi_prev = phi; + + theta = (float)(c.GetFloat(Definition.FloatControls[1]) * Math.PI / 180.0); + phi = (float)(c.GetFloat(Definition.FloatControls[0]) * Math.PI / 180.0); float temp = (float)(Math.Cos(theta) * Math.Sin(phi)); - return (ushort)(0x81D0 - Math.Floor(temp * 125)); + // here we add in rates of change parameters. + // a typical rate of change for a fast rotation is guessed at 0.5 rad / frame + // since rotations about X have less of a moment arm compared to by, we take 1/5 of the effect as a baseline + float temp2 = (float)((phi - phi_prev) / 0.5 * 25); + + return (ushort)(0x81D0 - Math.Floor(temp * 125) - temp2); } // acc y is just the sine of the angle + // we assume that ReadAccX is called first, which updates the the states public ushort ReadAccY(IController c) { - float temp = (float)Math.Sin(c.GetFloat(Definition.FloatControls[1]) * Math.PI / 180.0); - return (ushort)(0x81D0 - Math.Floor(temp * 125)); + float temp = (float)Math.Sin(theta); + + // here we add in rates of change parameters. + // a typical rate of change for a fast rotation is guessed at 0.5 rad / frame + // further it will be assumed that the resulting acceleration is roughly eqvuivalent to gravity + float temp2 = (float)((theta - theta_prev)/0.5 * 125); + + return (ushort)(0x81D0 - Math.Floor(temp * 125) + temp2); } private static readonly string[] BaseDefinition = @@ -193,7 +210,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk public void SyncState(Serializer ser) { - //nothing + // since we need rate of change of angle, need to savestate them + ser.Sync("theta", ref theta); } } } \ No newline at end of file