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
|
|
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
|
512
|
107 extern Application *
|
|
108 application() {
|
|
109 return new ball_bound();
|
|
110 }
|
|
111
|
510
|
112 MainLoopPtr
|
|
113 ball_bound::init(Viewer *viewer, int screen_w, int screen_h)
|
507
|
114 {
|
|
115 SceneGraphPtr ball;
|
|
116
|
|
117 // 固定した値で srandom すると、毎回同じ、random() 列が生成される
|
|
118 // random な値が欲しいなら、man random に方法が書いてあります。
|
|
119 srandom(100);
|
|
120
|
510
|
121 viewer->createFromXMLfile("xml_file/Ball.xml");
|
|
122
|
|
123 ball = viewer->createSceneGraph(Ball);
|
507
|
124 ball->set_move_collision(ball_move, ball_collision);
|
|
125
|
|
126 h0 = screen_h/2;
|
|
127 h0 = -1000;
|
|
128
|
|
129 ball->xyz[0] = screen_w/2;
|
|
130 //ball->xyz[0] = 0.0f;
|
|
131 ball->xyz[1] = h0;
|
|
132 ball->xyz[2] = 30.0f;
|
|
133
|
510
|
134 viewer->setSceneData(ball);
|
|
135
|
|
136 return viewer;
|
507
|
137 }
|
510
|
138
|
|
139 /* end */
|