comparison Renderer/Test/cube.cc @ 0:04e28d8d3c6f

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