Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
204 chris 1
#ifdef _WIN32
2
#include <windows.h>
3
#endif
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <math.h>
8
#include <AR/param.h>
9
#include <AR/matrix.h>
10
#include <AR/ar.h>
11
#include "paddle.h"
12
#include "command_sub.h"
13
#include "util.h"
14
 
15
 
16
#define  SHAKE_BUF_SIZE     10
17
#define  PUNCH_BUF_SIZE      5
18
#define  PUSH_BUF_SIZE       2
19
 
20
typedef struct {
21
    double   mat[3][4];
22
    int      f;
23
} SHAKE_BUF_T;
24
 
25
typedef struct {
26
    double   x, y, z;
27
    int      f;
28
} PUNCH_BUF_T;
29
 
30
typedef struct {
31
    double   x, y, z;
32
    int      f;
33
} PUSH_BUF_T;
34
 
35
static SHAKE_BUF_T    shake_buf[SHAKE_BUF_SIZE];
36
static int            shake_buf_num = 0;
37
//static PUNCH_BUF_T    punch_buf[PUNCH_BUF_SIZE];
38
//static int            punch_buf_num = 0;
39
//static PUSH_BUF_T     push_buf[PUNCH_BUF_SIZE];
40
//static int            push_buf_num = 0;
41
 
42
 
43
int check_shake( double card_trans[3][4], int f )
44
{
45
    ARMat   *mat_a,  *mat_b, *mat_c;
46
    double  lxy, lz;
47
    int     i, j, k;
48
 
49
    if( shake_buf_num < SHAKE_BUF_SIZE ) {
50
        if( f ) {
51
            for( j = 0; j < 3; j++ ) {
52
                for( i = 0; i < 4; i++ ) {
53
                    shake_buf[shake_buf_num].mat[j][i] = card_trans[j][i];
54
                }
55
            }
56
            shake_buf[shake_buf_num].f = 1;
57
        }
58
        else {
59
            shake_buf[shake_buf_num].f = 0;
60
        }
61
        shake_buf_num++;
62
 
63
        return 0;
64
    }
65
    for( i = 1; i < shake_buf_num; i++ ) {
66
        shake_buf[i-1] = shake_buf[i];
67
    }
68
    if( f ) {
69
        for( j = 0; j < 3; j++ ) {
70
            for( i = 0; i < 4; i++ ) {
71
                shake_buf[shake_buf_num-1].mat[j][i] = card_trans[j][i];
72
            }
73
        }
74
        shake_buf[shake_buf_num-1].f = 1;
75
    }
76
    else {
77
        shake_buf[shake_buf_num-1].f = 0;
78
 
79
        return 0;
80
    }
81
 
82
    if( shake_buf[SHAKE_BUF_SIZE-3].f == 0
83
     || shake_buf[0].f == 0 ) return 0;
84
 
85
 
86
    mat_a = arMatrixAlloc( 4, 4 );
87
    mat_b = arMatrixAlloc( 4, 4 );
88
    mat_c = arMatrixAlloc( 4, 4 );
89
    for( j = 0; j < 3; j++ ) {
90
        for( i = 0; i < 4; i++ ) {
91
            mat_a->m[j*4+i] = card_trans[j][i];
92
        }
93
    }
94
    mat_a->m[3*4+0] = 0.0;
95
    mat_a->m[3*4+1] = 0.0;
96
    mat_a->m[3*4+2] = 0.0;
97
    mat_a->m[3*4+3] = 1.0;
98
    arMatrixSelfInv( mat_a );
99
 
100
    for( k = 0 ; k < SHAKE_BUF_SIZE-3; k++ ) {
101
        if( shake_buf[k].f == 0 ) continue;
102
 
103
        for( j = 0; j < 3; j++ ) {
104
            for( i = 0; i < 4; i++ ) {
105
                mat_b->m[j*4+i] = shake_buf[k].mat[j][i];
106
            }
107
        }
108
        mat_b->m[3*4+0] = 0.0;
109
        mat_b->m[3*4+1] = 0.0;
110
        mat_b->m[3*4+2] = 0.0;
111
        mat_b->m[3*4+3] = 1.0;
112
        arMatrixMul( mat_c, mat_a, mat_b );
113
 
114
        lxy = sqrt( (mat_c->m[0*4+3])*(mat_c->m[0*4+3])
115
                   +(mat_c->m[1*4+3])*(mat_c->m[1*4+3]));
116
        lz  = mat_c->m[2*4+3];
117
 
118
        if( lxy < 20.0 && lz < 20.0 ) break;
119
    }
120
    if( k == SHAKE_BUF_SIZE-3 ) {
121
        arMatrixFree( mat_a );
122
        arMatrixFree( mat_b );
123
        arMatrixFree( mat_c );
124
        return 0;
125
    }
126
 
127
    for( ; k < SHAKE_BUF_SIZE-1; k++ ) {
128
        if( shake_buf[k].f == 0 ) continue;
129
 
130
        for( j = 0; j < 3; j++ ) {
131
            for( i = 0; i < 4; i++ ) {
132
                mat_b->m[j*4+i] = shake_buf[k].mat[j][i];
133
            }
134
        }
135
        mat_b->m[3*4+0] = 0.0;
136
        mat_b->m[3*4+1] = 0.0;
137
        mat_b->m[3*4+2] = 0.0;
138
        mat_b->m[3*4+3] = 1.0;
139
        arMatrixMul( mat_c, mat_a, mat_b );
140
 
141
        lxy = sqrt( (mat_c->m[0*4+3])*(mat_c->m[0*4+3])
142
                   +(mat_c->m[1*4+3])*(mat_c->m[1*4+3]));
143
        lz  = mat_c->m[2*4+3];
144
 
145
        if( lxy > 60.0 && lz < 20.0 ) break;
146
    }
147
    arMatrixFree( mat_a );
148
    arMatrixFree( mat_b );
149
    arMatrixFree( mat_c );
150
 
151
    if( k < SHAKE_BUF_SIZE-1 ) {
152
        shake_buf_num = 0;
153
        return 1;
154
    }
155
 
156
    return 0;
157
}
158
 
159
 
160
int check_incline( double card_trans[3][4], double base_trans[3][4], double *angle )
161
{
162
    ARMat   *mat_a, *mat_b, *mat_c;
163
    double  a, b, c;
164
    int     i, j;
165
 
166
    mat_a = arMatrixAlloc( 4, 4 );
167
    mat_b = arMatrixAlloc( 4, 4 );
168
    mat_c = arMatrixAlloc( 4, 4 );
169
    for( j = 0; j < 3; j++ ) {
170
        for( i = 0; i < 4; i++ ) {
171
            mat_b->m[j*4+i] = base_trans[j][i];
172
        }
173
    }
174
    mat_b->m[3*4+0] = 0.0;
175
    mat_b->m[3*4+1] = 0.0;
176
    mat_b->m[3*4+2] = 0.0;
177
    mat_b->m[3*4+3] = 1.0;
178
 
179
    for( j = 0; j < 3; j++ ) {
180
        for( i = 0; i < 4; i++ ) {
181
            mat_a->m[j*4+i] = card_trans[j][i];
182
        }
183
    }
184
    mat_a->m[3*4+0] = 0.0;
185
    mat_a->m[3*4+1] = 0.0;
186
    mat_a->m[3*4+2] = 0.0;
187
    mat_a->m[3*4+3] = 1.0;
188
    arMatrixSelfInv( mat_a );
189
    arMatrixMul( mat_c, mat_a, mat_b );
190
 
191
    get_angle( (double (*)[4])(mat_c->m), &a, &b, &c );
192
 
193
    arMatrixFree( mat_a );
194
    arMatrixFree( mat_b );
195
    arMatrixFree( mat_c );
196
 
197
    if( b > 0.4 ) {
198
      *angle = a + 3.141592;
199
      return 1;
200
    }
201
 
202
    return 0;
203
}
204
 
205
int check_pickup(double card_trans[3][4], double base_trans[3][4], ItemList* itlist, double* angle)
206
{
207
    ARMat   *t1, *t2, *t3;
208
    double   x, y, z;
209
    double   lx, ly;
210
    double   a, b, c;
211
    int      ret;
212
    int      i, j;
213
 
214
    //    printf("checking pickup New \n");
215
 
216
    t1 = arMatrixAlloc( 4, 4 );
217
    for( j = 0; j < 3; j++ ) {
218
       for( i = 0; i < 4; i++ ) {
219
            t1->m[j*4+i] = base_trans[j][i];
220
        }
221
    }
222
    t1->m[12] = t1->m[13] = t1->m[14] = 0.0;
223
    t1->m[15] = 1.0;
224
 
225
    t2 = arMatrixAlloc( 4, 4 );
226
    for( j = 0; j < 3; j++ ) {
227
        for( i = 0; i < 4; i++ ) {
228
            t2->m[j*4+i] = card_trans[j][i];
229
        }
230
    }
231
    t2->m[12] = t2->m[13] = t2->m[14] = 0.0;
232
    t2->m[15] = 1.0;
233
 
234
    if( arMatrixSelfInv(t1) != 0 ) {
235
        arMatrixFree( t1 );
236
        arMatrixFree( t2 );
237
        return -1;
238
    }    
239
    //    printf("past arMatrixSelfInv\n");
240
 
241
    t3 = arMatrixAllocMul(t1, t2);
242
    if( t3 == NULL ) {
243
        arMatrixFree( t1 );
244
        arMatrixFree( t2 );
245
        return -1;
246
    }
247
 
248
    //    printf("past arMatrixAllocMul\n");
249
 
250
    x = t3->m[0*4+3];
251
    y = t3->m[1*4+3];
252
    z = t3->m[2*4+3];
253
 
254
    //  printf("x: %f y: %f z: %f\n",x,y,z);
255
 
256
    ret = -1;
257
    for( i = 0; i < itlist->itemnum; i ++ ){
258
      lx = x - itlist->item[i].pos[0];
259
      ly = y - itlist->item[i].pos[1];
260
      //MB increased by a factor of 10
261
      if( lx*lx + ly*ly < 1000.0 && z < 20.0 ) {
262
          ret = i;
263
      }
264
    }
265
 
266
    if( ret >= 0 ) {
267
        get_angle( (double (*)[4])(t3->m), &a, &b, &c );
268
        *angle = -c;
269
    }
270
 
271
    arMatrixFree( t1 );
272
    arMatrixFree( t2 );
273
    arMatrixFree( t3 );
274
 
275
    return ret;
276
}