507
|
1 #include <iostream>
|
|
2 #include <math.h>
|
|
3 #include "SceneGraphRoot.h"
|
|
4 #include "SGList.h"
|
|
5 #include "SceneGraph.h"
|
|
6 #include "TaskManager.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, int screen_w, int screen_h)
|
|
46 {
|
|
47 Pad *pad = sgroot->getController();
|
|
48
|
|
49 if (pad->start.isHold()) {
|
|
50 cv[CHAIN_LEN-1].can_move = FALSE;
|
|
51 if (pad->left.isHold()) {
|
|
52 cv[CHAIN_LEN-1].x += -5.0;
|
|
53 } else if (pad->right.isHold()) {
|
|
54 cv[CHAIN_LEN-1].x += 5.0;
|
|
55 }
|
|
56
|
|
57 if (pad->up.isHold()) {
|
|
58 cv[CHAIN_LEN-1].y += -5.0;
|
|
59 } else if (pad->down.isHold()) {
|
|
60 cv[CHAIN_LEN-1].y += 5.0;
|
|
61 }
|
|
62 } else {
|
|
63 cv[CHAIN_LEN-1].can_move = TRUE;
|
|
64 }
|
|
65 }
|
|
66
|
|
67 void
|
|
68 chain_old_move(SceneGraphPtr sg, int w, int h)
|
|
69 {
|
|
70 int id = sg->id;
|
|
71 if(id == 0) {
|
|
72 for(int cnt = 0; cnt < 600; cnt++) {
|
|
73 for(int i = 0; i < CHAIN_LEN; i++) {
|
|
74 if(cv[i].can_move) {
|
|
75 double dx = cv[i-1].x - cv[i].x;
|
|
76 double dy = cv[i-1].y - cv[i].y;
|
|
77 double l = sqrt(dx * dx + dy * dy);
|
|
78 double a = k * (l - chain_width) / m;
|
|
79 double ax = a * dx / l;
|
|
80 double ay = a * dy / l;
|
|
81 if(i < CHAIN_LEN - 1) {
|
|
82 dx = cv[i+1].x - cv[i].x;
|
|
83 dy = cv[i+1].y - cv[i].y;
|
|
84 l = sqrt(dx * dx + dy * dy);
|
|
85 a = k * (l - chain_width) / m;
|
|
86 ax += a * dx / l;
|
|
87 ay += a * dy / l;
|
|
88 }
|
|
89 ay += g;
|
|
90 cv[i].vx *= safe;
|
|
91 cv[i].vy *= safe;
|
|
92 cv[i].next_vx = cv[i].vx + ax * dt;
|
|
93 cv[i].next_vy = cv[i].vy + ay * dt;
|
|
94 cv[i].next_x = cv[i].x + cv[i].vx * dt;
|
|
95 cv[i].next_y = cv[i].y + cv[i].vy * dt;
|
|
96 } else {
|
|
97 cv[i].next_x = cv[i].x;
|
|
98 cv[i].next_y = cv[i].y;
|
|
99 }
|
|
100 }
|
|
101 for(int i = 0; i < CHAIN_LEN; i++) {
|
|
102 cv[i].vx = cv[i].next_vx;
|
|
103 cv[i].vy = cv[i].next_vy;
|
|
104 cv[i].x = cv[i].next_x;
|
|
105 cv[i].y = cv[i].next_y;
|
|
106 }
|
|
107 }
|
|
108 // cout << id << ", " << sg->xyz[1] << endl;
|
|
109 }
|
|
110 set_old_vector(&cv[id], sg);
|
|
111 int p, n;
|
|
112 p = n = id;
|
|
113 if(p != 0) {
|
|
114 p--;
|
|
115 }
|
|
116 if(n != CHAIN_LEN - 1) {
|
|
117 n++;
|
|
118 }
|
|
119 sg->angle[2-(id%2)*2]
|
|
120 = 90 + atan((cv[p].next_y - cv[n].next_y) / (cv[p].next_x - cv[n].next_x)) * 180 / M_PI;
|
|
121 }
|
|
122
|
|
123 void
|
|
124 chain_old_collision(SceneGraphPtr sg, int w, int h, SceneGraphPtr osg)
|
|
125 {
|
|
126
|
|
127 }
|
|
128
|
|
129 void
|
|
130 chain_old_init(TaskManager *manager, int w, int h)
|
|
131 {
|
|
132 SceneGraphPtr root_old_chain, chain;
|
|
133 CHAIN_VARS rcv;
|
|
134
|
|
135 sgroot->createFromXMLfile(manager,"xml_file/chain.xml");
|
|
136
|
|
137 root_old_chain = sgroot->createSceneGraph(CHAIN);
|
|
138 root_old_chain->set_move_collision(chain_old_move_ope, chain_old_collision);
|
|
139 init_chainold_vars(&rcv);
|
|
140 rcv.next_x = w / 2;
|
|
141 rcv.next_y = 0.0;
|
|
142 set_old_vector(&rcv, root_old_chain);
|
|
143
|
|
144 for(int i = 0; i < CHAIN_LEN; i++) {
|
|
145 chain = sgroot->createSceneGraph(CHAIN);
|
|
146 chain->id = i;
|
|
147 init_chainold_vars(&cv[i]);
|
|
148 cv[i].x = 0;
|
|
149 cv[i].y = chain_width * i;
|
|
150 set_old_vector(&cv[i], chain);
|
|
151 chain->angle[1] = -90 * (i % 2);
|
|
152 chain->set_move_collision(chain_old_move, chain_old_collision);
|
|
153
|
|
154 root_old_chain->addChild(chain);
|
|
155 }
|
|
156 cv[0].can_move = FALSE;
|
|
157
|
|
158 sgroot->setSceneData(root_old_chain);
|
|
159 }
|