Mercurial > hg > Members > e085722 > Cerium
comparison Renderer/Test/property_test.cc @ 0:04e28d8d3c6f
first commit
author | Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Nov 2010 01:23:25 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:04e28d8d3c6f |
---|---|
1 #include <iostream> | |
2 #include <math.h> | |
3 #include "SceneGraphRoot.h" | |
4 #include "SceneGraph.h" | |
5 #include "TaskManager.h" | |
6 #include "property_test.h" | |
7 #include "Func.h" | |
8 | |
9 static ChainPropertyPtr properties[2]; | |
10 static ChainProperty cv[CHAIN_LEN]; | |
11 | |
12 | |
13 static void | |
14 init_chainold_vars(ChainPropertyPtr cv) { | |
15 cv->x = 0, cv->y = 0, cv->next_x = 0, cv->next_y = 0; | |
16 cv->vx = 0, cv->vy = 0, cv->next_vx = 0, cv->next_vy = 0; | |
17 cv->can_move = TRUE; | |
18 } | |
19 | |
20 static void | |
21 set_old_vector(ChainPropertyPtr cv, SceneGraphPtr sg) { | |
22 sg->xyz[0] = (float)cv->next_x; | |
23 sg->xyz[1] = (float)cv->next_y; | |
24 sg->xyz[2] = 0.0f; | |
25 } | |
26 | |
27 | |
28 static void | |
29 chain_old_move_ope(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
30 { | |
31 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; | |
32 Pad *pad = sgroot->getController(); | |
33 | |
34 if (pad->cross.isHold()) { | |
35 cv[CHAIN_LEN-1].can_move = FALSE; | |
36 if (pad->left.isHold()) { | |
37 cv[CHAIN_LEN-1].x += -5.0; | |
38 } else if (pad->right.isHold()) { | |
39 cv[CHAIN_LEN-1].x += 5.0; | |
40 } | |
41 | |
42 if (pad->up.isHold()) { | |
43 cv[CHAIN_LEN-1].y += -5.0; | |
44 } else if (pad->down.isHold()) { | |
45 cv[CHAIN_LEN-1].y += 5.0; | |
46 } | |
47 } else { | |
48 cv[CHAIN_LEN-1].can_move = TRUE; | |
49 } | |
50 } | |
51 | |
52 static void | |
53 chain_old_move(SceneGraphPtr sg, void *sgroot_, int w, int h) | |
54 { | |
55 int id = sg->id; | |
56 if(id == 0) { | |
57 for(int cnt = 0; cnt < 600; cnt++) { | |
58 for(int i = 0; i < CHAIN_LEN; i++) { | |
59 if(cv[i].can_move) { | |
60 double dx = cv[i-1].x - cv[i].x; | |
61 double dy = cv[i-1].y - cv[i].y; | |
62 double l = sqrt(dx * dx + dy * dy); | |
63 double a = k * (l - chain_width) / m; | |
64 double ax = a * dx / l; | |
65 double ay = a * dy / l; | |
66 if(i < CHAIN_LEN - 1) { | |
67 dx = cv[i+1].x - cv[i].x; | |
68 dy = cv[i+1].y - cv[i].y; | |
69 l = sqrt(dx * dx + dy * dy); | |
70 a = k * (l - chain_width) / m; | |
71 ax += a * dx / l; | |
72 ay += a * dy / l; | |
73 } | |
74 ay += g; | |
75 cv[i].vx *= safe; | |
76 cv[i].vy *= safe; | |
77 cv[i].next_vx = cv[i].vx + ax * dt; | |
78 cv[i].next_vy = cv[i].vy + ay * dt; | |
79 cv[i].next_x = cv[i].x + cv[i].vx * dt; | |
80 cv[i].next_y = cv[i].y + cv[i].vy * dt; | |
81 } else { | |
82 cv[i].next_x = cv[i].x; | |
83 cv[i].next_y = cv[i].y; | |
84 } | |
85 } | |
86 for(int i = 0; i < CHAIN_LEN; i++) { | |
87 cv[i].vx = cv[i].next_vx; | |
88 cv[i].vy = cv[i].next_vy; | |
89 cv[i].x = cv[i].next_x; | |
90 cv[i].y = cv[i].next_y; | |
91 } | |
92 } | |
93 // cout << id << ", " << sg->xyz[1] << endl; | |
94 } | |
95 set_old_vector(&cv[id], sg); | |
96 int p, n; | |
97 p = n = id; | |
98 if(p != 0) { | |
99 p--; | |
100 } | |
101 if(n != CHAIN_LEN - 1) { | |
102 n++; | |
103 } | |
104 sg->angle[2-(id%2)*2] | |
105 = 90 + atan((cv[p].next_y - cv[n].next_y) / (cv[p].next_x - cv[n].next_x)) * 180 / M_PI; | |
106 } | |
107 | |
108 | |
109 | |
110 static void | |
111 chain_old_collision(SceneGraphPtr node, void *sgroot_, int w, int h, SceneGraphPtr ocv) | |
112 { | |
113 //createSceneGraphFromProperty(cv, cv); | |
114 } | |
115 | |
116 | |
117 | |
118 static void | |
119 createSceneGraphFromProperty(SceneGraphPtr root, ChainPropertyPtr cv, Viewer *sgroot) | |
120 { | |
121 SceneGraphPtr node; | |
122 | |
123 for(int i = 0; i < CHAIN_LEN; i++) { | |
124 node = sgroot->createSceneGraph(cv[i].objname); | |
125 /** | |
126 * move, collision は spe で実行される, task taskID を持たせればよい | |
127 * property が 対応する taskID を持つので set_move_collision() は不要 | |
128 * set_task_propety(move_taskID, collision_taskID) のようなものを作る | |
129 */ | |
130 node->set_move_collision(chain_old_move, chain_old_collision); | |
131 //spe_move_collision(cv[i], chain_old_move, chain_old_collision); | |
132 node->id = cv[i].id; | |
133 node->angle[1] = cv[i].angle[1]; | |
134 set_old_vector(&cv[i], node); | |
135 cv[i].parent->addChild(node); | |
136 } | |
137 sgroot->setSceneData(root); | |
138 } | |
139 | |
140 | |
141 static void | |
142 set_properties(ChainPropertyPtr cv) | |
143 { | |
144 properties[0] = cv; | |
145 properties[1] = cv; | |
146 } | |
147 | |
148 MainLoopPtr | |
149 Chain::init(Viewer *sgroot, int w, int h) | |
150 { | |
151 SceneGraphPtr root, chain; | |
152 sgroot->createFromXMLfile("xml_file/chain.xml"); | |
153 | |
154 ChainProperty rcv; | |
155 init_chainold_vars(&rcv); | |
156 rcv.next_x = w / 2; | |
157 rcv.next_y = 0.0; | |
158 rcv.id = CHAIN_LEN; | |
159 | |
160 root = sgroot->createSceneGraph("CHAIN"); | |
161 root->set_move_collision(chain_old_move_ope, chain_old_collision); | |
162 set_old_vector(&rcv, root); | |
163 | |
164 chain = sgroot->createSceneGraph("CHAIN"); | |
165 chain->set_move_collision(chain_old_move, chain_old_collision); | |
166 | |
167 set_properties(cv); | |
168 | |
169 for(int i = 0; i < CHAIN_LEN; i++) { | |
170 init_chainold_vars(&cv[i]); | |
171 cv[i].x = 0; | |
172 cv[i].y = chain_width * i; | |
173 cv[i].angle[1] = -90 * (i % 2); | |
174 | |
175 chain->id = cv[i].id; | |
176 chain->angle[1] = cv[i].angle[1]; | |
177 set_old_vector(&cv[i], chain); | |
178 | |
179 cv[i].root = root; | |
180 cv[i].objname = "CHAIN"; | |
181 cv[i].id = i; | |
182 | |
183 cv[i].parent = root; | |
184 | |
185 } | |
186 | |
187 createSceneGraphFromProperty(root, cv, sgroot); | |
188 cv[0].can_move = FALSE; | |
189 //sgroot->setSceneData(root); | |
190 | |
191 return sgroot; | |
192 } | |
193 | |
194 extern Application * | |
195 application() { | |
196 return new Chain(); | |
197 } | |
198 | |
199 extern const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; | |
200 | |
201 extern int init(TaskManager *manager, int argc, char *argv[]); | |
202 extern void task_initialize(); | |
203 static void TMend(TaskManager *manager); | |
204 | |
205 int | |
206 TMmain(TaskManager *manager, int argc, char *argv[]) | |
207 { | |
208 task_initialize(); | |
209 manager->set_TMend(TMend); | |
210 return init(manager, argc, argv); | |
211 | |
212 } | |
213 | |
214 void | |
215 TMend(TaskManager *manager) | |
216 { | |
217 printf("test_nogl end\n"); | |
218 } | |
219 | |
220 /* end */ | |
221 |