Mercurial > hg > old > magoroku_racing
annotate col.cc @ 112:5106d8d12ded
Changing Magoroku.cc
author | Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 09 Jun 2011 14:59:24 +0900 |
parents | c534f339ee8b |
children | 6ef3b0efdeaf |
rev | line source |
---|---|
0 | 1 #include <stdio.h> |
91 | 2 #include"libps2.h" |
3 #include"ps2util.h" | |
0 | 4 #include"col.h" |
5 #include"mytype.h" | |
91 | 6 #include <stdbool.h> |
7 #include <string.h> | |
0 | 8 |
39 | 9 extern FILE *main_fp; |
10 | |
20 | 11 /** |
88 | 12 * v0, v1が描く三角形と点pの内外判定(approx detection) |
13 * 点pはv0, v1が描く面のどこかに位置している。 | |
14 * pが面の三角形の外側にあるときTRUEを戻し、 | |
15 * そうでなければFALSEを戻す。境界は含む。 | |
0 | 16 */ |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
17 |
91 | 18 |
19 bool | |
20 col_detect_approx(FVECTOR p, FVECTOR v0, FVECTOR v1){ | |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
21 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
22 float v_r1, v_r2, r1_r2, r1_r1, r2_r2; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
23 float a, b, r; |
0 | 24 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
25 v_r1 = ps2_vu0_inner_product( p, v0 ); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
26 v_r2 = ps2_vu0_inner_product( p, v1 ); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
27 r1_r2 = ps2_vu0_inner_product( v0, v1 ); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
28 r1_r1 = ps2_vu0_inner_product( v0, v0 ); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
29 r2_r2 = ps2_vu0_inner_product( v1, v1 ); |
0 | 30 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
31 a = (v_r1 * r2_r2 - v_r2 * r1_r2); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
32 b = (v_r2 * r1_r1 - v_r1 * r1_r2); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
33 r = (r1_r1 * r2_r2 - r1_r2 * r1_r2); |
0 | 34 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
35 return (a>=0 && b>=0 && a+b-r<=0) ? true : false; |
0 | 36 } |
91 | 37 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
38 |
0 | 39 |
40 /** | |
88 | 41 * 垂心oの算出. 点pからv0, v1が描く面へ垂線を引いたときの交点oを |
42 * 垂心(orthocenter)と呼ぶ。 | |
0 | 43 */ |
91 | 44 //なんか一回も使われていないらしいので消す、はずだった |
45 static void | |
46 col_orthocenter(FVECTOR o, FVECTOR p, FVECTOR p0, FVECTOR normal) | |
47 { | |
48 | |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
49 FVECTOR o0, d; |
0 | 50 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
51 ps2_vu0_sub_vector(o0, p, p0); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
52 o0[W_AXIS] = 0; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
53 ps2_vu0_scale_vector( |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
54 d, normal, ps2_vu0_inner_product(o0, normal)); |
0 | 55 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
56 o[X_AXIS] = p[X_AXIS] - d[X_AXIS]; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
57 o[Y_AXIS] = p[Y_AXIS] - d[Y_AXIS]; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
58 o[Z_AXIS] = p[Z_AXIS] - d[Z_AXIS]; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
59 o[W_AXIS] = 1; |
91 | 60 } |
0 | 61 |
62 /** | |
88 | 63 * 点pと面との衝突(点pとdetection face面との垂心oがdetection face領域の |
64 * 中にあるかを)判定衝突時(oがdetection face領域の外)はFALSEを戻し、そう | |
65 * でなければTRUE、 垂心(orthocenter)、垂心のある面の法線(normal | |
66 * vector)を戻す. | |
0 | 67 */ |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
68 |
91 | 69 |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
70 bool |
91 | 71 col_detect(ColFaceHd* hd, FVECTOR pose, FVECTOR p) |
0 | 72 { |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
73 FVECTOR o, o0; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
74 int i; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
75 ColFacePtr col_face=hd->colface; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
76 for (i=0; i<hd->facenum; i++) { |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
77 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
78 // * 垂心oを求める |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
79 col_orthocenter(o, p, col_face[i].p0, col_face[i].normal); |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
80 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
81 // * detection face上のある点p0から垂心oへのベクトルo0を求める |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
82 ps2_vu0_sub_vector(o0, o, col_face[i].p0); |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
83 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
84 // * ベクトルv0, v1の描く三角形とo0とで内外判定(range approx detection)を行う |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
85 if (col_detect_approx(o0, col_face[i].v0, col_face[i].v1)==true) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
86 goto NOCOLLISION; |
0 | 87 } |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
88 } |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
89 return false; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
90 NOCOLLISION: |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
91 ps2_vu0_copy_vector(p, o); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
92 ps2_vu0_copy_vector(pose, col_face[i].normal); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
93 return true; |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
94 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
95 return false; |
0 | 96 } |
91 | 97 |
0 | 98 |
99 /* | |
88 | 100 * detection face作成 |
0 | 101 */ |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
102 |
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
103 |
0 | 104 void |
91 | 105 col_init(ColFaceHd* hd, PolygonInfo* face) |
0 | 106 { |
91 | 107 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
108 int i; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
109 FVECTOR p1, p2; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
110 ColFacePtr colface; |
0 | 111 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
112 malloc_align16(&hd->free_addr, &colface, sizeof(ColFace)*face->siz/3); |
0 | 113 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
114 hd->facenum = face->siz/3; |
0 | 115 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
116 for (i=0; i<hd->facenum; i++) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
117 memcpy(colface[i].p0, ((FVECTOR*)face->pnts)[i*3], sizeof(FVECTOR)); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
118 memcpy(p1, ((FVECTOR*)face->pnts)[i*3+2], sizeof(FVECTOR)); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
119 memcpy(p2, ((FVECTOR*)face->pnts)[i*3+1], sizeof(FVECTOR)); |
0 | 120 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
121 ps2_vu0_sub_vector(colface[i].v0, p1, colface[i].p0); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
122 ps2_vu0_sub_vector(colface[i].v1, p2, colface[i].p0); |
0 | 123 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
124 ps2_vu0_outer_product(colface[i].normal, colface[i].v0, colface[i].v1); |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
125 ps2_vu0_normalize(colface[i].normal, colface[i].normal); |
0 | 126 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
127 if (colface[i].normal[1] > 0) { |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
128 colface[i].normal[0] = -colface[i].normal[0]; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
129 colface[i].normal[1] = -colface[i].normal[1]; |
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
130 colface[i].normal[2] = -colface[i].normal[2]; |
0 | 131 } |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
132 } |
0 | 133 |
96
0b65ca27f113
compile on Mac OS X 2
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
134 hd->colface = colface; |
90
8edae89a3877
compile on Mac OS X
Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
135 |
0 | 136 } |