public void OnSensorChanged(SensorEvent e) { if (e.Sensor.Type != SensorType.Accelerometer) { return; } // Record the accelerometer data, the event's timestamp as well as // the current time. The latter is needed so we can calculate the // "present" time during rendering. In this application, we need to // take into account how the screen is rotated with respect to the // sensors (which always return data in a coordinate space aligned // to with the screen in its native orientation). switch (display.Rotation) { case SurfaceOrientation.Rotation0: sensor_values.Set(e.Values[0], e.Values[1]); break; case SurfaceOrientation.Rotation90: sensor_values.Set(-e.Values[1], e.Values[0]); break; case SurfaceOrientation.Rotation180: sensor_values.Set(-e.Values[0], -e.Values[1]); break; case SurfaceOrientation.Rotation270: sensor_values.Set(e.Values[1], -e.Values[0]); break; } sensor_timestamp = e.Timestamp; cpu_timestamp = DateTime.Now.Ticks; }
public void ComputePhysics(float sx, float sy, float dT, float dTC) { // Force of gravity applied to our virtual object float m = 1000.0f; // mass of our virtual object float gx = -sx * m; float gy = -sy * m; // ·F = mA <=> A = ·F / m We could simplify the code by // completely eliminating "m" (the mass) from all the equations, // but it would hide the concepts from this sample code. float invm = 1.0f / m; float ax = gx * invm; float ay = gy * invm; // Time-corrected Verlet integration The position Verlet // integrator is defined as x(t+Æt) = x(t) + x(t) - x(t-Æt) + // a(t)Ætö2 However, the above equation doesn't handle variable // Æt very well, a time-corrected version is needed: x(t+Æt) = // x(t) + (x(t) - x(t-Æt)) * (Æt/Æt_prev) + a(t)Ætö2 We also add // a simple friction term (f) to the equation: x(t+Æt) = x(t) + // (1-f) * (x(t) - x(t-Æt)) * (Æt/Æt_prev) + a(t)Ætö2 float dTdT = dT * dT; float x = Location.X + friction * dTC * (Location.X - prev_location.X) + accel.X * dTdT; float y = Location.Y + friction * dTC * (Location.Y - prev_location.Y) + accel.Y * dTdT; prev_location.Set(Location); Location.Set(x, y); accel.Set(ax, ay); }
protected override void OnSizeChanged(int w, int h, int oldw, int oldh) { // Compute the origin of the screen relative // to the origin of the bitmap origin.Set((w - ball_bitmap.Width) * 0.5f, (h - ball_bitmap.Height) * 0.5f); Bounds.X = (((float)w / (float)meters_to_pixels_x - BALL_DIAMETER) * 0.5f); Bounds.Y = (((float)h / (float)meters_to_pixels_y - BALL_DIAMETER) * 0.5f); Console.WriteLine(Bounds.X); }