3
|
1 #include <math.h>
|
|
2 #include <stdlib.h>
|
|
3 #include "SceneGraphRoot.h"
|
|
4 #include "MainLoop.h"
|
|
5 #include "walk.h"
|
|
6
|
|
7 // prototype
|
|
8 static void arm_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
|
|
9 static void ball_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h);
|
|
10 static void ball_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree);
|
|
11 static void ball_collision_idle(SceneGraphPtr, void *sgroot_, int w, int h, SceneGraphPtr tree);
|
|
12
|
|
13 static float vy = 0.0f; // y 方向速度
|
|
14 static float dt = 1.0/1.0f; // frame rate
|
|
15
|
|
16 static float e = -0.8f; // 反発係数
|
|
17 static float g = 9.8f; // 重力加速度
|
|
18 //static float v0 = 0.0f; // 初速は 0
|
|
19
|
|
20 static float h0; // 初期高さ
|
|
21 static float ball_radius = 100.0f;
|
|
22
|
|
23 static float speed = 10.0f;
|
|
24
|
|
25 static void
|
|
26 ball_move_idle2(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
|
|
27 {
|
|
28 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
|
|
29 Pad *pad = sgroot->getController();
|
|
30
|
|
31 if (pad->circle.isHold()) {
|
|
32 if (pad->left.isHold()) {
|
|
33 node->xyz[0] -= speed;
|
|
34 if(node->xyz[0] < ball_radius)
|
|
35 node->xyz[0] = ball_radius;
|
|
36 } else if (pad->right.isHold()) {
|
|
37 node->xyz[0] += speed;
|
|
38 if(node->xyz[0] > screen_w - ball_radius)
|
|
39 node->xyz[0] = screen_w - ball_radius;
|
|
40 }
|
|
41
|
|
42 if (pad->up.isHold()) {
|
|
43 node->xyz[1] -= speed;
|
|
44 } else if (pad->down.isHold()) {
|
|
45 node->xyz[1] += speed;
|
|
46 if(node->xyz[1] > screen_h - ball_radius)
|
|
47 node->xyz[1] = screen_h - ball_radius;
|
|
48 }
|
|
49 } else {
|
|
50 node->set_move_collision(ball_move, ball_collision);
|
|
51 }
|
|
52 }
|
|
53
|
|
54 static int time = 0;
|
|
55
|
|
56 static void
|
|
57 ball_move_idle(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
|
|
58 {
|
|
59 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
|
|
60 Pad *pad = sgroot->getController();
|
|
61
|
|
62 if (pad->circle.isPush()) {
|
|
63 node->set_move_collision(ball_move_idle2, ball_collision_idle);
|
|
64 time = 0;
|
|
65 }
|
|
66
|
|
67 time++;
|
|
68
|
|
69 if (time > 90) {
|
|
70 float w = (float)random();
|
|
71
|
|
72 w = fmodf(w, screen_w - ball_radius*2);
|
|
73 node->xyz[0] = w + ball_radius;
|
|
74 node->xyz[1] = h0;
|
|
75 node->set_move_collision(ball_move, ball_collision);
|
|
76 time = 0;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 static void
|
|
81 ball_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
|
|
82 {
|
|
83 vy += g * dt;
|
|
84 node->xyz[1] += vy * dt;
|
|
85 // node->xyz[0] += 10.0f;
|
|
86 }
|
|
87
|
|
88 static void
|
|
89 ball_collision_idle(SceneGraphPtr, void *sgroot_, int w, int h, SceneGraphPtr tree)
|
|
90 {
|
|
91 }
|
|
92
|
|
93 static void
|
|
94 ball_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h,
|
|
95 SceneGraphPtr tree)
|
|
96 {
|
|
97 if (node->xyz[1] > screen_h - ball_radius) {
|
|
98 node->xyz[1] = screen_h - ball_radius;
|
|
99
|
|
100 vy *= e;
|
|
101 if (vy > -g && vy < 0) {
|
|
102 vy = 0.0;
|
|
103 node->set_move_collision(ball_move_idle, ball_collision_idle);
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107
|
|
108 MainLoopPtr
|
|
109 walk::init(Viewer *sgroot, int screen_w, int screen_h)
|
|
110 {
|
|
111 SceneGraphPtr root, body, head, r_leg_1, l_leg_1, r_leg_2, l_leg_2, r_arm_1, l_arm_1, r_arm_2, l_arm_2;
|
|
112
|
|
113 // 固定した値で srandom すると、毎回同じ、random() 列が生成される
|
|
114 // random な値が欲しいなら、man random に方法が書いてあります。
|
|
115 srandom(100);
|
|
116
|
|
117 sgroot->createFromXMLfile("xml_file/tetsujin_3.xml");
|
|
118
|
|
119 root = sgroot->createSceneGraph();
|
|
120 body = sgroot->createSceneGraph("body");
|
|
121 //body->set_move_collision(ball_move, ball_collision);
|
|
122
|
|
123 head = sgroot->createSceneGraph("head");
|
|
124 r_leg_1 = sgroot->createSceneGraph("right_leg_1");
|
|
125 l_leg_1 = sgroot->createSceneGraph("left_leg_1");
|
|
126 r_leg_2 = sgroot->createSceneGraph("right_leg_2");
|
|
127 l_leg_2 = sgroot->createSceneGraph("left_leg_2");
|
|
128 r_arm_1 = sgroot->createSceneGraph("right_arm_1");
|
|
129 l_arm_1 = sgroot->createSceneGraph("left_arm_1");
|
|
130 r_arm_2 = sgroot->createSceneGraph("right_arm_2");
|
|
131 l_arm_2 = sgroot->createSceneGraph("left_arm_2");
|
|
132
|
|
133 root->addChild(body);
|
|
134 body->addChild(head);
|
|
135 r_leg_2->addChild(r_leg_1);
|
|
136 l_leg_2->addChild(l_leg_1);
|
|
137 body->addChild(r_leg_2);
|
|
138 body->addChild(l_leg_2);
|
|
139 r_arm_2->addChild(r_arm_1);
|
|
140 l_arm_2->addChild(l_arm_1);
|
|
141 body->addChild(r_arm_2);
|
|
142 body->addChild(l_arm_2);
|
|
143
|
|
144 //h0 = screen_h/2;
|
|
145 //h0 = -1000;
|
|
146
|
|
147 body->xyz[0] = screen_w/2;
|
|
148 //ball->xyz[0] = 0.0f;
|
|
149 body->xyz[1] = screen_h/2 + 25;
|
|
150 body->xyz[2] = 0.0f;
|
|
151 body->angle[0] = -90.0f;
|
|
152
|
|
153 r_arm_1->angle[1] = -45.0f;
|
|
154 r_arm_1->xyz[0] = 0;
|
|
155 r_arm_1->xyz[1] = 0;
|
|
156 r_arm_1->xyz[2] = 0;
|
|
157
|
|
158 sgroot->setSceneData(root);
|
|
159
|
|
160 return sgroot;
|
|
161 }
|
|
162
|
|
163 MainLoopPtr
|
|
164 walk::init_only_sg(SgChange *sgroot, int screen_w, int screen_h)
|
|
165 {
|
|
166 return sgroot;
|
|
167 }
|
|
168
|
|
169 extern Application *
|
|
170 application() {
|
|
171 return new walk();
|
|
172 }
|
|
173
|
|
174 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n";
|
|
175
|
|
176 extern int init(TaskManager *manager, int argc, char *argv[]);
|
|
177 extern void task_initialize();
|
|
178 static void TMend(TaskManager *manager);
|
|
179
|
|
180 int
|
|
181 TMmain(TaskManager *manager, int argc, char *argv[])
|
|
182 {
|
|
183 task_initialize();
|
|
184 manager->set_TMend(TMend);
|
|
185 return init(manager, argc, argv);
|
|
186
|
|
187 }
|
|
188
|
|
189 void
|
|
190 TMend(TaskManager *manager)
|
|
191 {
|
|
192 printf("test_nogl end\n");
|
|
193 }
|
|
194
|
|
195 /* end */
|