跑酷遊戲觸控操作:左/右滑動切換跑道,上/下滑動跳躍/滑鏟(此處簡化為攝像機前後移動)。單點觸控,記錄按下與抬起座標,計算向量判斷方向。
diff = end - start。(1,0) 做點積 dot。|dot| > cos(45°) = √2/2 判定為水平滑動(左/右)。diff.y 正負區分。MoveBy 動作(帶 Tag 防重複觸發)。
void PlayerInputController::receiveTouchEnd(Vec2 pos, Node* pRenderNode) {
_touchEndPos = pos;
Vec2 diff = _touchEndPos - _touchBeginPos;
diff.normalize();
float dot = diff.dot(Vec2(1,0)); // 水平投影
const float THRESHOLD = sqrt(2)/2; // 45度
if (fabs(dot) > THRESHOLD) { // 左右
if (dot > 0) { // 右
if (playerX <= MID && !hasAction(TURN_RIGHT)) runAction(MoveBy(0.2f, Vec3(10,0,0)), TURN_RIGHT);
} else { // 左
if (playerX >= MID && !hasAction(TURN_LEFT)) runAction(MoveBy(0.2f, Vec3(-10,0,0)), TURN_LEFT);
}
} else { // 上下
auto cameras = pRenderNode->getScene()->getCameras();
auto mask = player->getCameraMask();
auto it = find_if(cameras.begin(), cameras.end(),
[mask](Camera* c){ return (c->getCameraFlag() & mask) != 0; });
while (it != cameras.end()) {
auto p = (*it)->getPosition3D();
p.z += (diff.y > 0) ? -5 : 5; // 上滑前進/下滑後退
(*it)->setPosition3D(p);
it = find_if(next(it), cameras.end(), ...);
}
}
}
find_if 謂詞,捕獲 mask 篩選攝像機。std::move 轉移觸控坐標(避免拷貝)。auto 類型推導簡化迭代器聲明。