Mercurial > hg > old > magoroku_racing.bad
comparison car.cc @ 99:c534f339ee8b
change c++
author | e085768 |
---|---|
date | Thu, 02 Jun 2011 17:51:41 +0900 |
parents | car.c@0b65ca27f113 |
children | e5f2eb98b575 |
comparison
equal
deleted
inserted
replaced
98:1033fece71ce | 99:c534f339ee8b |
---|---|
1 /* car.c */ | |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <stdbool.h> | |
6 #include "libps2.h" | |
7 #include "ps2util.h" | |
8 #include "field.h" | |
9 #include "car.h" | |
10 #include "mytype.h" | |
11 #include "quotanion.h" | |
12 #include "game.h" | |
13 #include "stdbool.h" | |
14 | |
15 #define DEFAULT_SET (EFFECT_TEXTURE_USE | EFFECT_ALPHABLEND_UNUSE | EFFECT_SHADING_FLAT) | |
16 #define BUFSIZE 256 | |
17 | |
18 /* 初期位置と初期方向 */ | |
19 static FVECTOR location = {0, 0, 0, 1}; | |
20 static FVECTOR direction = {0, 0, 1, 1}; | |
21 | |
22 extern FILE* main_fp; | |
23 | |
24 static CarPtr | |
25 car_create(int car_id, char *filename, char *texname, float speed_accel, | |
26 float speed_max, float rot, float brake) | |
27 { | |
28 CarPtr car; | |
29 OBJECT *body; | |
30 TEXTURE* tex; | |
31 void *free_addr; | |
32 | |
33 body = ps2util_obj_Create_fromXML(filename); | |
34 ps2util_obj_Set_effect(body, DEFAULT_SET); | |
35 | |
36 if (malloc_align16(&free_addr, &car, sizeof(Car)) == -1) { | |
37 fprintf(main_fp, "car.c: malloc_align16 error\n"); | |
38 exit(EXIT_FAILURE); | |
39 } | |
40 car->body = body; | |
41 car->next = NULL; | |
42 car->speed = 0.0; | |
43 car->speed_accel = speed_accel; | |
44 car->speed_max = speed_max; | |
45 car->brake = brake; | |
46 car->rotation_angle = rot; | |
47 car->y_angle = 0.0; | |
48 car->free_addr = free_addr; | |
49 | |
50 ps2_vu0_copy_vector(car->direction, direction); | |
51 ps2_vu0_copy_vector(car->location, location); | |
52 INIT_VECTOR(car->vertical, 0, -1, 0, 1); | |
53 INIT_VECTOR(car->body->xyz, 0, 0, 0, 1); | |
54 INIT_VECTOR(car->body->angle, 0, 0, 0, 1); | |
55 | |
56 ps2util_obj_Renew_transMatrix(car->body); | |
57 ps2util_obj_Set_effect(car->body, DEFAULT_SET); | |
58 | |
59 if (*texname != 'n') { | |
60 tex = read_png_file(texname); | |
61 ps2util_tex_Set(tex); | |
62 ps2util_obj_Set_texture(car->body, tex); | |
63 } | |
64 | |
65 #ifdef DEBUG | |
66 fprintf(main_fp, "malloc car addr = %x\n", (int)free_addr); | |
67 #endif | |
68 | |
69 return car; | |
70 } | |
71 | |
72 static CarPtr | |
73 car_new_readCSV(FILE *fp, int id) | |
74 { | |
75 CarPtr new; | |
76 int car_id; | |
77 float speed_accel, speed_max, brake, rot; | |
78 char buff[BUFSIZE], *bufp; | |
79 char carImg[BUFSIZE], texImg[BUFSIZE]; | |
80 bool flag; | |
81 | |
82 flag = false; | |
83 | |
84 while ((bufp = fgets(buff, BUFSIZE, fp)) != NULL) { | |
85 bufp++; | |
86 | |
87 switch (buff[0]) { | |
88 case 'n': | |
89 if (flag == false) { | |
90 sscanf(bufp, " %d\n", &car_id); | |
91 if (id == car_id) { | |
92 flag = true; | |
93 } | |
94 } | |
95 break; | |
96 case 't': | |
97 if (flag == true) { | |
98 sscanf(bufp, " %s %s %f %f %f %f\n", | |
99 carImg, texImg, &speed_accel, &speed_max, &rot, &brake); | |
100 new = car_create(id, carImg, texImg, | |
101 speed_accel, speed_max, rot, brake); | |
102 return new; | |
103 } | |
104 break; | |
105 default: | |
106 break; | |
107 } | |
108 } | |
109 | |
110 /* ここまで辿り着いたら読み込み失敗 */ | |
111 fprintf(main_fp, "error - car_new_readCSV\n"); | |
112 exit(EXIT_FAILURE); | |
113 } | |
114 | |
115 CarPtr car_init(int id) | |
116 { | |
117 CarPtr new; | |
118 FILE *fp; | |
119 char *filename = "car/car.dat"; | |
120 | |
121 if (!(fp = fopen(filename, "r"))) { | |
122 fprintf(main_fp, "error read file %s\n", filename); | |
123 exit(EXIT_FAILURE); | |
124 } | |
125 new = car_new_readCSV(fp, id); | |
126 fclose(fp); | |
127 | |
128 return new; | |
129 } | |
130 | |
131 /*--------------------------- | |
132 carをY軸で回転させる | |
133 flg: 回転方向 1:右, -1:左 | |
134 ---------------------------*/ | |
135 void | |
136 car_swerve(CarPtr car, int flg) | |
137 { | |
138 FMATRIX rot; | |
139 FVECTOR v; | |
140 | |
141 car->y_angle += (float)flg*car->rotation_angle; | |
142 car->y_angle += (car->y_angle < 0) ? 360.0 : 0; | |
143 car->y_angle += (car->y_angle > 360.0) ? -360.0 : 0; | |
144 | |
145 ps2_vu0_unit_matrix(rot); | |
146 ps2_vu0_rot_matrix_y(rot, rot, degree2radian((float)flg*car->rotation_angle)); | |
147 ps2_vu0_copy_vector(v, car->direction); | |
148 ps2_vu0_apply_matrix(car->direction, rot, v); | |
149 } | |
150 | |
151 /*---------------------------- | |
152 carを加速・減速する | |
153 (flg == 1) ? 加速 : 減速 | |
154 ---------------------------*/ | |
155 void | |
156 car_accelerate(CarPtr car, int flg) | |
157 { | |
158 car->speed += (flg == 1) ? car->speed_accel : -car->brake; | |
159 car->speed = (car->speed < 0) ? 0 : car->speed; | |
160 car->speed = (car->speed > car->speed_max) ? car->speed_max : car->speed; | |
161 } | |
162 | |
163 | |
164 /*--------------------------------- | |
165 進行後のcarに対してコース面の内外判定を行い、 | |
166 : 現在のコース | |
167 : 現在のコースの次のコース | |
168 : 現在のコースの前のコース | |
169 に存在すればTRUEを返す | |
170 どのコースにも居ない(壁に衝突した)場合は | |
171 FALSEを返す。 | |
172 ---------------------------------*/ | |
173 static bool | |
174 car_field_check(Game *game ,CarPtr car) | |
175 { | |
176 FieldPtr p; | |
177 FieldPtr f = field_get_actual(); | |
178 | |
179 // 現在のコースで衝突・内外判定 | |
180 p = f; | |
181 if (col_detect(&p->colface, car->vertical, car->location) == true) { | |
182 goto FIELD_CHECK_OK; | |
183 } | |
184 | |
185 // 現在のコースの次に隣接するコースで衝突・内外判定 | |
186 p = f->next; | |
187 if (col_detect(&p->colface, car->vertical, car->location) == true) { | |
188 if (field_rap_increment(1)) { | |
189 game->rap++; | |
190 } | |
191 goto FIELD_CHECK_OK; | |
192 } | |
193 | |
194 // 現在のコースの前に隣接するコースで衝突・内外判定 | |
195 p = f->prev; | |
196 if (col_detect(&p->colface, car->vertical, car->location) == true) { | |
197 field_rap_increment(-1); | |
198 goto FIELD_CHECK_OK; | |
199 } | |
200 | |
201 // どのコース上にも居ない(壁に衝突) | |
202 return false; | |
203 | |
204 FIELD_CHECK_OK: | |
205 field_set_actual(p); | |
206 return true; | |
207 } | |
208 | |
209 | |
210 /*------------------------------ | |
211 速度speedと向きdirectionから | |
212 進行後の位置locationを求める。 | |
213 また、進行後の衝突判定も行う。 | |
214 ------------------------------*/ | |
215 static void | |
216 car_move(Game *game,CarPtr car) | |
217 { | |
218 FVECTOR mov, prev_location; | |
219 | |
220 ps2_vu0_scale_vector(mov, car->direction, car->speed); | |
221 ps2_vu0_copy_vector(prev_location, car->location); | |
222 ps2_vu0_add_vector(car->location, car->location, mov); | |
223 | |
224 if (car_field_check(game,car) == false) { | |
225 ps2_vu0_copy_vector(car->location, prev_location); | |
226 car->speed = car->speed*0.5; | |
227 } | |
228 } | |
229 | |
230 | |
231 static void | |
232 car_axis_rotation(CarPtr car) | |
233 { | |
234 FMATRIX pose, yrot, yrotinv; | |
235 | |
236 ps2_vu0_unit_matrix(yrot); | |
237 ps2_vu0_rot_matrix_y(yrot, yrot, -degree2radian(car->y_angle)); | |
238 ps2_vu0_unit_matrix(yrotinv); | |
239 ps2_vu0_rot_matrix_y(yrotinv, yrotinv, degree2radian(car->y_angle)); | |
240 | |
241 { | |
242 FVECTOR yd = {0, -1, 0, 1}; | |
243 FVECTOR rotaxis; | |
244 FVECTOR q; | |
245 | |
246 ps2_vu0_outer_product(rotaxis, car->vertical, yd); | |
247 { | |
248 float scale=1/NORM(rotaxis); | |
249 rotaxis[0] *= scale; | |
250 rotaxis[1] *= scale; | |
251 rotaxis[2] *= scale; | |
252 } | |
253 | |
254 quotanion(q, rotaxis, acos(ps2_vu0_inner_product(yd, car->vertical)/(NORM(yd)*NORM(car->vertical)))); | |
255 quotanion_rotmatrix(pose, q); | |
256 } | |
257 | |
258 ps2_vu0_mul_matrix(pose, yrot, pose); | |
259 ps2_vu0_mul_matrix(pose, pose, yrotinv); | |
260 ps2_vu0_copy_matrix(car->body->transfer, pose); | |
261 ps2_vu0_rot_matrix_y(car->body->transfer, car->body->transfer, degree2radian(car->y_angle)); | |
262 } | |
263 | |
264 void car_update( Game *game, CarPtr car ) | |
265 { | |
266 // 移動 | |
267 car_move(game,car); | |
268 | |
269 // 摩擦による速度減少 | |
270 car->speed -= 0.005; | |
271 car->speed = (car->speed < 0) ? 0 : car->speed; | |
272 | |
273 // 傾き | |
274 car_axis_rotation(car); | |
275 } | |
276 | |
277 | |
278 void | |
279 car_destroy(CarPtr p) | |
280 { | |
281 TEXTURE *t; | |
282 | |
283 if ((t = p->body->surfaces->texture)) { | |
284 ps2util_tex_Exclude(t); | |
285 free_texture(t); | |
286 } | |
287 ps2util_obj_Free(p->body); | |
288 free(p->free_addr); | |
289 | |
290 #ifdef DEBUG | |
291 fprintf(main_fp, "free car addr = %x\n", (int)p->free_addr); | |
292 #endif | |
293 } | |
294 | |
295 // 適当に自作 | |
296 void car_id_update(Game *game, CarPtr jiki) | |
297 { | |
298 printf("car_id_update\n"); | |
299 } |