OSDN Git Service

Prevent camera moving square from getting stuck
authorBjoern Johansson <bjoernj@google.com>
Thu, 12 Jan 2017 20:42:41 +0000 (12:42 -0800)
committerBjoern Johansson <bjoernj@google.com>
Wed, 25 Jan 2017 22:11:48 +0000 (22:11 +0000)
The square in the emulated camera could get stuck bouncing around along
one edge if the time between two frames took long enough that the next
frame didn't clear the edge entirely. The coordinate for the square
would then remain outside the view and simply be negated once again,
causing it to bounce back immediately. On the next frame it would then
be negated again and so on.

This fixes the issue by correctly updating the relative position on
every bounce to make sure it's on the inside of the edge after the
bounce.

BUG: 34252276
Change-Id: I967fbef70d667e9977aaab9a11d07d7d6e8cd161
(cherry picked from commit 29dd289d7593bb4446b9d13a175a9cb2cfece01b)

camera/EmulatedFakeCameraDevice.cpp

index d59cdbf..d3cdd78 100755 (executable)
@@ -308,17 +308,23 @@ void EmulatedFakeCameraDevice::drawCheckerboard(void* buffer)
     int squareY = mSquareY * mFrameHeight;
     if (squareX + squareSize > mFrameWidth) {
         mSquareXSpeed = -mSquareXSpeed;
-        squareX -= 2 * (squareX + squareSize - mFrameWidth);
+        double relativeWidth = static_cast<double>(squareSize) / mFrameWidth;
+        mSquareX -= 2.0 * (mSquareX + relativeWidth - 1.0);
+        squareX = mSquareX * mFrameWidth;
     } else if (squareX < 0) {
         mSquareXSpeed = -mSquareXSpeed;
-        squareX = -squareX;
+        mSquareX = -mSquareX;
+        squareX = mSquareX * mFrameWidth;
     }
     if (squareY + squareSize > mFrameHeight) {
         mSquareYSpeed = -mSquareYSpeed;
-        squareY -= 2 * (squareY + squareSize - mFrameHeight);
+        double relativeHeight = static_cast<double>(squareSize) / mFrameHeight;
+        mSquareY -= 2.0 * (mSquareY + relativeHeight - 1.0);
+        squareY = mSquareY * mFrameHeight;
     } else if (squareY < 0) {
         mSquareYSpeed = -mSquareYSpeed;
-        squareY = -squareY;
+        mSquareY = -mSquareY;
+        squareY = mSquareY * mFrameHeight;
     }
 
     if (now - mLastColorChange > kSquareColorChangeIntervalNs) {