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