Răsfoiți Sursa

implement speed and move

Michael 2 ani în urmă
părinte
comite
2286e0c7b1
2 a modificat fișierele cu 20 adăugiri și 4 ștergeri
  1. 19 3
      client/mapView/MapView.cpp
  2. 1 1
      lib/Point.h

+ 19 - 3
client/mapView/MapView.cpp

@@ -131,12 +131,28 @@ void MapView::onMapSwiped(const Point & viewPosition)
 void MapView::postSwipe(uint32_t msPassed) {
 	if(!actions->dragActive && swipeHistory.size() > 0)
 	{
-		Point diff = swipeHistory.end()->second - swipeHistory.begin()->second;
-		double angle = diff.angle();
+		Point diff = Point(0, 0);
+		for (auto & x : swipeHistory)
+			diff += x.second;
 
-		std::cout << angle;
+		uint64_t timediff = swipeHistory.rbegin()->first - swipeHistory.begin()->first;
+
+		postSwipeAngle = diff.angle();
+		postSwipeSpeed = static_cast<double>(diff.length()) / static_cast<double>(timediff); // unit: pixel/millisecond
+
+		std::cout << postSwipeAngle << "   " << postSwipeSpeed << "   " << diff.x << "x" << diff.y << "   " << timediff << "\n";
 		swipeHistory.clear();
 	}
+	if(postSwipeSpeed > 0.1) {
+		double len = postSwipeSpeed * static_cast<double>(msPassed);
+		Point delta = Point(len * cos(postSwipeAngle), len * sin(postSwipeAngle));
+
+		std::cout << len << "   " << delta.x << "x" << delta.y << "\n";
+
+		controller->setViewCenter(model->getMapViewCenter() + delta, model->getLevel());
+
+		postSwipeSpeed /= 1.1;
+	}
 }
 
 void MapView::onCenteredTile(const int3 & tile)

+ 1 - 1
lib/Point.h

@@ -118,7 +118,7 @@ public:
 
 	double angle() const
 	{
-		return std::atan2(y, x) * 180.0 / M_PI;
+		return std::atan2(y, x); // rad
 	}
 
 	template <typename Handler>