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