Mercurial > hg > Members > kono > Cerium
annotate Renderer/Test/cube.cc @ 563:b21a013051a2
all exmple on Mac OS X
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 22 Oct 2009 23:05:16 +0900 |
parents | f6daf964f483 |
children | d0b8860c17f8 |
rev | line source |
---|---|
507 | 1 #include <math.h> |
2 #include "SceneGraphRoot.h" | |
3 #include "vacuum.h" | |
4 #define SELECT 2 | |
5 | |
6 void | |
7 cube_collision(SceneGraphPtr node, int screen_w, int screen_h, | |
8 SceneGraphPtr tree) | |
9 { | |
10 if (node->frame > 120) { | |
11 cube_split(node,tree); | |
12 } | |
13 } | |
14 | |
15 void | |
16 cube_move_left(SceneGraphPtr node, int screen_w, int screen_h) | |
17 { | |
18 node->xyz[0] -= node->stack_xyz[0]; | |
19 node->xyz[1] += node->stack_xyz[1]; | |
20 | |
21 if (node->xyz[0] < 0) { | |
22 node->set_move_collision(cube_move_right, cube_collision); | |
23 } | |
24 | |
25 if (node->xyz[1] < 0 || node->xyz[1] > screen_h) { | |
26 node->stack_xyz[1] = -node->stack_xyz[1]; | |
27 } | |
28 } | |
29 | |
30 void | |
31 cube_rotate(SceneGraphPtr node, int w, int h) | |
32 { | |
33 node->angle[0] += 2.0f; | |
34 node->angle[1] += 2.0f; | |
35 node->angle[2] += 2.0f; | |
36 } | |
37 | |
38 void | |
39 cube_move_right(SceneGraphPtr node, int screen_w, int screen_h) | |
40 { | |
41 node->xyz[0] += node->stack_xyz[0]; | |
42 node->xyz[1] += node->stack_xyz[1]; | |
43 | |
44 if (node->xyz[0] > screen_w) { | |
45 node->set_move_collision(cube_move_left, cube_collision); | |
46 } | |
47 | |
48 if (node->xyz[1] < 0 || node->xyz[1] > screen_h) { | |
49 node->stack_xyz[1] = -node->stack_xyz[1]; | |
50 } | |
51 | |
52 } | |
53 | |
563
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
54 extern int redcube ; |
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
55 extern int enemy ; |
507 | 56 |
57 void | |
58 cube_split(SceneGraphPtr root,SceneGraphPtr tree) | |
59 { | |
60 | |
61 SceneGraphPtr p; | |
62 // SceneGraphPtr common_move = sgroot->createSceneGraph(); | |
63 // SceneGraphPtr root_common_move = root->parent; | |
64 | |
65 if(random()%SELECT) { | |
563
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
66 p = sgroot->createSceneGraph(redcube); |
507 | 67 } |
68 else { | |
563
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
69 p = sgroot->createSceneGraph(enemy); |
507 | 70 } |
71 | |
72 root->set_move_collision(cube_move_right, cube_collision); | |
73 p->set_move_collision(cube_move_left, cube_collision); | |
74 | |
75 root->frame = 0; | |
76 p->frame = 0; | |
77 | |
78 p->xyz[0] = root->xyz[0] + 2; | |
79 p->xyz[1] = root->xyz[1]; | |
80 p->xyz[2] = root->xyz[2]; | |
81 | |
82 p->stack_xyz[0] = 2.0f; | |
83 p->stack_xyz[1] = random()%3-1; | |
84 p->stack_xyz[2] = 0.0f; | |
85 | |
86 root->xyz[0] -= 2; | |
87 root->stack_xyz[0] = 2.0f; | |
88 root->stack_xyz[1] = random()%3-1; | |
89 | |
90 //common_move->addChild(p); | |
91 root->addBrother(p); | |
92 | |
93 } | |
94 | |
95 | |
96 void | |
97 collision_red(SceneGraphIteratorPtr it,SceneGraphPtr node) | |
98 { | |
99 float dx, dy,ddx,ddy, r; | |
100 float q = 0; | |
101 | |
563
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
102 for (; it->hasNext(redcube);) { |
507 | 103 |
563
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
104 it->next(redcube); |
507 | 105 SceneGraphPtr mcube = it->get(); |
106 dx = node->xyz[0] - mcube->xyz[0]; | |
107 dy = node->xyz[1] - mcube->xyz[1]; | |
108 | |
109 ddx = dx*dx; | |
110 ddy = dy*dy; | |
111 | |
112 if(sqrt(ddx) < 10 && sqrt(ddy) < 10) { | |
113 mcube->remove(); | |
114 continue; | |
115 } | |
116 r = sqrt(ddx + ddy); | |
117 if (r >= 1) q = 200/r; | |
118 if (dx == 0) { | |
119 if(mcube->xyz[1] > node->xyz[1]) { | |
120 mcube->stack_xyz[1] -= q; | |
121 } else if(mcube->xyz[1] < node->xyz[1]) { | |
122 mcube->stack_xyz[1] += q; | |
123 } | |
124 } else { | |
125 if(mcube->xyz[0] > node->xyz[0]) { | |
126 mcube->xyz[0] -= q*cos(atan(dy/dx)); | |
127 mcube->xyz[1] -= q*sin(atan(dy/dx)); | |
128 } else if(mcube->xyz[0] < node->xyz[0]) { | |
129 mcube->xyz[0] += q*cos(atan(dy/dx)); | |
130 mcube->xyz[1] += q*sin(atan(dy/dx)); | |
131 } | |
132 } | |
133 } | |
134 } | |
135 | |
136 void | |
137 collision_purple(SceneGraphIteratorPtr it,SceneGraphPtr node,int w,int h) | |
138 { | |
139 float dx, dy,ddx,ddy, r; | |
140 float q = 0; | |
141 | |
563
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
142 for (; it->hasNext(enemy);) { |
b21a013051a2
all exmple on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
508
diff
changeset
|
143 it->next(enemy); |
507 | 144 SceneGraphPtr mcube = it->get(); |
145 | |
146 dx = node->xyz[0] - mcube->xyz[0]; | |
147 dy = node->xyz[1] - mcube->xyz[1]; | |
148 ddx = dx*dx; | |
149 ddy = dy*dy; | |
150 | |
151 if(sqrt(ddx) < 10 && sqrt(ddy) < 10) { | |
152 gameover_scene(w,h,mcube); | |
153 node->remove(); | |
154 break; | |
155 } | |
156 r = sqrt(ddx + ddy); | |
157 if (r >= 1) q = 200/r; | |
158 if (dx == 0) { | |
159 if(mcube->xyz[1] > node->xyz[1]) { | |
160 mcube->stack_xyz[1] -= q; | |
161 } else if(mcube->xyz[1] < node->xyz[1]) { | |
162 mcube->stack_xyz[1] += q; | |
163 } | |
164 } else { | |
165 | |
166 if(mcube->xyz[0] > node->xyz[0]) { | |
167 mcube->xyz[0] -= q*cos(atan(dy/dx)); | |
168 mcube->xyz[1] -= q*sin(atan(dy/dx)); | |
169 } else if(mcube->xyz[0] < node->xyz[0]) { | |
170 mcube->xyz[0] += q*cos(atan(dy/dx)); | |
171 mcube->xyz[1] += q*sin(atan(dy/dx)); | |
172 } | |
173 } | |
174 } | |
175 } |