0
|
1 #include <iostream>
|
|
2 #include <math.h>
|
|
3 #include "SceneGraphRoot.h"
|
|
4 #include "SceneGraph.h"
|
|
5 #include "TaskManager.h"
|
|
6 #include "Chain.h"
|
|
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
|
|
45 chain_old_move_ope(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
|
|
46 {
|
|
47 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
|
|
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
|
|
69 chain_old_move(SceneGraphPtr sg, void *sgroot_, int w, int h)
|
|
70 {
|
|
71
|
|
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
|
|
126 chain_old_collision(SceneGraphPtr sg, void *sgroot_, int w, int h, SceneGraphPtr osg)
|
|
127 {
|
|
128
|
|
129 }
|
|
130
|
|
131 MainLoopPtr
|
|
132 Chain::init(Viewer *sgroot, int w, int h)
|
|
133 {
|
|
134 SceneGraphPtr root_old_chain, chain;
|
|
135 CHAIN_VARS rcv;
|
|
136
|
|
137 sgroot->createFromXMLfile("xml_file/chain.xml");
|
|
138
|
|
139 root_old_chain = sgroot->createSceneGraph("CHAIN");
|
|
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++) {
|
|
147 chain = sgroot->createSceneGraph("CHAIN");
|
|
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);
|
|
161 return sgroot;
|
|
162 }
|
|
163
|
|
164 extern Application *
|
|
165 application() {
|
|
166 return new Chain();
|
|
167 }
|
|
168
|
|
169 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n";
|
|
170
|
|
171 extern int init(TaskManager *manager, int argc, char *argv[]);
|
|
172 extern void task_initialize();
|
|
173 static void TMend(TaskManager *manager);
|
|
174
|
|
175 int
|
|
176 TMmain(TaskManager *manager, int argc, char *argv[])
|
|
177 {
|
|
178 task_initialize();
|
|
179 manager->set_TMend(TMend);
|
|
180 return init(manager, argc, argv);
|
|
181
|
|
182 }
|
|
183
|
|
184 void
|
|
185 TMend(TaskManager *manager)
|
|
186 {
|
|
187 printf("test_nogl end\n");
|
|
188 }
|
|
189
|
|
190 /* end */
|
|
191
|