Skip to content

Commit

Permalink
fixes rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmermannubique committed Apr 3, 2024
1 parent ce330c3 commit f8966d7
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions shared/src/map/camera/MapCamera2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,15 @@ void MapCamera2d::setRotation(float angle, bool animated) {
if (cameraFrozen)
return;
double newAngle = (angle > 360 || angle < 0) ? fmod(angle + 360.0, 360.0) : angle;

if (animated) {
double currentAngle = fmod(this->angle, 360.0);
if (abs(currentAngle - newAngle) > abs(currentAngle - (newAngle + 360.0))) {
newAngle += 360.0;
} else if (abs(currentAngle - newAngle) > abs(currentAngle - (newAngle - 360.0))) {
newAngle -= 360.0;
}

std::lock_guard<std::recursive_mutex> lock(animationMutex);
rotationAnimation = std::make_shared<DoubleAnimation>(
DEFAULT_ANIM_LENGTH, currentAngle, newAngle, InterpolatorFunction::Linear,
Expand All @@ -253,17 +255,24 @@ void MapCamera2d::setRotation(float angle, bool animated) {
rotationAnimation->start();
mapInterface->invalidate();
} else {
double angleDiff = newAngle - this->angle;
Coord centerScreen = centerPosition;
if(coordAnimation) {
// if a coordinate animation is running during rotation,
// we go right to the correct end coordinate such that the rotation
// is performed correctly
centerPosition = coordAnimation->endValue;
coordAnimation->cancel();
coordAnimation = nullptr;
}

Coord realCenter = getCenterPosition();
Vec2D rotatedDiff =
Vec2DHelper::rotate(Vec2D(centerScreen.x - realCenter.x, centerScreen.y - realCenter.y), Vec2D(0.0, 0.0), angleDiff);
const auto [adjPosition, adjZoom] =
getBoundsCorrectedCoords(adjustCoordForPadding(Coord(mapCoordinateSystem.identifier, realCenter.x + rotatedDiff.x, realCenter.y + rotatedDiff.y, centerPosition.z), zoom), zoom);
centerPosition = adjPosition;
zoom = adjZoom;
Vec2D padVec = Vec2D(0.5 * (paddingLeft - paddingRight) * screenPixelAsRealMeterFactor * zoom,
0.5 * (paddingBottom - paddingTop) * screenPixelAsRealMeterFactor * zoom);
Vec2D rotPadVec = Vec2DHelper::rotate(padVec, Vec2D(0.0, 0.0), newAngle);

this->centerPosition.x = realCenter.x - rotPadVec.x;
this->centerPosition.y = realCenter.y - rotPadVec.y;
this->angle = newAngle;

notifyListeners(ListenerType::ROTATION | ListenerType::BOUNDS);
mapInterface->invalidate();
}
Expand Down

0 comments on commit f8966d7

Please sign in to comment.