之前做Android 游戲皆单㸃触摸, 諗住係風水羅盤實現两指縮放.
触摸分叁動作『鬆』『撳』『拖』.
動作 | 注释 |
#define TOUCH_UP 1 | 鬆 |
#define TOUCH_DOWN 2 | 撳 |
#define TOUCH_DRAGGED 3 | 拖 |
雙手拾指, 除非用埋脚指
#define MAX_FINGER 10 | 拾指 |
定義TOUCH結构, 用蒞存拾指『方位』同『動作』
TOUCH結构 | 注释 |
typedef struct TOUCH_STR{ | |
int count; | 手指量, 最多10指 |
int action[MAX_FINGER]; | 動作 |
int x[MAX_FINGER]; | X座標 |
int y[MAX_FINGER]; | Y座標 |
}TOUCH,*TOUCH_PTR; |
用栈stack蒞存触摸,
#define MAX_TOUCH 32 | 棧高32 |
TOUCH touch_array[MAX_TOUCH] ; | 触摸棧 |
int touch_count; | 棧頂 |
棧頂加壹, 每次存触摸『方位』同『動作』前調用
int Add_Touch(){
TOUCH_PTR touch; int index; if(touch_count >= MAX_TOUCH) return touch_count; index = touch_count; ++touch_count;// 棧頂加壹 touch = &touch_array[index]; touch->count = 0; return touch_count; } |
棧頂减壹, 讀『方位』同『動作』後調用
int Sub_Touch(){
if(touch_count <= 0) return touch_count; –touch_count; return touch_count; } |
存触摸『方位』同『動作』, finger係手指索引
bool Set_Touch(int finger,int action,float x,float y){
int index; TOUCH_PTR touch; index = touch_count-1; // 棧頂 touch = &touch_array[index]; if(touch->count < finger + 1) touch->count = finger + 1; touch->action[finger] = action; touch->x[finger] = x ; touch->y[finger] = y ; return true; } |
讀触摸, 『方位』同『動作』, finger係手指索引
bool Get_Touch(int finger,int * action,int * x,int * y){
TOUCH_PTR touch; int index; if(touch_count == 0) return false; index = touch_count – 1; touch = &touch_array[index]; *action = touch->action[finger]; *x = touch->x[finger]; *y = touch->y[finger]; return true; } |
計两指(x0,y0)(x1,y1)縮放時中心位(cx,cy)
float cx = (x0 – x1)/2 + x1; | |
float cy = (y0 – y1)/2 + y1; |
2D触摸坐标转屏幕坐标
VECTOR2D touchPoint2D; | 2D触摸點(x,y) |
VECTOR3D touchPoint3D; | 3D触摸點(x,y,z) |
触摸坐标转3D世界坐标
Init_VECTOR2D(&touchPoint2D, cx, cy); |
TouchToWorld(camera3D, &touchPoint2D, &touchPoint3D); |
計两指距,
Init_VECTOR2D(&v0,x0,y0); |
Init_VECTOR2D(&v1,x1,y1); |
Sub_VECTOR2D(&vdiff,&v0,&v1); |
length = Length_VECTOR2D(&vdiff); |
指距拉開放大, 两指行埋縮細. 通過移3D相機實現縮放.
if(_length > length ) y = Camera3D.pos.y + 2; | 縮細 |
if(_length < length ) y = Camera3D.pos.y – 2; | 放大 |