Advertisement
Tatarize

Ball Motion with super-basic collision detection.

Dec 29th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. package viewmotionexample.ballanimation;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Point;
  9. import android.graphics.Rect;
  10. import android.graphics.RectF;
  11. import android.graphics.drawable.Drawable;
  12. import android.util.AttributeSet;
  13. import android.view.Display;
  14. import android.view.MotionEvent;
  15. import android.view.VelocityTracker;
  16. import android.view.View;
  17. import android.view.WindowManager;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.Random;
  21.  
  22.  
  23. /**
  24. * Created by David Olsen, 2015
  25. * Public Domain. Use for anything.
  26. */
  27.  
  28. public class CirclesView extends View {
  29. Random random = new Random();
  30. ArrayList<BallDrawing> circles = new ArrayList<>();
  31. Activity activity;
  32. Paint backgroundPaint = new Paint();
  33.  
  34. public CirclesView(Context context) {
  35. super(context);
  36. this.activity = (Activity) context;
  37. init();
  38. }
  39.  
  40. public CirclesView(Context context, AttributeSet attrs) {
  41. super(context, attrs);
  42. this.activity = (Activity) context;
  43. init();
  44. }
  45.  
  46. public CirclesView(Context context, AttributeSet attrs, int defStyleAttr) {
  47. super(context, attrs, defStyleAttr);
  48. this.activity = (Activity) context;
  49.  
  50. init();
  51. }
  52.  
  53. VelocityTracker velocityTracker;
  54.  
  55. @Override
  56. public boolean onTouchEvent(MotionEvent event) {
  57. switch (event.getAction()) {
  58. case MotionEvent.ACTION_UP:
  59. BallDrawing bd = new BallDrawing(event.getX(), event.getY());
  60. velocityTracker.computeCurrentVelocity(10);
  61. bd.vx = velocityTracker.getXVelocity();
  62. bd.vy = velocityTracker.getYVelocity();
  63. circles.add(bd);
  64. break;
  65. case MotionEvent.ACTION_MOVE:
  66. velocityTracker.addMovement(event);
  67. break;
  68. case MotionEvent.ACTION_DOWN:
  69. velocityTracker.clear();
  70. break;
  71. }
  72. return true;
  73. }
  74.  
  75. Drawable drawable;
  76. Rect drawableBounds = null;
  77.  
  78. int maxX, maxY;
  79. int minX = 0, minY = 0;
  80.  
  81. private void init() {
  82. WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
  83. Display display = wm.getDefaultDisplay();
  84. Point size = new Point();
  85. display.getSize(size);
  86. maxX = size.x;
  87. maxY = size.y;
  88.  
  89. velocityTracker = VelocityTracker.obtain();
  90.  
  91. backgroundPaint.setColor(Color.BLUE);
  92. drawable = getResources().getDrawable(R.drawable.ball);
  93. if (drawable != null) {
  94. drawable.setBounds(0, 0, 40, 40);
  95. drawableBounds = drawable.getBounds();
  96. }
  97. Thread t = new Thread(new Runnable() {
  98. @Override
  99. public void run() {
  100.  
  101. while (true) {
  102. try {
  103. Thread.sleep(100);
  104. tick();
  105.  
  106. } catch (InterruptedException e) {
  107. return;
  108. }
  109. }
  110. }
  111. });
  112. t.start();
  113. }
  114.  
  115. public void tick() {
  116. activity.runOnUiThread(new Runnable() {
  117. @Override
  118. public void run() {
  119. for (BallDrawing circle : circles) {
  120. circle.tickMotion();
  121. }
  122. for (int j = 0; j < circles.size(); j++) {
  123. BallDrawing jball = circles.get(j);
  124. RectF jballbounds = jball.getBounds();
  125. for (int k = 0; k < circles.size(); k++) {
  126. if (j != k) {
  127. BallDrawing kball = circles.get(k);
  128. RectF kballBounds = kball.getBounds();
  129. if (kballBounds.intersect(jballbounds)) {
  130. kball.hit();
  131. //jball.hit();
  132. }
  133. }
  134. }
  135. }
  136. }
  137. });
  138.  
  139. }
  140.  
  141.  
  142. @Override
  143. protected void onDraw(Canvas canvas) {
  144. for (BallDrawing c : circles) {
  145. c.draw(canvas);
  146. }
  147. }
  148.  
  149. private class BallDrawing {
  150. public float cx, cy;
  151. public float vx, vy;
  152.  
  153. public BallDrawing(float cx, float cy) {
  154. this.cx = cx;
  155. this.cy = cy;
  156. }
  157.  
  158. public RectF getBounds() {
  159. return new RectF(cx,cy,cx+drawableBounds.width(),cy+drawableBounds.height());
  160. }
  161.  
  162. public void applyMotion() {
  163. cx += vx;
  164. cy += vy;
  165. if (cx < minX) {
  166. vx =-vx;
  167. cx = minX - (cx - minX);
  168. }
  169. else if (cx > maxX) {
  170. vx =-vx;
  171. cx = maxX - (cx - maxX);
  172. }
  173. if (cy < minY) {
  174. vy =-vy;
  175. cy = minY - (cy - minY);
  176. }
  177. else if (cy > maxY) {
  178. vy =-vy;
  179. cy = maxY - (cy - maxY);
  180. }
  181. }
  182.  
  183. public void hit() {
  184. vx *= -1;
  185. vy *= -1;
  186. }
  187.  
  188. public void tickMotion() {
  189. invalidate((int) cx + drawableBounds.left, (int) cy + drawableBounds.top, (int) cx + 1 + drawableBounds.right, (int) cy + 1 + drawableBounds.bottom);
  190. applyMotion();
  191. invalidate((int) cx + drawableBounds.left, (int) cy + drawableBounds.top, (int) cx + 1 + drawableBounds.right, (int) cy + 1 + drawableBounds.bottom);
  192. }
  193.  
  194.  
  195. public void draw(Canvas canvas) {
  196. canvas.save();
  197. canvas.translate(cx, cy);
  198. drawable.draw(canvas);
  199. canvas.restore();
  200. }
  201.  
  202. }
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement