annotate TaskManager/Test/test_render/chain.cpp @ 375:2ae25549dd6a draft

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