comparison TaskManager/Test/test_render/vacuum.cpp @ 223:ef7034891800 draft

add Vacuume
author gongo@localhost.localdomain
date Tue, 10 Feb 2009 13:11:26 +0900
parents
children 443c7ae1c7a7 4fad8f9329ac
comparison
equal deleted inserted replaced
222:0351818cf0fe 223:ef7034891800
1 #include <math.h>
2 #include "SceneGraphRoot.h"
3 #include "SGList.h"
4
5 static void cube_move_left(SceneGraphPtr node, int screen_w, int screen_h);
6 static void cube_move_right(SceneGraphPtr node, int screen_w, int screen_h);
7 static void cube_move_idle(SceneGraphPtr node, int screen_w, int screen_h);
8 static void cube_collision(SceneGraphPtr node, int screen_w, int screen_h,
9 SceneGraphPtr tree);
10 static void cube_split(SceneGraphPtr root);
11
12 static void vacuum_move(SceneGraphPtr node, int w, int h);
13 static void vacuum_coll(SceneGraphPtr node, int w, int h);
14
15 static float vacuum_speed = 3.0f;
16
17 static void
18 cube_move_left(SceneGraphPtr node, int screen_w, int screen_h)
19 {
20 node->xyz[0] -= node->stack_xyz[0];
21 #if 0
22 node->xyz[1] -= node->stack_xyz[0] * node->stack_xyz[1];
23 node->xyz[2] -= node->stack_xyz[0] * node->stack_xyz[2];
24 #else
25 node->xyz[1] += node->stack_xyz[1];
26 //node->xyz[2] -= node->stack_xyz[2];
27 #endif
28
29 if (node->xyz[0] < 0) {
30 node->set_move_collision(cube_move_right, cube_collision);
31 }
32
33 if (node->xyz[1] < 0 || node->xyz[1] > screen_h) {
34 node->stack_xyz[1] = -node->stack_xyz[1];
35 }
36
37 //node->angle[0] += 2.0f;
38 //node->angle[1] += 2.0f * node->stack_xyz[1];
39 //node->angle[2] += 2.0f * node->stack_xyz[2];
40
41 if (++node->frame > 60) {
42 cube_split(node);
43 }
44 }
45
46 static void
47 cube_move_right(SceneGraphPtr node, int screen_w, int screen_h)
48 {
49 node->xyz[0] += node->stack_xyz[0];
50 #if 0
51 node->xyz[1] -= node->stack_xyz[0] * node->stack_xyz[1];
52 node->xyz[2] -= node->stack_xyz[0] * node->stack_xyz[2];
53 #else
54 node->xyz[1] += node->stack_xyz[1];
55 #endif
56
57 if (node->xyz[0] > screen_w) {
58 node->set_move_collision(cube_move_left, cube_collision);
59 }
60
61 if (node->xyz[1] < 0 || node->xyz[1] > screen_h) {
62 node->stack_xyz[1] = -node->stack_xyz[1];
63 }
64
65 //node->angle[0] += 2.0f;
66 //node->angle[1] += 2.0f * node->stack_xyz[1];
67 //node->angle[2] += 2.0f * node->stack_xyz[2];
68
69 if (++node->frame > 60) {
70 cube_split(node);
71 }
72 }
73
74 static void
75 cube_split(SceneGraphPtr root)
76 {
77 SceneGraphPtr p = root->clone();
78 root->addBrother(p);
79
80 root->set_move_collision(cube_move_left, cube_collision);
81 p->set_move_collision(cube_move_right, cube_collision);
82
83 root->frame = 0;
84 p->frame = 0;
85
86 p->xyz[0] = root->xyz[0] + 2;
87 p->xyz[1] = root->xyz[1];
88 p->xyz[2] = root->xyz[2];
89
90 p->stack_xyz[0] = 2.0f;
91 p->stack_xyz[1] = random()%3-1;
92 p->stack_xyz[2] = random()%3-1;
93
94 root->xyz[0] -= 2;
95 root->stack_xyz[0] = 2.0f;
96 root->stack_xyz[1] = random()%3-1;
97 root->stack_xyz[2] = 0.0f;
98 //root->stack_xyz[2] = random()%3-1;
99 }
100
101
102 static void
103 cube_move_idle(SceneGraphPtr node, int screen_w, int screen_h)
104 {
105 Pad *pad = sgroot->getController();
106
107 if (pad->circle.isPush()) {
108 cube_split(node);
109 }
110 }
111
112 static void
113 cube_collision(SceneGraphPtr node, int screen_w, int screen_h,
114 SceneGraphPtr tree)
115 {
116 }
117
118 static void
119 vacuum_coll(SceneGraphPtr node, int screen_w, int screen_h,
120 SceneGraphPtr tree)
121 {
122 SceneGraphIteratorPtr it = sgroot->getIterator();
123 SceneGraphPtr bigm;
124 Pad *pad = sgroot->getController();
125 float dx, dy, r;
126 float q = 0;
127
128 if (pad->cross.isRelease()) {
129 return;
130 }
131
132 return;
133
134 for (; it->hasNext(MCUBE);) {
135 it->next(MCUBE);
136 SceneGraphPtr mcube = it->get();
137
138 dx = node->xyz[0] - mcube->xyz[0];
139 dy = node->xyz[1] - mcube->xyz[1];
140
141 r = sqrt(dx*dx + dy*dy);
142
143 if (r >= 1) q = 300/r;
144
145 if (r < 50.0f) {
146 mcube->remove();
147 continue;
148 }
149
150 if (dx == 0) {
151 if(mcube->xyz[1] > node->xyz[1]) {
152 mcube->xyz[1] -= q;
153 } else if(mcube->xyz[1] < node->xyz[1]) {
154 mcube->xyz[1] += q;
155 }
156 } else {
157 if(mcube->xyz[0] > node->xyz[0]) {
158 mcube->xyz[0] -= q*cos(atan(dy/dx));
159 mcube->xyz[1] -= q*sin(atan(dy/dx));
160 mcube->stack_xyz[0] = -q*cos(atan(dy/dx));
161 mcube->stack_xyz[1] = -q*sin(atan(dy/dx));
162 } else if(mcube->xyz[0] < mcube->xyz[0]) {
163 mcube->xyz[0] += q*cos(atan(dy/dx));
164 mcube->xyz[1] += q*sin(atan(dy/dx));
165 mcube->stack_xyz[0] = -q*cos(atan(dy/dx));
166 mcube->stack_xyz[1] = -q*sin(atan(dy/dx));
167 }
168 }
169 }
170 }
171
172 static void
173 vacuum_move(SceneGraphPtr node , int w, int h)
174 {
175 Pad *pad = sgroot->getController();
176
177 if (pad->right.isHold()) {
178 node->xyz[0] += vacuum_speed;
179 } else if (pad->left.isHold()) {
180 node->xyz[0] -= vacuum_speed;
181 }
182
183 if (pad->up.isHold()) {
184 node->xyz[1] -= vacuum_speed;
185 } else if (pad->down.isHold()) {
186 node->xyz[1] += vacuum_speed;
187 }
188
189 if (pad->start.isPush()) {
190 node->xyz[0] = w/2;
191 node->xyz[1] = h*0.8;
192 }
193 }
194
195 void
196 vacuum_init(int w, int h)
197 {
198 SceneGraphPtr cube;
199 SceneGraphPtr vacuum;
200 SceneGraphPtr back;
201
202 sgroot->createFromXMLfile("xml_file/mcube.xml");
203
204 cube = sgroot->createSceneGraph(MCUBE);
205 cube->xyz[0] = w/2;
206 cube->xyz[1] = h/2;
207 cube->xyz[2] = 0.0f;
208 cube->set_move_collision(cube_move_idle, cube_collision);
209
210 vacuum = sgroot->createSceneGraph(BIGMCUBE);
211 vacuum->xyz[0] = w/2;
212 vacuum->xyz[1] = h*0.8;
213 vacuum->set_move_collision(vacuum_move, vacuum_coll);
214
215 back = sgroot->createSceneGraph();
216 back->addChild(vacuum);
217 back->addChild(cube);
218
219 sgroot->setSceneData(back);
220 }