Make analog joystick input get clamped to a circle

In common case this only costs two floating point multiplies.
Uncommon case requires a square root.
This commit is contained in:
danieru 2013-04-17 22:53:38 -06:00
parent 4c46426d00
commit 6a84a6e4ea

View File

@ -15,6 +15,7 @@
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <math.h>
#include "HLE.h"
#include "../MIPS/MIPS.h"
#include "../CoreTiming.h"
@ -156,11 +157,19 @@ void __CtrlButtonUp(u32 buttonBit)
void __CtrlSetAnalog(float x, float y)
{
std::lock_guard<std::recursive_mutex> guard(ctrlMutex);
// TODO: Circle!
if (x > 1.0f) x = 1.0f;
if (y > 1.0f) y = 1.0f;
if (x < -1.0f) x = -1.0f;
if (y < -1.0f) y = -1.0f;
/* Confine joy stick to circular radius.
* We do this by normalizing our 2d
* vector only when the points are
* outside the circle. */
float length = x*x + y*y;
if (length > 1) {
/* We can do lazy evaluation
* of the square root because
* iff sqrt(X) > 1 then X > 1 */
length = sqrt(length);
x /= length;
y /= length;
}
ctrlCurrent.analog[0] = (u8)(x * 127.f + 128.f);
ctrlCurrent.analog[1] = (u8)(-y * 127.f + 128.f);
}