Mercurial > hg > Members > kono > Cerium
annotate Renderer/Test/ball_bound.cc @ 562:a5fda4e51498
name search
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 22 Oct 2009 22:22:31 +0900 |
parents | ec72b601b71f |
children | d0b8860c17f8 |
rev | line source |
---|---|
507 | 1 #include <math.h> |
2 #include <stdlib.h> | |
3 #include "SceneGraphRoot.h" | |
511 | 4 #include "MainLoop.h" |
5 #include "ball_bound.h" | |
507 | 6 |
524 | 7 |
507 | 8 // prototype |
9 static void ball_move(SceneGraphPtr node, int screen_w, int screen_h); | |
10 static void ball_collision(SceneGraphPtr node, int screen_w, int screen_h, SceneGraphPtr tree); | |
11 static void ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree); | |
12 | |
13 | |
14 static float vy = 0.0f; // y 方向速度 | |
15 static float dt = 1.0/1.0f; // frame rate | |
16 | |
17 static float e = -0.8f; // 反発係数 | |
18 static float g = 9.8f; // 重力加速度 | |
19 //static float v0 = 0.0f; // 初速は 0 | |
20 | |
21 static float h0; // 初期高さ | |
22 static float ball_radius = 100.0f; | |
23 | |
24 static float speed = 10.0f; | |
25 | |
26 static void | |
27 ball_move_idle2(SceneGraphPtr node, int screen_w, int screen_h) | |
28 { | |
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, int screen_w, int screen_h) | |
58 { | |
59 Pad *pad = sgroot->getController(); | |
60 | |
61 if (pad->circle.isPush()) { | |
62 node->set_move_collision(ball_move_idle2, ball_collision_idle); | |
63 time = 0; | |
64 } | |
65 | |
66 time++; | |
67 | |
68 if (time > 90) { | |
69 float w = (float)random(); | |
70 | |
71 w = fmodf(w, screen_w - ball_radius*2); | |
72 node->xyz[0] = w + ball_radius; | |
73 node->xyz[1] = h0; | |
74 node->set_move_collision(ball_move, ball_collision); | |
75 time = 0; | |
76 } | |
77 } | |
78 | |
79 static void | |
80 ball_move(SceneGraphPtr node, int screen_w, int screen_h) | |
81 { | |
82 vy += g * dt; | |
83 node->xyz[1] += vy * dt; | |
84 // node->xyz[0] += 10.0f; | |
85 } | |
86 | |
87 static void | |
88 ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree) | |
89 { | |
90 } | |
91 | |
92 static void | |
93 ball_collision(SceneGraphPtr node, int screen_w, int screen_h, | |
94 SceneGraphPtr tree) | |
95 { | |
96 if (node->xyz[1] > screen_h - ball_radius) { | |
97 node->xyz[1] = screen_h - ball_radius; | |
98 | |
99 vy *= e; | |
100 if (vy > -g && vy < 0) { | |
101 vy = 0.0; | |
102 node->set_move_collision(ball_move_idle, ball_collision_idle); | |
103 } | |
104 } | |
105 } | |
106 | |
510 | 107 MainLoopPtr |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
108 ball_bound::init(Viewer *sgroot, int screen_w, int screen_h) |
507 | 109 { |
110 SceneGraphPtr ball; | |
111 | |
112 // 固定した値で srandom すると、毎回同じ、random() 列が生成される | |
113 // random な値が欲しいなら、man random に方法が書いてあります。 | |
114 srandom(100); | |
115 | |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
116 sgroot->createFromXMLfile("xml_file/Ball.xml"); |
510 | 117 |
562 | 118 ball = sgroot->createSceneGraph("Ball"); |
507 | 119 ball->set_move_collision(ball_move, ball_collision); |
120 | |
121 h0 = screen_h/2; | |
122 h0 = -1000; | |
123 | |
124 ball->xyz[0] = screen_w/2; | |
125 //ball->xyz[0] = 0.0f; | |
126 ball->xyz[1] = h0; | |
127 ball->xyz[2] = 30.0f; | |
128 | |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
129 sgroot->setSceneData(ball); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
130 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
131 return sgroot; |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
132 } |
510 | 133 |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
134 extern Application * |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
135 application() { |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
136 return new ball_bound(); |
507 | 137 } |
510 | 138 |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
139 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
140 |
524 | 141 extern int init(TaskManager *manager, int argc, char *argv[]); |
142 extern void task_initialize(); | |
143 static void TMend(TaskManager *manager); | |
144 | |
145 int | |
146 TMmain(TaskManager *manager, int argc, char *argv[]) | |
147 { | |
148 task_initialize(); | |
149 manager->set_TMend(TMend); | |
150 return init(manager, argc, argv); | |
151 | |
152 } | |
153 | |
154 void | |
155 TMend(TaskManager *manager) | |
156 { | |
157 printf("test_nogl end\n"); | |
158 } | |
159 | |
510 | 160 /* end */ |