283
|
1 #include <math.h>
|
|
2 #include <stdlib.h>
|
|
3 #include "SceneGraphRoot.h"
|
|
4 #include "SGList.h"
|
|
5
|
|
6 // prototype
|
|
7 static void ball_move(SceneGraphPtr node, int screen_w, int screen_h);
|
|
8 static void ball_collision(SceneGraphPtr node, int screen_w, int screen_h, SceneGraphPtr tree);
|
|
9 static void ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree);
|
|
10
|
|
11
|
|
12 static float vy = 0.0f; // y 方向速度
|
|
13 static float dt = 1.0/1.0f; // frame rate
|
|
14
|
|
15 static float e = -0.8f; // 反発係数
|
|
16 static float g = 9.8f; // 重力加速度
|
|
17 //static float v0 = 0.0f; // 初速は 0
|
|
18
|
|
19 static float h0; // 初期高さ
|
|
20 static float ball_radius = 100.0f;
|
|
21
|
|
22 static int speed = 10.0f;
|
|
23
|
|
24 static void
|
|
25 ball_move_idle2(SceneGraphPtr node, int screen_w, int screen_h)
|
|
26 {
|
|
27 Pad *pad = sgroot->getController();
|
|
28
|
|
29 if (pad->circle.isHold()) {
|
|
30 if (pad->left.isHold()) {
|
|
31 node->xyz[0] -= speed;
|
|
32 if(node->xyz[0] < ball_radius)
|
|
33 node->xyz[0] = ball_radius;
|
|
34 } else if (pad->right.isHold()) {
|
|
35 node->xyz[0] += speed;
|
|
36 if(node->xyz[0] > screen_w - ball_radius)
|
|
37 node->xyz[0] = screen_w - ball_radius;
|
|
38 }
|
|
39
|
|
40 if (pad->up.isHold()) {
|
|
41 node->xyz[1] -= speed;
|
|
42 } else if (pad->down.isHold()) {
|
|
43 node->xyz[1] += speed;
|
|
44 if(node->xyz[1] > screen_h - ball_radius)
|
|
45 node->xyz[1] = screen_h - ball_radius;
|
|
46 }
|
|
47 } else {
|
|
48 node->set_move_collision(ball_move, ball_collision);
|
|
49 }
|
|
50 }
|
|
51
|
|
52 static int time = 0;
|
|
53
|
|
54 static void
|
|
55 ball_move_idle(SceneGraphPtr node, int screen_w, int screen_h)
|
|
56 {
|
|
57 Pad *pad = sgroot->getController();
|
|
58
|
|
59 if (pad->circle.isPush()) {
|
|
60 node->set_move_collision(ball_move_idle2, ball_collision_idle);
|
|
61 time = 0;
|
|
62 }
|
|
63
|
|
64 time++;
|
|
65
|
|
66 if (time > 90) {
|
|
67 float w = (float)random();
|
|
68
|
|
69 w = fmodf(w, screen_w - ball_radius*2);
|
|
70 node->xyz[0] = w + ball_radius;
|
|
71 node->xyz[1] = h0;
|
|
72 node->set_move_collision(ball_move, ball_collision);
|
|
73 time = 0;
|
|
74 }
|
|
75 }
|
|
76
|
|
77 static void
|
|
78 ball_move(SceneGraphPtr node, int screen_w, int screen_h)
|
|
79 {
|
|
80 vy += g * dt;
|
|
81 node->xyz[1] += vy * dt;
|
|
82 // node->xyz[0] += 10.0f;
|
|
83 }
|
|
84
|
|
85 static void
|
|
86 ball_collision_idle(SceneGraphPtr, int w, int h, SceneGraphPtr tree)
|
|
87 {
|
|
88 }
|
|
89
|
|
90 static void
|
|
91 ball_collision(SceneGraphPtr node, int screen_w, int screen_h,
|
|
92 SceneGraphPtr tree)
|
|
93 {
|
|
94 if (node->xyz[1] > screen_h - ball_radius) {
|
|
95 node->xyz[1] = screen_h - ball_radius;
|
|
96
|
|
97 vy *= e;
|
|
98 if (vy > -g && vy < 0) {
|
|
99 vy = 0.0;
|
|
100 node->set_move_collision(ball_move_idle, ball_collision_idle);
|
|
101 }
|
|
102 }
|
|
103 }
|
|
104
|
|
105
|
|
106 void
|
|
107 ball_bound_init(int screen_w, int screen_h)
|
|
108 {
|
|
109 SceneGraphPtr ball;
|
|
110
|
|
111 srandom(100);
|
|
112
|
|
113 sgroot->createFromXMLfile("xml_file/Ball.xml");
|
|
114 ball = sgroot->createSceneGraph(Ball);
|
|
115 ball->set_move_collision(ball_move, ball_collision);
|
|
116
|
|
117 h0 = screen_h/2;
|
|
118 h0 = -1000;
|
|
119
|
|
120 ball->xyz[0] = screen_w/2;
|
|
121 //ball->xyz[0] = 0.0f;
|
|
122 ball->xyz[1] = h0;
|
|
123 ball->xyz[2] = 30.0f;
|
|
124
|
|
125 sgroot->setSceneData(ball);
|
|
126 }
|