Mercurial > hg > Game > Cerium
annotate TaskManager/Test/test_render/chain.cpp @ 388:3d1e86396d16 draft
MemHash (OS X version)
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 06 Aug 2009 23:30:26 +0900 |
parents | 1c3f84a6f5d0 |
children | 208ba3551474 |
rev | line source |
---|---|
388
3d1e86396d16
MemHash (OS X version)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
382
diff
changeset
|
1 #include <iostream> |
375 | 2 #include <math.h> |
3 #include "SceneGraphRoot.h" | |
4 #include "SGList.h" | |
5 | |
6 #define FALSE 0 | |
7 #define TRUE !FALSE | |
382 | 8 #define CHAIN_LEN 50 |
375 | 9 |
10 static double m = 100.0; | |
382 | 11 static double k = 7000.0; |
375 | 12 static double g = 9.8; |
382 | 13 static double dt = 0.003; |
14 static double chain_width = 10; | |
375 | 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()) { | |
382 | 48 cv[CHAIN_LEN-1].x += -5.0; |
375 | 49 } else if (pad->right.isHold()) { |
382 | 50 cv[CHAIN_LEN-1].x += 5.0; |
375 | 51 } |
52 | |
53 if (pad->up.isHold()) { | |
382 | 54 cv[CHAIN_LEN-1].y += -5.0; |
375 | 55 } else if (pad->down.isHold()) { |
382 | 56 cv[CHAIN_LEN-1].y += 5.0; |
375 | 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; | |
382 | 67 if(id == 0) { |
68 for(int cnt = 0; cnt < 600; cnt++) { | |
69 for(int i = 0; i < CHAIN_LEN; i++) { | |
70 if(cv[i].can_move) { | |
71 double dx = cv[i-1].x - cv[i].x; | |
72 double dy = cv[i-1].y - cv[i].y; | |
73 double l = sqrt(dx * dx + dy * dy); | |
74 double a = k * (l - chain_width) / m; | |
75 double ax = a * dx / l; | |
76 double ay = a * dy / l; | |
77 if(i < CHAIN_LEN - 1) { | |
78 dx = cv[i+1].x - cv[i].x; | |
79 dy = cv[i+1].y - cv[i].y; | |
80 l = sqrt(dx * dx + dy * dy); | |
81 a = k * (l - chain_width) / m; | |
82 ax += a * dx / l; | |
83 ay += a * dy / l; | |
84 } | |
85 ay += g; | |
86 cv[i].vx *= safe; | |
87 cv[i].vy *= safe; | |
88 cv[i].next_vx = cv[i].vx + ax * dt; | |
89 cv[i].next_vy = cv[i].vy + ay * dt; | |
90 cv[i].next_x = cv[i].x + cv[i].vx * dt; | |
91 cv[i].next_y = cv[i].y + cv[i].vy * dt; | |
92 } else { | |
93 cv[i].next_x = cv[i].x; | |
94 cv[i].next_y = cv[i].y; | |
95 } | |
96 } | |
97 for(int i = 0; i < CHAIN_LEN; i++) { | |
98 cv[i].vx = cv[i].next_vx; | |
99 cv[i].vy = cv[i].next_vy; | |
100 cv[i].x = cv[i].next_x; | |
101 cv[i].y = cv[i].next_y; | |
102 } | |
375 | 103 } |
382 | 104 // cout << id << ", " << sg->xyz[1] << endl; |
375 | 105 } |
106 set_vector(&cv[id], sg); | |
382 | 107 int p, n; |
108 p = n = id; | |
109 if(p != 0) { | |
110 p--; | |
375 | 111 } |
382 | 112 if(n != CHAIN_LEN - 1) { |
113 n++; | |
114 } | |
115 sg->angle[2-(id%2)*2] | |
116 = 90 + atan((cv[p].next_y - cv[n].next_y) / (cv[p].next_x - cv[n].next_x)) * 180 / M_PI; | |
375 | 117 } |
118 | |
119 void | |
120 chain_collision(SceneGraphPtr sg, int w, int h, SceneGraphPtr osg) | |
121 { | |
122 | |
123 } | |
124 | |
125 void | |
126 chain_init(int w, int h) | |
127 { | |
128 SceneGraphPtr root_chain, chain; | |
129 CHAIN_VARS rcv; | |
130 | |
131 sgroot->createFromXMLfile("xml_file/chain.xml"); | |
132 | |
133 root_chain = sgroot->createSceneGraph(CHAIN); | |
134 root_chain->set_move_collision(chain_move_ope, chain_collision); | |
135 init_chain_vars(&rcv); | |
136 rcv.next_x = w / 2; | |
137 rcv.next_y = 0.0; | |
138 set_vector(&rcv, root_chain); | |
139 | |
140 for(int i = 0; i < CHAIN_LEN; i++) { | |
141 chain = sgroot->createSceneGraph(CHAIN); | |
142 chain->id = i; | |
143 init_chain_vars(&cv[i]); | |
144 cv[i].x = 0; | |
145 cv[i].y = chain_width * i; | |
146 set_vector(&cv[i], chain); | |
382 | 147 chain->angle[1] = -90 * (i % 2); |
375 | 148 chain->set_move_collision(chain_move, chain_collision); |
149 | |
150 root_chain->addChild(chain); | |
151 } | |
382 | 152 cv[0].can_move = FALSE; |
375 | 153 |
154 sgroot->setSceneData(root_chain); | |
155 } |