CIwVec3 HexMapTest::getWorldCoords(int x, int y) { // Generate a ray pointing to the view plane from the camera CIwVec3 dir(x - IwGxGetScreenWidth()/2, y - IwGxGetScreenHeight()/2, IwGxGetPerspMul()); CIwVec3 origin = s_viewMatrix.t; // Rotate into camera space dir = s_viewMatrix.RotateVec(dir); // dir.NormaliseSlow(); CIwVec3 inter = getIntersectionNew(origin, dir); inter = inter - s_ModelMatrix.t; iwfixed z = IW_FIXED_FROM_FLOAT(1.0f/zoom); inter.x = IW_FIXED_MUL(inter.x, z); inter.y = IW_FIXED_MUL(inter.y, z); inter.z = IW_FIXED_MUL(inter.z, z); CIwMat rotationMat = CIwMat::g_Identity; rotationMat.SetRotZ(IW_ANGLE_FROM_DEGREES(-rotation), true, true); inter = rotationMat.RotateVec(inter); return inter; }
//----------------------------------------------------------------------------- bool HexMapTest::Update() { int16 sprite1_pos_x,sprite1_pos_y; int16 sprite2_pos_x,sprite2_pos_y; if (mouse_mode == MOUSE_MODE_CHECKING) mouse_mode = MOUSE_MODE_DOWN; // UI processing if (((s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_PRESSED)) && mouse_mode == MOUSE_MODE_IDLE) mouse_mode = MOUSE_MODE_IDLE; if ((s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_RELEASED)) mouse_mode = MOUSE_MODE_CHECKING; // UpdateKey(); IwGxSetViewMatrix(&s_viewMatrix); // Generate a ray pointing to the view plane from the camera CIwVec3 dir(s3ePointerGetX() - IwGxGetScreenWidth()/2, s3ePointerGetY() - IwGxGetScreenHeight()/2, IwGxGetPerspMul()); // Rotate into camera space dir = s_viewMatrix.RotateVec(dir); // Update pointer system g_Input.Update(); if (g_Input.finger1IsDown()) { if (g_Input.overThreshold()) { if (!g_Input.finger1Continuing()) { s_ModelMatrix_initial = s_ModelMatrix; CIwVec3 vectCenter = getWorldCoords(0, 0); zoom_initial = zoom; rotation_initial = rotation; screenTranslationX_initial = screenTranslationX; screenTranslationY_initial = screenTranslationY; if (!g_Input.isMultiTouch()) { if (g_Input.finger1MovedTo(sprite1_pos_x, sprite1_pos_y)) { if (sprite1_pos_x > int(IwGxGetScreenWidth()) - SCREEN_MARGIN) { zooming = true; } if (sprite1_pos_y > int(IwGxGetScreenHeight()) - SCREEN_MARGIN) { rotating = true; } } } } if (!g_Input.finger2IsDown() && g_Input.finger1MovedTo(sprite1_pos_x, sprite1_pos_y)) { if (g_Input.finger1Continuing()) { if (zooming) { if (sprite1_pos_x > int(IwGxGetScreenWidth()) - SCREEN_MARGIN) { int16 dsprite1_pos_x, dsprite1_pos_y; if (g_Input.finger1MovementDelta(dsprite1_pos_x, dsprite1_pos_y)) { float deltaZoom = 1.0f - 1.0f*(dsprite1_pos_y)/IwGxGetScreenHeight(); SetZoom(deltaZoom, 8); } } } else if (rotating) { if (sprite1_pos_y > int(IwGxGetScreenHeight()) - SCREEN_MARGIN) { int16 dsprite1_pos_x, dsprite1_pos_y; if (g_Input.finger1MovementDelta(dsprite1_pos_x, dsprite1_pos_y)) { float deltaRotation = 360.0f*(dsprite1_pos_x)/IwGxGetScreenWidth(); SetRotation(deltaRotation); } } } else { SetTranslation(); } } } else { if (g_Input.finger1MovedTo(sprite1_pos_x, sprite1_pos_y)) { if (g_Input.finger2MovedTo(sprite2_pos_x, sprite2_pos_y)) { if (!g_Input.finger2Continuing()) { screenTranslationX_initial = screenTranslationX; screenTranslationY_initial = screenTranslationY; g_Input.resetInitial(0); } else { int16 sprite1_pos_x_initial,sprite1_pos_y_initial,sprite2_pos_x_initial,sprite2_pos_y_initial; g_Input.finger1Initial(sprite1_pos_x_initial,sprite1_pos_y_initial); g_Input.finger2Initial(sprite2_pos_x_initial,sprite2_pos_y_initial); int d12x=sprite1_pos_x-sprite2_pos_x; int d12y=sprite1_pos_y-sprite2_pos_y; int d12x_initial=sprite1_pos_x_initial-sprite2_pos_x_initial; int d12y_initial=sprite1_pos_y_initial-sprite2_pos_y_initial; int Delta_initial = d12x_initial*d12x_initial+d12y_initial*d12y_initial; int Delta = d12x*d12x+d12y*d12y; float newZoom = float(sqrt(1.0*Delta)/sqrt(Delta_initial)); SetZoom(newZoom, 1); float oldRotation = float(atan2(float(d12y_initial), float(d12x_initial))); float newRotation = float(atan2(float(d12y), float(d12x))); SetRotation(180.0f*(oldRotation-newRotation)/PI); { char string[256]; //sprintf(string, "`1`a del %04d, %04d",d1x,d2x); //IwGxPrintString(2, 96, string); sprintf(string, "`1`a zoo %5.2f, %5.2f, %5.2f",newZoom,oldRotation,newRotation); IwGxPrintString(2, 96, string); } } } } } } } else { zooming = false; rotating = false; // s_ModelMatrix = CIwMat::g_Identity; } return true; }
//----------------------------------------------------------------------------- int32 GhostCollision::GetFaceUnderCursor(int32 x, int32 y) { //Calculate pos/dir of cursor from camera CIwFVec3 pos = IwGxGetViewMatrix().t; CIwFVec3 dir(x, y, IwGxGetPerspMul()); dir.x -= IwGxGetScreenWidth()/2; dir.y -= IwGxGetScreenHeight()/2; //Extend to the far plane dir *= IW_FIXED_DIV(IwGxGetFarZ(), IwGxGetPerspMul()); //Transform pos/dir into model space dir = IwGxGetViewMatrix().RotateVec(dir); dir = modelMatrix->TransposeRotateVec(dir); //Use more accurate normalise dir.Normalise(); // Scale to touch far plane dir *= IwGxGetFarZ(); pos = modelMatrix->TransposeTransformVec(pos); //find first face intersection int32 minf = INT32_MAX; //nearest intersection distance uint32 nearest = 0; //nearest intersection index for (uint32 i = 0; i < m_Points.size(); i += 3) { CIwFVec3 v1 = (CIwFVec3)GetVert(i); CIwFVec3 v2 = (CIwFVec3)GetVert(i+1); CIwFVec3 v3 = (CIwFVec3)GetVert(i+2); float f = 0; if (IwIntersectLineTriNorm(pos, dir, v1, v2, v3, m_Norms[i/3], f)) { if (f < minf) { minf = f; nearest = i; } } /*{ // Draw the custom resource. Kills fps but helps debugging. CIwMaterial* pMat = IW_GX_ALLOC_MATERIAL(); pMat->SetColAmbient(0xff0000ff); pMat->SetCullMode(CIwMaterial::CULL_NONE); IwGxSetMaterial(pMat); CIwFVec3* verts = IW_GX_ALLOC(CIwFVec3, 3); verts[0] = v1; verts[1] = v2; verts[2] = v3; IwGxSetVertStreamModelSpace(verts, 3); IwGxDrawPrims(IW_GX_TRI_LIST, NULL, 3); }*/ } if (minf != INT32_MAX) { return nearest; } return -1; }