blob: b5f149c46a957a218db922edb8452f5a104dae5a [file] [log] [blame]
Jason Sams177f8442010-10-29 10:19:21 -07001#pragma version(1)
2#pragma rs java_package_name(com.android.balls)
3
4#include "balls.rsh"
5
6float2 gGravityVector = {0.f, 9.8f};
7
8#pragma rs export_func(setGamma);
9
10float2 gMinPos = {0.f, 0.f};
11float2 gMaxPos = {1280.f, 700.f};
12
13float touchX;
14float touchY;
15float touchPressure = 0.f;
16
17void setGamma(float g) {
18}
19
20
21void root(const Ball_t *ballIn, Ball_t *ballOut, const BallControl_t *ctl, uint32_t x) {
22 float2 fv = {0, 0};
23 float2 pos = ballIn->position;
24 //rsDebug("physics pos in", pos);
25
26 int arcID = -1;
27 float arcInvStr = 100000;
28
29 const Ball_t * bPtr = rsGetElementAt(ctl->ain, 0);
30 for (uint32_t xin = 0; xin < ctl->dimX; xin++) {
31 float2 vec = bPtr[xin].position - pos;
32 float2 vec2 = vec * vec;
33 float len2 = vec2.x + vec2.y;
34
35 if (len2 < 1000) {
36 if (len2 > (4*4)) {
37 // Repulsion
38 float len = sqrt(len2);
39 if (len < arcInvStr) {
40 arcInvStr = len;
41 arcID = xin;
42 }
43 fv -= (vec / (len * len * len)) * 20000.f;
44 } else {
45 if (len2 < 0.1) {
46 continue;
47 }
48 // Collision
49 float2 axis = normalize(vec);
50 float e1 = dot(axis, ballIn->delta);
51 float e2 = dot(axis, bPtr[xin].delta);
52 float e = (e1 - e2) * 0.45f;
53 if (e1 > 0) {
54 fv -= axis * e;
55 } else {
56 fv += axis * e;
57 }
58 }
59 }
60 }
61
62 fv -= gGravityVector;
63 fv *= ctl->dt;
64
65 {
66 float2 tp = {touchX, touchY};
67 float2 vec = tp - ballIn->position;
68 float2 vec2 = vec * vec;
69 float len2 = vec2.x + vec2.y;
70
71 if (len2 > 0.2) {
72 float len = sqrt(len2);
73 fv -= (vec / (len * len)) * touchPressure * 1000.f;
74 }
75 }
76
77 ballOut->delta = ballIn->delta * 0.998f;
78 ballOut->position = ballIn->position;
79
80 ballOut->delta += fv;
81 ballOut->position += ballOut->delta * ctl->dt;
82
83 if (ballOut->position.x > gMaxPos.x) {
84 if (ballOut->delta.x > 0) {
85 ballOut->delta.x *= -0.7;
86 }
87 ballOut->position.x = gMaxPos.x;
88 }
89 if (ballOut->position.y > gMaxPos.y) {
90 if (ballOut->delta.y > 0) {
91 ballOut->delta.y *= -0.7;
92 }
93 ballOut->position.y = gMaxPos.y - 1.f;
94 }
95 if (ballOut->position.x < gMinPos.x) {
96 if (ballOut->delta.x < 0) {
97 ballOut->delta.x *= -0.7;
98 }
99 ballOut->position.x = gMinPos.x + 1.f;
100 }
101 if (ballOut->position.y < gMinPos.y) {
102 if (ballOut->delta.y < 0) {
103 ballOut->delta.y *= -0.7;
104 }
105 ballOut->position.y = gMinPos.y + 1.f;
106 }
107
108 ballOut->color.b = 1.f;
109 ballOut->color.r = min(sqrt(length(ballOut->delta)) * 0.1f, 1.f);
110 ballOut->color.g = min(sqrt(length(fv) * 0.1f), 1.f);
111 ballOut->arcID = arcID;
112 ballOut->arcStr = 8 / arcInvStr;
113
114 //rsDebug("physics pos out", ballOut->position);
115}
116