annotate car.c @ 88:ce9ef7dcf2f0

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