// Update Scene Struct void updateSceneMouse(double diffX, double diffY) { double eyeX = myScene->pose.eye.val[0]; double eyeY = myScene->pose.eye.val[1]; double eyeZ = myScene->pose.eye.val[2]; double centerX = myScene->pose.center.val[0]; double centerY = myScene->pose.center.val[1]; double centerZ = myScene->pose.center.val[2]; double threshold = .1; double scale = 10; double maxAngle = PI/4; // Rotating from Z-Axis (optional X & Y) - move mouse left and right if (leftClicked && !rightClicked) { CvScalar diff = cvScalar(centerX - eyeX, centerY - eyeY, centerZ - eyeZ, 0.0); double radius = norm(diff); myScene->pose.eye.val[0] = centerX + radius * cos(maxAngle * diffY); myScene->pose.eye.val[1] = centerY + radius * sin(maxAngle * diffY); } // Translation along Z-Y plane if (!leftClicked && rightClicked) { if ((absD(diffX) > threshold) && (absD(diffY) < threshold)) { myScene->pose.eye.val[1] = eyeY + scale*diffX; myScene->pose.center.val[1] = centerY + scale*diffX; } else if ((absD(diffX) < threshold) && (absD(diffY) > threshold)) { myScene->pose.eye.val[2] = eyeZ + scale*diffY; myScene->pose.center.val[2] = centerZ + scale*diffY; } } // Zooming along X-Axis - move mouse up and down if ((leftClicked && rightClicked) || middleClicked) { CvScalar diff = cvScalar(centerX - eyeX, centerY - eyeY, centerZ - eyeZ, 0.0); double radius = norm(diff); double zoom = radius + diffX/10; myScene->pose.eye.val[0] = centerX + zoom * diff.val[0] / radius; myScene->pose.eye.val[1] = centerY + zoom * diff.val[1] / radius; } }
// From Real-time Collision Detection, p179. bool b2AABB::RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const { float32 tmin = -b2_maxFloat; float32 tmax = b2_maxFloat; b2Vec2 p = input.p1; b2Vec2 d = input.p2 - input.p1; b2Vec2 absD = b2Abs(d); b2Vec2 normal; for (int32 i = 0; i < 2; ++i) { if (absD(i) < b2_epsilon) { // Parallel. if (p(i) < lowerBound(i) || upperBound(i) < p(i)) { return false; } } else { float32 inv_d = 1.0f / d(i); float32 t1 = (lowerBound(i) - p(i)) * inv_d; float32 t2 = (upperBound(i) - p(i)) * inv_d; // Sign of the normal vector. float32 s = -1.0f; if (t1 > t2) { b2Swap(t1, t2); s = 1.0f; } // Push the min up if (t1 > tmin) { normal.SetZero(); normal(i) = s; tmin = t1; } // Pull the max down tmax = b2Min(tmax, t2); if (tmin > tmax) { return false; } } } // Does the ray start inside the box? // Does the ray intersect beyond the max fraction? if (tmin < 0.0f || input.maxFraction < tmin) { return false; } // Intersection. output->fraction = tmin; output->normal = normal; return true; }