Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.model; |
| 2 | |
| 3 | import java.util.ArrayList; |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 4 | import java.util.List; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 5 | |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 6 | import android.graphics.PointF; |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 7 | |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 8 | public class BubbleModel |
| 9 | { |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 10 | private static final String TAG = BubbleModel.class.getSimpleName(); |
| 11 | |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 12 | private long lastUpdate = 0; |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 13 | public int width, height; |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 14 | private ArrayList<Bubble> bubbles = new ArrayList<Bubble>(); |
| 15 | private ArrayList<Attractor> attractors = new ArrayList<Attractor>(); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 16 | |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 17 | private static final double BUBBLE_RETURN_TIME_HALF_LIFE = .3; |
Adrien Béraud | 7ed23dc | 2013-05-06 16:27:24 +1000 | [diff] [blame] | 18 | private static final double BUBBLE_RETURN_TIME_LAMBDA = Math.log(2)/BUBBLE_RETURN_TIME_HALF_LIFE; |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 19 | |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 20 | private static final double FRICTION_VISCOUS = Math.log(2)/.2f; // Viscous friction factor |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 21 | |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 22 | private static final float BUBBLE_MAX_SPEED = 2500.f; // px.s-1 : Max target speed in px/sec |
| 23 | private static final float ATTRACTOR_SMOOTH_DIST = 50.f; // px : Size of the "gravity hole" around the attractor |
| 24 | private static final float ATTRACTOR_STALL_DIST = 15.f; // px : Size of the "gravity hole" flat bottom |
| 25 | private static final float ATTRACTOR_DIST_SUCK = 20.f; // px |
| 26 | |
| 27 | private static final float BORDER_REPULSION = 60000; // px.s^-2 |
| 28 | |
| 29 | private final float border_repulsion; |
| 30 | private final float bubble_max_speed; |
| 31 | private final float attractor_smooth_dist; |
| 32 | private final float attractor_stall_dist; |
| 33 | private final float attractor_dist_suck; |
| 34 | |
| 35 | private float density = 1.f; |
| 36 | |
| 37 | public BubbleModel(float screen_density) { |
| 38 | this.density = screen_density; |
| 39 | attractor_dist_suck = ATTRACTOR_DIST_SUCK*density; |
| 40 | bubble_max_speed = BUBBLE_MAX_SPEED*density; |
| 41 | attractor_smooth_dist = ATTRACTOR_SMOOTH_DIST*density; |
| 42 | attractor_stall_dist = ATTRACTOR_STALL_DIST*density; |
| 43 | border_repulsion = BORDER_REPULSION*density; |
| 44 | } |
| 45 | |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 46 | public void addBubble(Bubble b) { |
| 47 | b.setDensity(density); |
| 48 | bubbles.add(b); |
| 49 | } |
| 50 | |
| 51 | public List<Bubble> getBubbles() |
| 52 | { |
| 53 | return bubbles; |
| 54 | } |
| 55 | |
| 56 | public void addAttractor(Attractor a) { |
| 57 | a.setDensity(density); |
| 58 | attractors.add(a); |
| 59 | } |
| 60 | |
| 61 | public List<Attractor> getAttractors() |
| 62 | { |
| 63 | return attractors; |
| 64 | } |
| 65 | |
| 66 | public void clearAttractors() { |
| 67 | attractors.clear(); |
| 68 | } |
| 69 | |
| 70 | public void clear() { |
| 71 | clearAttractors(); |
| 72 | bubbles.clear(); |
| 73 | } |
| 74 | |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 75 | public void update() |
| 76 | { |
| 77 | long now = System.nanoTime(); |
| 78 | |
| 79 | // Do nothing if lastUpdate is in the future. |
| 80 | if (lastUpdate > now) |
| 81 | return; |
| 82 | |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 83 | double ddt = Math.min((now - lastUpdate) / 1000000000.0, .2); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 84 | lastUpdate = now; |
| 85 | |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 86 | float dt = (float)ddt; |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 87 | //Log.w(TAG, "update dt="+dt); |
| 88 | |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 89 | int attr_n = attractors.size(); |
| 90 | |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 91 | // Iterators should not be used in frequently called methods |
| 92 | // to avoid garbage collection glitches caused by iterator objects. |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 93 | for(int i=0, n=bubbles.size(); i<n; i++) { |
| 94 | Bubble b = bubbles.get(i); |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 95 | //Log.w(TAG, "update b"); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 96 | |
| 97 | if(!b.dragged) { |
Adrien Béraud | e0ef0c2 | 2013-05-18 01:56:27 +1000 | [diff] [blame] | 98 | float bx=b.getPosX(), by=b.getPosY(); |
| 99 | |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 100 | Attractor attractor = null; |
| 101 | PointF attractor_pos = b.attractor; |
| 102 | float attractor_dist = (attractor_pos.x-bx)*(attractor_pos.x-bx) + (attractor_pos.y-by)*(attractor_pos.x-by); |
| 103 | |
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame^] | 104 | for(int j=0; j<attr_n; j++) { |
| 105 | Attractor t = attractors.get(j); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 106 | float dx = t.pos.x-bx, dy = t.pos.y-by; |
| 107 | float adist = dx*dx + dy*dy; |
| 108 | if(adist < attractor_dist) { |
| 109 | attractor = t; |
| 110 | attractor_pos = t.pos; |
| 111 | attractor_dist = adist; |
| 112 | } |
| 113 | } |
| 114 | |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 115 | //float friction_coef = 1.f-FRICTION_VISCOUS*dt; |
| 116 | double friction_coef = 1+Math.expm1(-FRICTION_VISCOUS*ddt); |
| 117 | b.speed.x *= friction_coef; |
| 118 | b.speed.y *= friction_coef; |
| 119 | |
| 120 | //if(attractor != null) { |
| 121 | float target_speed; |
| 122 | float tdx = attractor_pos.x - bx, tdy = attractor_pos.y - by; |
| 123 | float dist = Math.max(1.f, (float) Math.sqrt(tdx*tdx + tdy*tdy)); |
| 124 | if(dist > attractor_smooth_dist) |
| 125 | target_speed = bubble_max_speed; |
| 126 | else if(dist < attractor_stall_dist) |
| 127 | target_speed = 0; |
| 128 | else { |
| 129 | float a = (dist-attractor_stall_dist)/(attractor_smooth_dist-attractor_stall_dist); |
| 130 | target_speed = bubble_max_speed*a; |
| 131 | } |
| 132 | if(attractor != null) { |
| 133 | if(dist > attractor_smooth_dist) |
| 134 | b.target_scale = 1.f; |
| 135 | else if(dist < attractor_stall_dist) |
| 136 | b.target_scale = .2f; |
| 137 | else { |
| 138 | float a = (dist-attractor_stall_dist)/(attractor_smooth_dist-attractor_stall_dist); |
| 139 | b.target_scale = a*.8f+.2f; |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | // border repulsion |
| 145 | if(bx < 0 && b.speed.x < 0) { |
| 146 | b.speed.x += dt * border_repulsion; |
| 147 | } else if(bx > width && b.speed.x > 0) { |
| 148 | b.speed.x -= dt * border_repulsion; |
| 149 | } |
| 150 | if(by < 0 && b.speed.y < 0) { |
| 151 | b.speed.y += dt * border_repulsion; |
| 152 | } else if(by > height && b.speed.y > 0) { |
| 153 | b.speed.y -= dt * border_repulsion; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | b.speed.x += dt * target_speed * tdx/dist; |
| 158 | b.speed.y += dt * target_speed * tdy/dist; |
| 159 | |
| 160 | double edt = -Math.expm1(-BUBBLE_RETURN_TIME_LAMBDA*ddt); |
| 161 | double dx = (attractor_pos.x - bx) * edt + Math.min(bubble_max_speed, b.speed.x) * dt; |
| 162 | double dy = (attractor_pos.y - by) * edt + Math.min(bubble_max_speed, b.speed.y) * dt; |
| 163 | // Log.w(TAG, "update dx="+dt+" dy="+dy); |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 164 | b.setPos((float)(bx+dx), (float)(by+dy)); |
| 165 | |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 166 | if(attractor != null && attractor_dist < attractor_dist_suck*attractor_dist_suck) { |
Adrien Béraud | c9c424d | 2013-05-30 17:47:35 +1000 | [diff] [blame^] | 167 | b.dragged = false; |
| 168 | if(attractor.callback.onBubbleSucked(b)) { |
| 169 | bubbles.remove(b); |
| 170 | n--; |
| 171 | } else { |
| 172 | b.target_scale = 1.f; |
| 173 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 174 | } |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 175 | } |
Adrien Béraud | 3326888 | 2013-05-18 03:41:15 +1000 | [diff] [blame] | 176 | |
Adrien Béraud | 6bbce91 | 2013-05-24 00:48:13 +1000 | [diff] [blame] | 177 | b.setScale(b.getScale() + (b.target_scale-b.getScale())*dt*10.f); |
| 178 | |
Adrien Béraud | 25fc409 | 2013-05-06 15:28:39 +1000 | [diff] [blame] | 179 | } |
| 180 | } |
Adrien Béraud | 0c9bd8f | 2013-05-30 16:16:57 +1000 | [diff] [blame] | 181 | |
| 182 | |
Adrien Béraud | 0446309 | 2013-05-06 14:17:22 +1000 | [diff] [blame] | 183 | } |