375
|
1 #include <math.h>
|
|
2 #include "SceneGraphRoot.h"
|
|
3 #include "SGList.h"
|
|
4
|
|
5 #define FALSE 0
|
|
6 #define TRUE !FALSE
|
|
7 #define CHAIN_LEN 30
|
|
8
|
|
9 static double m = 100.0;
|
|
10 static double k = 1000.0;
|
|
11 static double g = 9.8;
|
|
12 static double dt = 0.01;
|
|
13 static double chain_width = 5;
|
|
14 static double safe = 0.995;
|
|
15
|
|
16 typedef struct {
|
|
17 double x, y, next_x, next_y;
|
|
18 double vx, vy, next_vx, next_vy;
|
|
19 int can_move;
|
|
20 } CHAIN_VARS;
|
|
21
|
|
22 CHAIN_VARS cv[CHAIN_LEN];
|
|
23
|
|
24 void
|
|
25 init_chain_vars(CHAIN_VARS *cv) {
|
|
26 cv->x = 0, cv->y = 0, cv->next_x = 0, cv->next_y = 0;
|
|
27 cv->vx = 0, cv->vy = 0, cv->next_vx = 0, cv->next_vy = 0;
|
|
28 cv->can_move = TRUE;
|
|
29 }
|
|
30
|
|
31 void
|
|
32 set_vector(CHAIN_VARS *cv, SceneGraphPtr sg) {
|
|
33 sg->xyz[0] = (float)cv->next_x;
|
|
34 sg->xyz[1] = (float)cv->next_y;
|
|
35 sg->xyz[2] = 0.0f;
|
|
36 }
|
|
37
|
|
38
|
|
39 static void
|
|
40 chain_move_ope(SceneGraphPtr node, int screen_w, int screen_h)
|
|
41 {
|
|
42 Pad *pad = sgroot->getController();
|
|
43
|
|
44 if (pad->circle.isHold()) {
|
|
45 cv[CHAIN_LEN-1].can_move = FALSE;
|
|
46 if (pad->left.isHold()) {
|
|
47 cv[CHAIN_LEN-1].x += -1.0;
|
|
48 } else if (pad->right.isHold()) {
|
|
49 cv[CHAIN_LEN-1].x += 1.0;
|
|
50 }
|
|
51
|
|
52 if (pad->up.isHold()) {
|
|
53 cv[CHAIN_LEN-1].y += -1.0;
|
|
54 } else if (pad->down.isHold()) {
|
|
55 cv[CHAIN_LEN-1].y += 1.0;
|
|
56 }
|
|
57 } else {
|
|
58 cv[CHAIN_LEN-1].can_move = TRUE;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 void
|
|
63 chain_move(SceneGraphPtr sg, int w, int h)
|
|
64 {
|
|
65 int id = sg->id;
|
|
66 if(cv[id].can_move) {
|
|
67 double dx = cv[id-1].x - cv[id].x;
|
|
68 double dy = cv[id-1].y - cv[id].y;
|
|
69 double l = sqrt(dx * dx + dy * dy);
|
|
70 double a = k * (l - chain_width) / m;
|
|
71 double ax = a * dx / l;
|
|
72 double ay = a * dy / l;
|
|
73 if(id < CHAIN_LEN - 1) {
|
|
74 dx = cv[id+1].x - cv[id].x;
|
|
75 dy = cv[id+1].y - cv[id].y;
|
|
76 l = sqrt(dx * dx + dy * dy);
|
|
77 a = k * (l - chain_width) / m;
|
|
78 ax += a * dx / l;
|
|
79 ay += a * dy / l;
|
|
80 }
|
|
81 ay += g;
|
|
82 cv[id].vx *= safe;
|
|
83 cv[id].vy *= safe;
|
|
84 cv[id].next_vx = cv[id].vx + ax * dt;
|
|
85 cv[id].next_vy = cv[id].vy + ay * dt;
|
|
86 cv[id].next_x = cv[id].x + cv[id].vx * dt;
|
|
87 cv[id].next_y = cv[id].y + cv[id].vy * dt;
|
|
88 } else {
|
|
89 cv[id].next_x = cv[id].x;
|
|
90 cv[id].next_y = cv[id].y;
|
|
91 }
|
|
92 set_vector(&cv[id], sg);
|
|
93 if(id == CHAIN_LEN -1) {
|
|
94 for(int i = 0; i < CHAIN_LEN; i++) {
|
|
95 cv[i].vx = cv[i].next_vx;
|
|
96 cv[i].vy = cv[i].next_vy;
|
|
97 cv[i].x = cv[i].next_x;
|
|
98 cv[i].y = cv[i].next_y;
|
|
99 }
|
|
100 }
|
|
101 // cout << id << ", " << sg->xyz[1] << endl;
|
|
102 }
|
|
103
|
|
104 void
|
|
105 chain_collision(SceneGraphPtr sg, int w, int h, SceneGraphPtr osg)
|
|
106 {
|
|
107
|
|
108 }
|
|
109
|
|
110 void
|
|
111 chain_init(int w, int h)
|
|
112 {
|
|
113 SceneGraphPtr root_chain, chain;
|
|
114 CHAIN_VARS rcv;
|
|
115
|
|
116 sgroot->createFromXMLfile("xml_file/chain.xml");
|
|
117
|
|
118 root_chain = sgroot->createSceneGraph(CHAIN);
|
|
119 root_chain->set_move_collision(chain_move_ope, chain_collision);
|
|
120 init_chain_vars(&rcv);
|
|
121 rcv.next_x = w / 2;
|
|
122 rcv.next_y = 0.0;
|
|
123 set_vector(&rcv, root_chain);
|
|
124
|
|
125 for(int i = 0; i < CHAIN_LEN; i++) {
|
|
126 chain = sgroot->createSceneGraph(CHAIN);
|
|
127 chain->id = i;
|
|
128 init_chain_vars(&cv[i]);
|
|
129 if(i == 0)
|
|
130 cv[i].can_move = FALSE;
|
|
131 cv[i].x = 0;
|
|
132 cv[i].y = chain_width * i;
|
|
133 set_vector(&cv[i], chain);
|
|
134 chain->angle[1] = 90 * (i % 2);
|
|
135 chain->set_move_collision(chain_move, chain_collision);
|
|
136
|
|
137 root_chain->addChild(chain);
|
|
138 }
|
|
139
|
|
140 sgroot->setSceneData(root_chain);
|
|
141 }
|