Mercurial > hg > Members > kono > Cerium
annotate Renderer/Test/ball_bound.cc @ 557:ec72b601b71f
fix examlples (on going)
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 22 Oct 2009 17:34:12 +0900 |
parents | 0f180d46db89 |
children | a5fda4e51498 |
rev | line source |
---|---|
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 | |
510 | 108 MainLoopPtr |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
109 ball_bound::init(Viewer *sgroot, int screen_w, int screen_h) |
507 | 110 { |
111 SceneGraphPtr ball; | |
112 | |
113 // 固定した値で srandom すると、毎回同じ、random() 列が生成される | |
114 // random な値が欲しいなら、man random に方法が書いてあります。 | |
115 srandom(100); | |
116 | |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
117 sgroot->createFromXMLfile("xml_file/Ball.xml"); |
510 | 118 |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
119 ball = sgroot->createSceneGraph(Ball); |
507 | 120 ball->set_move_collision(ball_move, ball_collision); |
121 | |
122 h0 = screen_h/2; | |
123 h0 = -1000; | |
124 | |
125 ball->xyz[0] = screen_w/2; | |
126 //ball->xyz[0] = 0.0f; | |
127 ball->xyz[1] = h0; | |
128 ball->xyz[2] = 30.0f; | |
129 | |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
130 sgroot->setSceneData(ball); |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
131 |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
132 return sgroot; |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
133 } |
510 | 134 |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
135 extern Application * |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
136 application() { |
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
137 return new ball_bound(); |
507 | 138 } |
510 | 139 |
557
ec72b601b71f
fix examlples (on going)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
524
diff
changeset
|
140 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
|
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 */ |