Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Ensure correct spin direction
Browse files Browse the repository at this point in the history
Ported mapbox/mapbox-gl-js#821 as well as `util.wrap()` from mapbox-gl-js.

Fixes #1199.
  • Loading branch information
1ec5 committed Apr 19, 2015
1 parent f802385 commit 8b44cfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions include/mbgl/util/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ T clamp(T value, T min, T max) {
return value < min ? min : (value > max ? max : value);
}

template <typename T>
T wrap(T value, T min, T max) {
T d = max - min;
return value == max ? value : std::fmod((std::fmod((value - min), d) + d), d) + min;
}

template <typename T>
T smoothstep(T edge0, T edge1, T x) {
T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1));
Expand Down
22 changes: 16 additions & 6 deletions src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@

using namespace mbgl;

/** Converts the given angle (in radians) to be numerically close to the anchor angle, allowing it to be interpolated properly without sudden jumps. */
static double _normalizeAngle(double angle, double anchorAngle)
{
angle = util::wrap(angle, -M_PI, M_PI);
double diff = std::abs(angle - anchorAngle);
if (std::abs(angle - util::M2PI - anchorAngle) < diff) {
angle -= util::M2PI;
}
if (std::abs(angle + util::M2PI - anchorAngle) < diff) {
angle += util::M2PI;
}

return angle;
}

Transform::Transform(View &view_)
: view(view_)
{
Expand Down Expand Up @@ -330,12 +345,7 @@ void Transform::_setAngle(double new_angle, const Duration duration) {
MapChangeRegionWillChangeAnimated :
MapChangeRegionWillChange);

while (new_angle > M_PI)
new_angle -= util::M2PI;
while (new_angle <= -M_PI)
new_angle += util::M2PI;

final.angle = new_angle;
final.angle = _normalizeAngle(new_angle, current.angle);

if (duration == Duration::zero()) {
current.angle = final.angle;
Expand Down

0 comments on commit 8b44cfe

Please sign in to comment.