Mercurial > hg > Members > kono > Cerium
annotate Renderer/Test/chain_old.cc @ 671:f42b303044f7
fix several Renderer/Test with (void*)sgroot.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 05 Dec 2009 17:26:58 +0900 |
parents | 987380738a50 |
children | 4dc02d3e98bb |
rev | line source |
---|---|
507 | 1 #include <iostream> |
2 #include <math.h> | |
3 #include "SceneGraphRoot.h" | |
4 #include "SceneGraph.h" | |
5 #include "TaskManager.h" | |
585 | 6 #include "Chain.h" |
507 | 7 #include "Func.h" |
8 | |
9 | |
10 #define FALSE 0 | |
11 #define TRUE !FALSE | |
12 #define CHAIN_LEN 50 | |
13 | |
14 static double m = 100.0; | |
15 static double k = 7000.0; | |
16 static double g = 9.8; | |
17 static double dt = 0.003; | |
18 static double chain_width = 10; | |
19 static double safe = 0.995; | |
20 | |
21 typedef struct { | |
22 double x, y, next_x, next_y; | |
23 double vx, vy, next_vx, next_vy; | |
24 int can_move; | |
25 } CHAIN_VARS; | |
26 | |
27 CHAIN_VARS cv[CHAIN_LEN]; | |
28 | |
29 void | |
30 init_chainold_vars(CHAIN_VARS *cv) { | |
31 cv->x = 0, cv->y = 0, cv->next_x = 0, cv->next_y = 0; | |
32 cv->vx = 0, cv->vy = 0, cv->next_vx = 0, cv->next_vy = 0; | |
33 cv->can_move = TRUE; | |
34 } | |
35 | |
36 void | |
37 set_old_vector(CHAIN_VARS *cv, SceneGraphPtr sg) { | |
38 sg->xyz[0] = (float)cv->next_x; | |
39 sg->xyz[1] = (float)cv->next_y; | |
40 sg->xyz[2] = 0.0f; | |
41 } | |
42 | |
43 | |
44 static void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
45 chain_old_move_ope(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) |
507 | 46 { |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
47 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; |
507 | 48 Pad *pad = sgroot->getController(); |
49 | |
50 if (pad->start.isHold()) { | |
51 cv[CHAIN_LEN-1].can_move = FALSE; | |
52 if (pad->left.isHold()) { | |
53 cv[CHAIN_LEN-1].x += -5.0; | |
54 } else if (pad->right.isHold()) { | |
55 cv[CHAIN_LEN-1].x += 5.0; | |
56 } | |
57 | |
58 if (pad->up.isHold()) { | |
59 cv[CHAIN_LEN-1].y += -5.0; | |
60 } else if (pad->down.isHold()) { | |
61 cv[CHAIN_LEN-1].y += 5.0; | |
62 } | |
63 } else { | |
64 cv[CHAIN_LEN-1].can_move = TRUE; | |
65 } | |
66 } | |
67 | |
68 void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
69 chain_old_move(SceneGraphPtr sg, void *sgroot_, int w, int h) |
507 | 70 { |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
71 |
507 | 72 int id = sg->id; |
73 if(id == 0) { | |
74 for(int cnt = 0; cnt < 600; cnt++) { | |
75 for(int i = 0; i < CHAIN_LEN; i++) { | |
76 if(cv[i].can_move) { | |
77 double dx = cv[i-1].x - cv[i].x; | |
78 double dy = cv[i-1].y - cv[i].y; | |
79 double l = sqrt(dx * dx + dy * dy); | |
80 double a = k * (l - chain_width) / m; | |
81 double ax = a * dx / l; | |
82 double ay = a * dy / l; | |
83 if(i < CHAIN_LEN - 1) { | |
84 dx = cv[i+1].x - cv[i].x; | |
85 dy = cv[i+1].y - cv[i].y; | |
86 l = sqrt(dx * dx + dy * dy); | |
87 a = k * (l - chain_width) / m; | |
88 ax += a * dx / l; | |
89 ay += a * dy / l; | |
90 } | |
91 ay += g; | |
92 cv[i].vx *= safe; | |
93 cv[i].vy *= safe; | |
94 cv[i].next_vx = cv[i].vx + ax * dt; | |
95 cv[i].next_vy = cv[i].vy + ay * dt; | |
96 cv[i].next_x = cv[i].x + cv[i].vx * dt; | |
97 cv[i].next_y = cv[i].y + cv[i].vy * dt; | |
98 } else { | |
99 cv[i].next_x = cv[i].x; | |
100 cv[i].next_y = cv[i].y; | |
101 } | |
102 } | |
103 for(int i = 0; i < CHAIN_LEN; i++) { | |
104 cv[i].vx = cv[i].next_vx; | |
105 cv[i].vy = cv[i].next_vy; | |
106 cv[i].x = cv[i].next_x; | |
107 cv[i].y = cv[i].next_y; | |
108 } | |
109 } | |
110 // cout << id << ", " << sg->xyz[1] << endl; | |
111 } | |
112 set_old_vector(&cv[id], sg); | |
113 int p, n; | |
114 p = n = id; | |
115 if(p != 0) { | |
116 p--; | |
117 } | |
118 if(n != CHAIN_LEN - 1) { | |
119 n++; | |
120 } | |
121 sg->angle[2-(id%2)*2] | |
122 = 90 + atan((cv[p].next_y - cv[n].next_y) / (cv[p].next_x - cv[n].next_x)) * 180 / M_PI; | |
123 } | |
124 | |
125 void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
126 chain_old_collision(SceneGraphPtr sg, void *sgroot_, int w, int h, SceneGraphPtr osg) |
507 | 127 { |
128 | |
129 } | |
130 | |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
131 MainLoopPtr |
585 | 132 Chain::init(Viewer *sgroot, int w, int h) |
507 | 133 { |
134 SceneGraphPtr root_old_chain, chain; | |
135 CHAIN_VARS rcv; | |
136 | |
585 | 137 sgroot->createFromXMLfile("xml_file/chain.xml"); |
507 | 138 |
585 | 139 root_old_chain = sgroot->createSceneGraph("CHAIN"); |
507 | 140 root_old_chain->set_move_collision(chain_old_move_ope, chain_old_collision); |
141 init_chainold_vars(&rcv); | |
142 rcv.next_x = w / 2; | |
143 rcv.next_y = 0.0; | |
144 set_old_vector(&rcv, root_old_chain); | |
145 | |
146 for(int i = 0; i < CHAIN_LEN; i++) { | |
585 | 147 chain = sgroot->createSceneGraph("CHAIN"); |
507 | 148 chain->id = i; |
149 init_chainold_vars(&cv[i]); | |
150 cv[i].x = 0; | |
151 cv[i].y = chain_width * i; | |
152 set_old_vector(&cv[i], chain); | |
153 chain->angle[1] = -90 * (i % 2); | |
154 chain->set_move_collision(chain_old_move, chain_old_collision); | |
155 | |
156 root_old_chain->addChild(chain); | |
157 } | |
158 cv[0].can_move = FALSE; | |
159 | |
160 sgroot->setSceneData(root_old_chain); | |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
161 return sgroot; |
507 | 162 } |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
163 |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
164 MainLoopPtr |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
165 Chain::init_only_sg(SgChange *sgchange, int w, int h) |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
166 { |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
167 return sgchange; |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
168 } |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
169 |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
585
diff
changeset
|
170 |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
171 extern Application * |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
172 application() { |
585 | 173 return new Chain(); |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
174 } |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
175 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
176 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
177 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
178 extern int init(TaskManager *manager, int argc, char *argv[]); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
179 extern void task_initialize(); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
180 static void TMend(TaskManager *manager); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
181 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
182 int |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
183 TMmain(TaskManager *manager, int argc, char *argv[]) |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
184 { |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
185 task_initialize(); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
186 manager->set_TMend(TMend); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
187 return init(manager, argc, argv); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
188 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
189 } |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
190 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
191 void |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
192 TMend(TaskManager *manager) |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
193 { |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
194 printf("test_nogl end\n"); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
195 } |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
196 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
197 /* end */ |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
198 |