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