Mercurial > hg > old > magoroku_racing
annotate car.c @ 90:8edae89a3877
compile on Mac OS X
author | Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 May 2011 16:41:21 +0900 |
parents | ce9ef7dcf2f0 |
children | cb6c6de125dc |
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> |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
4 //#include "libps2.h" |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
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 /* 初期位置と初期方向 */ |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
17 //static FVECTOR location = {0, 0, 0, 1}; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
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; | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
33 //OBJECT *body; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
34 //TEXTURE* tex; |
0 | 35 void *free_addr; |
36 | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
37 //body = ps2util_obj_Create_fromXML(filename); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
38 //ps2util_obj_Set_effect(body, DEFAULT_SET); |
0 | 39 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
40 if (true){//malloc_align16(&free_addr, &car, sizeof(Car)) == -1) { |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
41 // fprintf(main_fp, "car.c: malloc_align16 error\n"); |
0 | 42 exit(EXIT_FAILURE); |
43 } | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
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 | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
54 //ps2_vu0_copy_vector(car->direction, direction); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
55 //ps2_vu0_copy_vector(car->location, location); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
56 //INIT_VECTOR(car->vertical, 0, -1, 0, 1); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
57 //INIT_VECTOR(car->body->xyz, 0, 0, 0, 1); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
58 //INIT_VECTOR(car->body->angle, 0, 0, 0, 1); |
0 | 59 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
60 //ps2util_obj_Renew_transMatrix(car->body); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
61 //ps2util_obj_Set_effect(car->body, DEFAULT_SET); |
0 | 62 |
63 if (*texname != 'n') { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
64 //tex = read_png_file(texname); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
65 //ps2util_tex_Set(tex); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
66 //ps2util_obj_Set_texture(car->body, tex); |
0 | 67 } |
68 | |
69 #ifdef DEBUG | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
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 { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
143 //FMATRIX rot; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
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 | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
150 // ps2_vu0_unit_matrix(rot); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
151 // ps2_vu0_rot_matrix_y(rot, rot, degree2radian((float)flg*car->rotation_angle)); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
152 // ps2_vu0_copy_vector(v, car->direction); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
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 |
0 | 179 car_field_check(CarPtr car) |
180 { | |
181 FieldPtr p; | |
182 FieldPtr f = field_get_actual(); | |
183 | |
88 | 184 // 現在のコースで衝突・内外判定 |
0 | 185 p = f; |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
186 if (true){//(col_detect(&p->colface, car->vertical, car->location) == true) { |
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; |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
192 if (true){//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; |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
201 if (true){//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 | |
221 car_move(CarPtr car) | |
222 { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
223 //FVECTOR mov, prev_location; |
0 | 224 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
225 // ps2_vu0_scale_vector(mov, car->direction, car->speed); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
226 // ps2_vu0_copy_vector(prev_location, car->location); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
227 // ps2_vu0_add_vector(car->location, car->location, mov); |
0 | 228 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
229 if (car_field_check(car) == false) { |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
230 // ps2_vu0_copy_vector(car->location, prev_location); |
0 | 231 car->speed = car->speed*0.5; |
232 } | |
233 } | |
234 | |
235 | |
236 static void | |
237 car_axis_rotation(CarPtr car) | |
238 { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
239 // FMATRIX pose, yrot, yrotinv; |
0 | 240 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
241 // ps2_vu0_unit_matrix(yrot); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
242 // ps2_vu0_rot_matrix_y(yrot, yrot, -degree2radian(car->y_angle)); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
243 // ps2_vu0_unit_matrix(yrotinv); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
244 // ps2_vu0_rot_matrix_y(yrotinv, yrotinv, degree2radian(car->y_angle)); |
0 | 245 |
246 { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
247 // FVECTOR yd = {0, -1, 0, 1}; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
248 // FVECTOR rotaxis; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
249 // FVECTOR q; |
0 | 250 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
251 // ps2_vu0_outer_product(rotaxis, car->vertical, yd); |
0 | 252 { |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
253 // float scale=1/NORM(rotaxis); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
254 // rotaxis[0] *= scale; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
255 // rotaxis[1] *= scale; |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
256 // rotaxis[2] *= scale; |
0 | 257 } |
258 | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
259 // quotanion(q, rotaxis, acos(ps2_vu0_inner_product(yd, car->vertical)/(NORM(yd)*NORM(car->vertical)))); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
260 // quotanion_rotmatrix(pose, q); |
0 | 261 } |
262 | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
263 // ps2_vu0_mul_matrix(pose, yrot, pose); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
264 // ps2_vu0_mul_matrix(pose, pose, yrotinv); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
265 // ps2_vu0_copy_matrix(car->body->transfer, pose); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
266 // ps2_vu0_rot_matrix_y(car->body->transfer, car->body->transfer, degree2radian(car->y_angle)); |
0 | 267 } |
268 | |
269 void | |
270 car_update(CarPtr car) | |
271 { | |
88 | 272 // 移動 |
0 | 273 car_move(car); |
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 { | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
286 // TEXTURE *t; |
0 | 287 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
288 // if ((t = p->body->surfaces->texture)) { |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
289 // ps2util_tex_Exclude(t); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
290 // free_texture(t); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
291 // } |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
292 // ps2util_obj_Free(p->body); |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
293 // free(p->free_addr); |
0 | 294 |
295 #ifdef DEBUG | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
296 // fprintf(main_fp, "free car addr = %x\n", (int)p->free_addr); |
0 | 297 #endif |
298 } |