507
|
1 #include <math.h>
|
|
2 #include <stdlib.h>
|
|
3 #include "SceneGraphRoot.h"
|
511
|
4 #include "MainLoop.h"
|
512
|
5 #include "xml_file/Ball.xml.h"
|
511
|
6 #include "ball_bound.h"
|
507
|
7
|
524
|
8
|
507
|
9 // prototype
|
|
10 static void ball_move(SceneGraphPtr node, int screen_w, int screen_h);
|
|
11 static void ball_collision(SceneGraphPtr node, int screen_w, int screen_h, SceneGraphPtr tree);
|
|
12 static void ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree);
|
|
13
|
|
14
|
|
15 static float vy = 0.0f; // y 方向速度
|
|
16 static float dt = 1.0/1.0f; // frame rate
|
|
17
|
|
18 static float e = -0.8f; // 反発係数
|
|
19 static float g = 9.8f; // 重力加速度
|
|
20 //static float v0 = 0.0f; // 初速は 0
|
|
21
|
|
22 static float h0; // 初期高さ
|
|
23 static float ball_radius = 100.0f;
|
|
24
|
|
25 static float speed = 10.0f;
|
|
26
|
|
27 static void
|
|
28 ball_move_idle2(SceneGraphPtr node, int screen_w, int screen_h)
|
|
29 {
|
|
30 Pad *pad = sgroot->getController();
|
|
31
|
|
32 if (pad->circle.isHold()) {
|
|
33 if (pad->left.isHold()) {
|
|
34 node->xyz[0] -= speed;
|
|
35 if(node->xyz[0] < ball_radius)
|
|
36 node->xyz[0] = ball_radius;
|
|
37 } else if (pad->right.isHold()) {
|
|
38 node->xyz[0] += speed;
|
|
39 if(node->xyz[0] > screen_w - ball_radius)
|
|
40 node->xyz[0] = screen_w - ball_radius;
|
|
41 }
|
|
42
|
|
43 if (pad->up.isHold()) {
|
|
44 node->xyz[1] -= speed;
|
|
45 } else if (pad->down.isHold()) {
|
|
46 node->xyz[1] += speed;
|
|
47 if(node->xyz[1] > screen_h - ball_radius)
|
|
48 node->xyz[1] = screen_h - ball_radius;
|
|
49 }
|
|
50 } else {
|
|
51 node->set_move_collision(ball_move, ball_collision);
|
|
52 }
|
|
53 }
|
|
54
|
|
55 static int time = 0;
|
|
56
|
|
57 static void
|
|
58 ball_move_idle(SceneGraphPtr node, int screen_w, int screen_h)
|
|
59 {
|
|
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, 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, int w, int h, SceneGraphPtr tree)
|
|
90 {
|
|
91 }
|
|
92
|
|
93 static void
|
|
94 ball_collision(SceneGraphPtr node, 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
|
512
|
108 extern Application *
|
|
109 application() {
|
|
110 return new ball_bound();
|
|
111 }
|
|
112
|
524
|
113 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n";
|
|
114
|
510
|
115 MainLoopPtr
|
|
116 ball_bound::init(Viewer *viewer, int screen_w, int screen_h)
|
507
|
117 {
|
|
118 SceneGraphPtr ball;
|
|
119
|
|
120 // 固定した値で srandom すると、毎回同じ、random() 列が生成される
|
|
121 // random な値が欲しいなら、man random に方法が書いてあります。
|
|
122 srandom(100);
|
|
123
|
510
|
124 viewer->createFromXMLfile("xml_file/Ball.xml");
|
|
125
|
|
126 ball = viewer->createSceneGraph(Ball);
|
507
|
127 ball->set_move_collision(ball_move, ball_collision);
|
|
128
|
|
129 h0 = screen_h/2;
|
|
130 h0 = -1000;
|
|
131
|
|
132 ball->xyz[0] = screen_w/2;
|
|
133 //ball->xyz[0] = 0.0f;
|
|
134 ball->xyz[1] = h0;
|
|
135 ball->xyz[2] = 30.0f;
|
|
136
|
510
|
137 viewer->setSceneData(ball);
|
|
138
|
|
139 return viewer;
|
507
|
140 }
|
510
|
141
|
524
|
142 extern int init(TaskManager *manager, int argc, char *argv[]);
|
|
143 extern void task_initialize();
|
|
144 static void TMend(TaskManager *manager);
|
|
145
|
|
146 int
|
|
147 TMmain(TaskManager *manager, int argc, char *argv[])
|
|
148 {
|
|
149 task_initialize();
|
|
150 manager->set_TMend(TMend);
|
|
151 return init(manager, argc, argv);
|
|
152
|
|
153 }
|
|
154
|
|
155 void
|
|
156 TMend(TaskManager *manager)
|
|
157 {
|
|
158 printf("test_nogl end\n");
|
|
159 }
|
|
160
|
510
|
161 /* end */
|