Skip to content

Commit

Permalink
Fix keyboard simulated AccelerometerTest and add comments to the ugly…
Browse files Browse the repository at this point in the history
… hack that is WP8 accelerometer support.
  • Loading branch information
kjpou1 committed Aug 11, 2013
1 parent f3ea51e commit cf55d58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 15 additions & 9 deletions cocos2d/platform/CCAccelerometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public void SetDelegate(ICCAccelerometerDelegate pDelegate)
private void accelerometer_CurrentValueChanged(object sender, Microsoft.Devices.Sensors.SensorReadingEventArgs<Microsoft.Devices.Sensors.AccelerometerReading> e)
{

//CCLog.Log("Acce value changed");
// We have to use reflection to get the Vector3 value out of Acceleration
// What happens is that the Sensor used XNA Vector3 and what we have done is replaced
// the XNA with MonoGame which our Sensor does not compile against.
//
// Result is this ugly hack.
object val = e.SensorReading.GetType()
.GetProperty("Acceleration",
BindingFlags.FlattenHierarchy |
Expand All @@ -115,18 +119,20 @@ private void accelerometer_CurrentValueChanged(object sender, Microsoft.Devices.

// store the accelerometer value in our acceleration object to be updated.
UpdateAccelerationValue(val.ToString());

//m_obAccelerationValue.X = acceleration.X;
//m_obAccelerationValue.Y = acceleration.Y;
//m_obAccelerationValue.Z = acceleration.Z;

m_obAccelerationValue.TimeStamp = e.SensorReading.Timestamp.Ticks;
}

private void UpdateAccelerationValue(string acceleration)
{
string[] temp = acceleration.Substring(1, acceleration.Length - 2).Split(':');
m_obAccelerationValue.X = float.Parse(temp[1].Substring(0, temp[1].Length - 1));
m_obAccelerationValue.Y = float.Parse(temp[2].Substring(0, temp[2].Length - 1));
// The format of the string is {X: 0000 Y: 0000 Z: 0000}
// Here we need to parse differently so that we can get a constant value back
// Cocos2D-XNA mapps the Sensor reading of the X value to be our Y value
// and the Y value to our X value. Also the values need to be negated so that
// it maps correctly.
m_obAccelerationValue.Y = -float.Parse(temp[1].Substring(0, temp[1].Length - 1));
m_obAccelerationValue.X = -float.Parse(temp[2].Substring(0, temp[2].Length - 1));
m_obAccelerationValue.Z = float.Parse(temp[3]);

}
Expand Down Expand Up @@ -163,9 +169,9 @@ public void Update()
if (keyboardState.IsKeyDown(Keys.Right))
stateValue.X = .1f;
if (keyboardState.IsKeyDown(Keys.Up))
stateValue.Y = .1f;
if (keyboardState.IsKeyDown(Keys.Down))
stateValue.Y = -.1f;
if (keyboardState.IsKeyDown(Keys.Down))
stateValue.Y = .1f;

stateValue.Normalize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public override void DidAccelerate(CCAcceleration pAccelerationValue)
ptTemp.X -= (float) pAccelerationValue.X * 9.81f;
ptTemp.Y += (float) pAccelerationValue.Y * 9.81f;
#else
ptTemp.X -= (float) pAccelerationValue.Y * 9.81f;
ptTemp.Y -= (float) pAccelerationValue.X * 9.81f;
//ptTemp.X -= (float) pAccelerationValue.Y * 9.81f;
//ptTemp.Y -= (float) pAccelerationValue.X * 9.81f;
ptTemp.X += (float)pAccelerationValue.X * 9.81f;
ptTemp.Y += (float)pAccelerationValue.Y * 9.81f;
#endif

CCPoint ptNext = pDir.ConvertToGl(ptTemp);
Expand Down

0 comments on commit cf55d58

Please sign in to comment.