annotate TaskManager/Test/test_render/chain.cpp @ 378:bdeb7a73285b draft

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