Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. for (int i = 0; i < cells.size(); i++) {
  2. float a[] = cells.get(i).updateAcceleration();
  3. cells.get(i).xVel += (a[0] * 1.0f/60.0f);
  4. cells.get(i).yVel += (a[1] * 1.0f/60.0f);
  5. cells.get(i).xPos += (cells.get(i).xVel * 1.0f/60.0f);
  6. cells.get(i).yPos += (cells.get(i).yVel * 1.0f/60.0f);
  7. cells.get(i).update();
  8. }
  9. for (int i = 0; i < cells.size(); i++) {
  10. //cells.get(i).update();
  11. for (int j = i+1; j < cells.size(); j++) {
  12. if (collision.BalltoBall(cells.get(i), cells.get(j))) {
  13. float normalx, normaly;
  14. normalx = cells.get(j).xPos - cells.get(i).xPos;
  15. normaly = cells.get(j).yPos - cells.get(i).yPos;
  16. float mag = (float) Math.hypot(normalx,normaly);
  17. normalx = normalx/mag;
  18. normaly = normaly/mag;
  19. float radiumSum = cells.get(i).radius+cells.get(j).radius;
  20. float penetrationdepth = radiumSum - mag;
  21. float percentCorrection = 0.5f;
  22. float slop = 0.02f;
  23.  
  24. float inverseMassI = 1.0f/cells.get(i).mass;
  25. float inverseMassJ = 1.0f/cells.get(j).mass;
  26. float totalinverseMass = inverseMassI + inverseMassJ;
  27.  
  28. float correctionX = normalx * ((Math.max(penetrationdepth-slop,0.0f)/totalinverseMass)*percentCorrection);
  29. float correctionY = normaly * ((Math.max(penetrationdepth-slop,0.0f)/totalinverseMass)*percentCorrection);
  30. cells.get(i).xPos -= (correctionX*inverseMassI);
  31. cells.get(i).yPos -= (correctionY*inverseMassI);
  32. cells.get(j).xPos += (correctionX*inverseMassJ);
  33. cells.get(j).yPos += (correctionY*inverseMassJ);
  34.  
  35. float velx = cells.get(i).xVel - cells.get(j).xVel;
  36. float vely = cells.get(i).yVel - cells.get(j).yVel;
  37.  
  38. float dot = (normalx * velx) + (normaly * vely);
  39.  
  40. Log.d("dot", ""+dot);
  41. float elasticity = 0.9f;
  42. float scalarJ = -(1+elasticity)*dot;
  43. scalarJ /= totalinverseMass;
  44. float impulsex = normalx * scalarJ;
  45. float impulsey = normaly * scalarJ;
  46.  
  47. cells.get(i).xVel += (impulsex*inverseMassI);
  48. cells.get(i).yVel += (impulsey*inverseMassI);
  49. cells.get(j).xVel -= (impulsex*inverseMassJ);
  50. cells.get(j).yVel -= (impulsey*inverseMassJ);
  51.  
  52. }
  53. }
  54.  
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement