Skip to content
Permalink
Browse files

WiimoteEmu: MotionPlus is now working.

  • Loading branch information...
jordan-woyak committed Jan 17, 2019
1 parent 127b4e7 commit 9554ece874094a50182d3b8e0e036b2be08c403c
@@ -129,7 +129,7 @@ WiimoteCommon::DataReportBuilder::AccelData ConvertAccelData(const Common::Vec3&
u16(MathUtil::Clamp(std::lround(scaled_accel.z + zero_g), 0l, MAX_VALUE))};
}

Common::Matrix44 EmulateCursorMovement(ControllerEmu::Cursor* ir_group)
void EmulateCursor(MotionState* state, ControllerEmu::Cursor* ir_group, float time_elapsed)
{
using Common::Matrix33;
using Common::Matrix44;
@@ -149,10 +149,19 @@ Common::Matrix44 EmulateCursorMovement(ControllerEmu::Cursor* ir_group)

const auto cursor = ir_group->GetState(true);

return Matrix44::Translate({0, MOVE_DISTANCE * float(cursor.z), 0}) *
Matrix44::FromMatrix33(Matrix33::RotateX(pitch_scale * cursor.y) *
Matrix33::RotateZ(yaw_scale * cursor.x)) *
Matrix44::Translate({0, -NEUTRAL_DISTANCE, height});
// TODO: Move state out of ControllerEmu::Cursor
// TODO: Use ApproachPositionWithJerk
// TODO: Move forward/backward after rotation.
const auto new_position =
Common::Vec3{0, NEUTRAL_DISTANCE - MOVE_DISTANCE * float(cursor.z), height};
state->acceleration = new_position - state->position;
state->position = new_position;

// TODO: expose this setting in UI:
constexpr auto MAX_ACCEL = float(MathUtil::TAU * 100);

ApproachAngleWithAccel(state, Common::Vec3(pitch_scale * -cursor.y, 0, yaw_scale * -cursor.x),
MAX_ACCEL, time_elapsed);
}

void ApproachAngleWithAccel(RotationalState* state, const Common::Vec3& angle_target,
@@ -19,14 +19,19 @@ constexpr double GRAVITY_ACCELERATION = 9.80665;

struct PositionalState
{
// meters
Common::Vec3 position;
// meters/second
Common::Vec3 velocity;
// meters/second^2
Common::Vec3 acceleration;
};

struct RotationalState
{
// radians
Common::Vec3 angle;
// radians/second
Common::Vec3 angular_velocity;
};

@@ -47,11 +52,10 @@ void ApproachAngleWithAccel(RotationalState* state, const Common::Vec3& target,
void EmulateShake(PositionalState* state, ControllerEmu::Shake* shake_group, float time_elapsed);
void EmulateTilt(RotationalState* state, ControllerEmu::Tilt* tilt_group, float time_elapsed);
void EmulateSwing(MotionState* state, ControllerEmu::Force* swing_group, float time_elapsed);
void EmulateCursor(MotionState* state, ControllerEmu::Cursor* ir_group, float time_elapsed);

// Convert m/s/s acceleration data to the format used by Wiimote/Nunchuk (10-bit unsigned integers).
WiimoteCommon::DataReportBuilder::AccelData ConvertAccelData(const Common::Vec3& accel, u16 zero_g,
u16 one_g);

Common::Matrix44 EmulateCursorMovement(ControllerEmu::Cursor* ir_group);

} // namespace WiimoteEmu
@@ -149,11 +149,17 @@ void Wiimote::SendAck(OutputReportID rpt_id, ErrorCode error_code)

void Wiimote::HandleExtensionSwap()
{
if (WIIMOTE_BALANCE_BOARD == m_index)
{
// Prevent M+ or anything else silly from being attached to a balance board.
// In the future if we support an emulated balance board we can force the BB "extension" here.
return;
}

ExtensionNumber desired_extension_number =
static_cast<ExtensionNumber>(m_attachments->GetSelectedAttachment());

// const bool desired_motion_plus = m_motion_plus_setting->GetValue();
const bool desired_motion_plus = false;
const bool desired_motion_plus = m_motion_plus_setting.GetValue();

// FYI: AttachExtension also connects devices to the i2c bus

@@ -283,7 +289,7 @@ void Wiimote::HandleWriteData(const OutputReportWriteData& wd)
if (address >= 0x0FCA && address < 0x12C0)
{
// TODO: Only write parts of the Mii block.
// TODO: Use fifferent files for different wiimote numbers.
// TODO: Use different files for different wiimote numbers.
std::ofstream file;
File::OpenFStream(file, File::GetUserPath(D_SESSION_WIIROOT_IDX) + "/mii.bin",
std::ios::binary | std::ios::out);
@@ -578,12 +584,16 @@ void Wiimote::DoState(PointerWrap& p)
(m_is_motion_plus_attached ? m_motion_plus.GetExtPort() : m_extension_port)
.AttachExtension(GetActiveExtension());

m_motion_plus.DoState(p);
GetActiveExtension()->DoState(p);
if (m_is_motion_plus_attached)
m_motion_plus.DoState(p);

if (m_active_extension != ExtensionNumber::NONE)
GetActiveExtension()->DoState(p);

// Dynamics
p.Do(m_swing_state);
p.Do(m_tilt_state);
p.Do(m_cursor_state);
p.Do(m_shake_state);

p.DoMarker("Wiimote");
@@ -91,8 +91,7 @@ void Nunchuk::Update()
EmulateTilt(&m_tilt_state, m_tilt, 1.f / ::Wiimote::UPDATE_FREQ);
EmulateShake(&m_shake_state, m_shake, 1.f / ::Wiimote::UPDATE_FREQ);

const auto transformation =
GetRotationalMatrix(-m_tilt_state.angle) * GetRotationalMatrix(-m_swing_state.angle);
const auto transformation = GetRotationalMatrix(-m_tilt_state.angle - m_swing_state.angle);

Common::Vec3 accel = transformation * (m_swing_state.acceleration +
Common::Vec3(0, 0, float(GRAVITY_ACCELERATION)));

0 comments on commit 9554ece

Please sign in to comment.
You can’t perform that action at this time.