1. package org.gdworkshop.ripple_p;
  2.  
  3. /*
  4. * Copyright (C) 2010 The Android Open Source Project
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18.  
  19.  
  20. import android.app.Activity;
  21. import android.content.Context;
  22. import android.graphics.Bitmap;
  23. import android.graphics.BitmapFactory;
  24. import android.graphics.Canvas;
  25. import android.graphics.BitmapFactory.Options;
  26. import android.hardware.Sensor;
  27. import android.hardware.SensorEvent;
  28. import android.hardware.SensorEventListener;
  29. import android.hardware.SensorManager;
  30. import android.os.Bundle;
  31. import android.os.PowerManager;
  32. import android.os.PowerManager.WakeLock;
  33. import android.util.DisplayMetrics;
  34. import android.view.Display;
  35. import android.view.Surface;
  36. import android.view.View;
  37. import android.view.WindowManager;
  38.  
  39. /**
  40. * This is an example of using the accelerometer to integrate the device's
  41. * acceleration to a position using the Verlet method. This is illustrated with
  42. * a very simple particle system comprised of a few iron balls freely moving on
  43. * an inclined wooden table. The inclination of the virtual table is controlled
  44. * by the device's accelerometer.
  45. *
  46. * @see SensorManager
  47. * @see SensorEvent
  48. * @see Sensor
  49. */
  50.  
  51. public class AccelRipple extends Activity {
  52.  
  53. private SimulationView mSimulationView;
  54. private SensorManager mSensorManager;
  55. private PowerManager mPowerManager;
  56. private WindowManager mWindowManager;
  57. private Display mDisplay;
  58. private WakeLock mWakeLock;
  59.  
  60. /** Called when the activity is first created. */
  61. @Override
  62. public void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64.  
  65. // Get an instance of the SensorManager
  66. mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  67.  
  68. // Get an instance of the PowerManager
  69. mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
  70.  
  71. // Get an instance of the WindowManager
  72. mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  73. mDisplay = mWindowManager.getDefaultDisplay();
  74.  
  75. // Create a bright wake lock
  76. mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
  77. .getName());
  78.  
  79. // instantiate our simulation view and set it as the activity's content
  80. mSimulationView = new SimulationView(this);
  81. setContentView(mSimulationView);
  82. }
  83.  
  84. @Override
  85. protected void onResume() {
  86. super.onResume();
  87. /*
  88. * when the activity is resumed, we acquire a wake-lock so that the
  89. * screen stays on, since the user will likely not be fiddling with the
  90. * screen or buttons.
  91. */
  92. mWakeLock.acquire();
  93.  
  94. // Start the simulation
  95. mSimulationView.startSimulation();
  96. }
  97.  
  98. @Override
  99. protected void onPause() {
  100. super.onPause();
  101. /*
  102. * When the activity is paused, we make sure to stop the simulation,
  103. * release our sensor resources and wake locks
  104. */
  105.  
  106. // Stop the simulation
  107. mSimulationView.stopSimulation();
  108.  
  109. // and release our wake-lock
  110. mWakeLock.release();
  111. }
  112.  
  113. class SimulationView extends View implements SensorEventListener {
  114. // diameter of the balls in meters
  115. private static final float sBallDiameter = 0.004f;
  116. private static final float sBallDiameter2 = sBallDiameter * sBallDiameter;
  117.  
  118. // friction of the virtual table and air
  119. private static final float sFriction = 0.1f;
  120.  
  121. private Sensor mAccelerometer;
  122. private long mLastT;
  123. private float mLastDeltaT;
  124.  
  125. private float mXDpi;
  126. private float mYDpi;
  127. private float mMetersToPixelsX;
  128. private float mMetersToPixelsY;
  129. private Bitmap mBitmap;
  130. private Bitmap mWood;
  131. private float mXOrigin;
  132. private float mYOrigin;
  133. private float mSensorX;
  134. private float mSensorY;
  135. private long mSensorTimeStamp;
  136. private long mCpuTimeStamp;
  137. private float mHorizontalBound;
  138. private float mVerticalBound;
  139. private final ParticleSystem mParticleSystem = new ParticleSystem();
  140.  
  141. /*
  142. * Each of our particle holds its previous and current position, its
  143. * acceleration. for added realism each particle has its own friction
  144. * coefficient.
  145. */
  146. class Particle {
  147. private float mPosX;
  148. private float mPosY;
  149. private float mAccelX;
  150. private float mAccelY;
  151. private float mLastPosX;
  152. private float mLastPosY;
  153. private float mOneMinusFriction;
  154.  
  155. Particle() {
  156. // make each particle a bit different by randomizing its
  157. // coefficient of friction
  158. final float r = ((float) Math.random() - 0.5f) * 0.2f;
  159. mOneMinusFriction = 1.0f - sFriction + r;
  160. }
  161.  
  162. public void computePhysics(float sx, float sy, float dT, float dTC) {
  163. // Force of gravity applied to our virtual object
  164. final float m = 1000.0f; // mass of our virtual object
  165. final float gx = -sx * m;
  166. final float gy = -sy * m;
  167.  
  168. /*
  169. * F = mA <=> A = F / m We could simplify the code by
  170. * completely eliminating "m" (the mass) from all the equations,
  171. * but it would hide the concepts from this sample code.
  172. */
  173. final float invm = 1.0f / m;
  174. final float ax = gx * invm;
  175. final float ay = gy * invm;
  176.  
  177. /*
  178. * Time-corrected Verlet integration The position Verlet
  179. * integrator is defined as x(t+t) = x(t) + x(t) - x(t-t) +
  180. * a(t)t2 However, the above equation doesn't handle variable
  181. * t very well, a time-corrected version is needed: x(t+t) =
  182. * x(t) + (x(t) - x(t-t)) * (t/t_prev) + a(t)t2 We also add
  183. * a simple friction term (f) to the equation: x(t+t) = x(t) +
  184. * (1-f) * (x(t) - x(t-t)) * (t/t_prev) + a(t)t2
  185. */
  186. final float dTdT = dT * dT;
  187. final float x = mPosX + mOneMinusFriction * dTC * (mPosX - mLastPosX) + mAccelX
  188. * dTdT;
  189. final float y = mPosY + mOneMinusFriction * dTC * (mPosY - mLastPosY) + mAccelY
  190. * dTdT;
  191. mLastPosX = mPosX;
  192. mLastPosY = mPosY;
  193. mPosX = x;
  194. mPosY = y;
  195. mAccelX = ax;
  196. mAccelY = ay;
  197. }
  198.  
  199. /*
  200. * Resolving constraints and collisions with the Verlet integrator
  201. * can be very simple, we simply need to move a colliding or
  202. * constrained particle in such way that the constraint is
  203. * satisfied.
  204. */
  205. public void resolveCollisionWithBounds() {
  206. final float xmax = mHorizontalBound;
  207. final float ymax = mVerticalBound;
  208. final float x = mPosX;
  209. final float y = mPosY;
  210. if (x > xmax) {
  211. mPosX = xmax;
  212. } else if (x < -xmax) {
  213. mPosX = -xmax;
  214. }
  215. if (y > ymax) {
  216. mPosY = ymax;
  217. } else if (y < -ymax) {
  218. mPosY = -ymax;
  219. }
  220. }
  221. }
  222.  
  223. /*
  224. * A particle system is just a collection of particles
  225. */
  226. class ParticleSystem {
  227. static final int NUM_PARTICLES = 15;
  228. private Particle mBalls[] = new Particle[NUM_PARTICLES];
  229.  
  230. ParticleSystem() {
  231. /*
  232. * Initially our particles have no speed or acceleration
  233. */
  234. for (int i = 0; i < mBalls.length; i++) {
  235. mBalls[i] = new Particle();
  236. }
  237. }
  238.  
  239. /*
  240. * Update the position of each particle in the system using the
  241. * Verlet integrator.
  242. */
  243. private void updatePositions(float sx, float sy, long timestamp) {
  244. final long t = timestamp;
  245. if (mLastT != 0) {
  246. final float dT = (float) (t - mLastT) * (1.0f / 1000000000.0f);
  247. if (mLastDeltaT != 0) {
  248. final float dTC = dT / mLastDeltaT;
  249. final int count = mBalls.length;
  250. for (int i = 0; i < count; i++) {
  251. Particle ball = mBalls[i];
  252. ball.computePhysics(sx, sy, dT, dTC);
  253. }
  254. }
  255. mLastDeltaT = dT;
  256. }
  257. mLastT = t;
  258. }
  259.  
  260. /*
  261. * Performs one iteration of the simulation. First updating the
  262. * position of all the particles and resolving the constraints and
  263. * collisions.
  264. */
  265. public void update(float sx, float sy, long now) {
  266. // update the system's positions
  267. updatePositions(sx, sy, now);
  268.  
  269. // We do no more than a limited number of iterations
  270. final int NUM_MAX_ITERATIONS = 10;
  271.  
  272. /*
  273. * Resolve collisions, each particle is tested against every
  274. * other particle for collision. If a collision is detected the
  275. * particle is moved away using a virtual spring of infinite
  276. * stiffness.
  277. */
  278. boolean more = true;
  279. final int count = mBalls.length;
  280. for (int k = 0; k < NUM_MAX_ITERATIONS && more; k++) {
  281. more = false;
  282. for (int i = 0; i < count; i++) {
  283. Particle curr = mBalls[i];
  284. for (int j = i + 1; j < count; j++) {
  285. Particle ball = mBalls[j];
  286. float dx = ball.mPosX - curr.mPosX;
  287. float dy = ball.mPosY - curr.mPosY;
  288. float dd = dx * dx + dy * dy;
  289. // Check for collisions
  290. if (dd <= sBallDiameter2) {
  291. /*
  292. * add a little bit of entropy, after nothing is
  293. * perfect in the universe.
  294. */
  295. dx += ((float) Math.random() - 0.5f) * 0.0001f;
  296. dy += ((float) Math.random() - 0.5f) * 0.0001f;
  297. dd = dx * dx + dy * dy;
  298. // simulate the spring
  299. final float d = (float) Math.sqrt(dd);
  300. final float c = (0.5f * (sBallDiameter - d)) / d;
  301. curr.mPosX -= dx * c;
  302. curr.mPosY -= dy * c;
  303. ball.mPosX += dx * c;
  304. ball.mPosY += dy * c;
  305. more = true;
  306. }
  307. }
  308. /*
  309. * Finally make sure the particle doesn't intersects
  310. * with the walls.
  311. */
  312. curr.resolveCollisionWithBounds();
  313. }
  314. }
  315. }
  316.  
  317. public int getParticleCount() {
  318. return mBalls.length;
  319. }
  320.  
  321. public float getPosX(int i) {
  322. return mBalls[i].mPosX;
  323. }
  324.  
  325. public float getPosY(int i) {
  326. return mBalls[i].mPosY;
  327. }
  328. }
  329.  
  330. public void startSimulation() {
  331. /*
  332. * It is not necessary to get accelerometer events at a very high
  333. * rate, by using a slower rate (SENSOR_DELAY_UI), we get an
  334. * automatic low-pass filter, which "extracts" the gravity component
  335. * of the acceleration. As an added benefit, we use less power and
  336. * CPU resources.
  337. */
  338. mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
  339. }
  340.  
  341. public void stopSimulation() {
  342. mSensorManager.unregisterListener(this);
  343. }
  344.  
  345. public SimulationView(Context context) {
  346. super(context);
  347. mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  348.  
  349. DisplayMetrics metrics = new DisplayMetrics();
  350. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  351. mXDpi = metrics.xdpi;
  352. mYDpi = metrics.ydpi;
  353. mMetersToPixelsX = mXDpi / 0.0254f;
  354. mMetersToPixelsY = mYDpi / 0.0254f;
  355.  
  356. // rescale the ball so it's about 0.5 cm on screen
  357. Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
  358. final int dstWidth = (int) (sBallDiameter * mMetersToPixelsX + 0.5f);
  359. final int dstHeight = (int) (sBallDiameter * mMetersToPixelsY + 0.5f);
  360. mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);
  361.  
  362. Options opts = new Options();
  363. opts.inDither = true;
  364. opts.inPreferredConfig = Bitmap.Config.RGB_565;
  365. mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood, opts);
  366. }
  367.  
  368. @Override
  369. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  370. // compute the origin of the screen relative to the origin of
  371. // the bitmap
  372. mXOrigin = (w - mBitmap.getWidth()) * 0.5f;
  373. mYOrigin = (h - mBitmap.getHeight()) * 0.5f;
  374. mHorizontalBound = ((w / mMetersToPixelsX - sBallDiameter) * 0.5f);
  375. mVerticalBound = ((h / mMetersToPixelsY - sBallDiameter) * 0.5f);
  376. }
  377.  
  378. @Override
  379. public void onSensorChanged(SensorEvent event) {
  380. if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
  381. return;
  382. /*
  383. * record the accelerometer data, the event's timestamp as well as
  384. * the current time. The latter is needed so we can calculate the
  385. * "present" time during rendering. In this application, we need to
  386. * take into account how the screen is rotated with respect to the
  387. * sensors (which always return data in a coordinate space aligned
  388. * to with the screen in its native orientation).
  389. */
  390.  
  391. switch (mDisplay.getOrientation()) {
  392. case Surface.ROTATION_0:
  393. mSensorX = event.values[0];
  394. mSensorY = event.values[1];
  395. break;
  396. case Surface.ROTATION_90:
  397. mSensorX = -event.values[1];
  398. mSensorY = event.values[0];
  399. break;
  400. case Surface.ROTATION_180:
  401. mSensorX = -event.values[0];
  402. mSensorY = -event.values[1];
  403. break;
  404. case Surface.ROTATION_270:
  405. mSensorX = event.values[1];
  406. mSensorY = -event.values[0];
  407. break;
  408. }
  409.  
  410. mSensorTimeStamp = event.timestamp;
  411. mCpuTimeStamp = System.nanoTime();
  412. }
  413.  
  414. @Override
  415. protected void onDraw(Canvas canvas) {
  416.  
  417. /*
  418. * draw the background
  419. */
  420.  
  421. canvas.drawBitmap(mWood, 0, 0, null);
  422.  
  423. /*
  424. * compute the new position of our object, based on accelerometer
  425. * data and present time.
  426. */
  427.  
  428. final ParticleSystem particleSystem = mParticleSystem;
  429. final long now = mSensorTimeStamp + (System.nanoTime() - mCpuTimeStamp);
  430. final float sx = mSensorX;
  431. final float sy = mSensorY;
  432.  
  433. particleSystem.update(sx, sy, now);
  434.  
  435. final float xc = mXOrigin;
  436. final float yc = mYOrigin;
  437. final float xs = mMetersToPixelsX;
  438. final float ys = mMetersToPixelsY;
  439. final Bitmap bitmap = mBitmap;
  440. final int count = particleSystem.getParticleCount();
  441. for (int i = 0; i < count; i++) {
  442. /*
  443. * We transform the canvas so that the coordinate system matches
  444. * the sensors coordinate system with the origin in the center
  445. * of the screen and the unit is the meter.
  446. */
  447.  
  448. final float x = xc + particleSystem.getPosX(i) * xs;
  449. final float y = yc - particleSystem.getPosY(i) * ys;
  450. canvas.drawBitmap(bitmap, x, y, null);
  451. }
  452.  
  453. // and make sure to redraw asap
  454. invalidate();
  455. }
  456.  
  457. @Override
  458. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  459. }
  460. }
  461. }