Skip to content

Commit 652152e

Browse files
authoredJan 1, 2022
Merge pull request #3270 from eggrobin/camera-orientation
SO(3) is not commutative
2 parents fa74acf + afe3f5a commit 652152e

File tree

7 files changed

+40
-10
lines changed

7 files changed

+40
-10
lines changed
 

Diff for: ‎ksp_plugin/frames.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ using CameraReference = Frame<serialization::Frame::PluginTag,
9999
Handedness::Left,
100100
serialization::Frame::CAMERA_REFERENCE>;
101101

102+
// |CameraReference|, rotated about its y axis by the angle of the planetarium
103+
// rotation. KSP compensates for the planetarium rotation so that the
104+
// orientation of the camera remains inertially fixed regardless of whether
105+
// World is rotating; we must undo this compensation in order for the camera to
106+
// be fixed in |CameraReference|.
107+
using CameraCompensatedReference =
108+
Frame<serialization::Frame::PluginTag,
109+
Arbitrary,
110+
Handedness::Left,
111+
serialization::Frame::CAMERA_COMPENSATED_REFERENCE>;
112+
102113
// A nonrotating referencence frame comoving with the sun with the same axes as
103114
// |AliceWorld|. Since it is nonrotating (though not inertial), differences
104115
// between velocities are consistent with those in an inertial reference frame.
@@ -162,6 +173,7 @@ using internal_frames::ApparentWorld;
162173
using internal_frames::Barycentric;
163174
using internal_frames::BodyWorld;
164175
using internal_frames::Camera;
176+
using internal_frames::CameraCompensatedReference;
165177
using internal_frames::CameraReference;
166178
using internal_frames::CelestialSphere;
167179
using internal_frames::EccentricPart;

Diff for: ‎ksp_plugin/interface_renderer.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ WXYZ __cdecl principia__CameraReferenceRotation(Plugin* const plugin) {
145145
return m.Return(
146146
ToWXYZ(GetRenderer(plugin)
147147
.CameraReferenceRotation(plugin->CurrentTime(),
148-
plugin->PlanetariumRotation())
148+
plugin->PlanetariumRotation(),
149+
plugin->CameraCompensation())
149150
.quaternion()));
150151
}
151152

Diff for: ‎ksp_plugin/plugin.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,11 @@ Rotation<Barycentric, AliceSun> const& Plugin::PlanetariumRotation() const {
12941294
return *cached_planetarium_rotation_;
12951295
}
12961296

1297+
Rotation<CameraCompensatedReference, CameraReference> const&
1298+
Plugin::CameraCompensation() const {
1299+
return *camera_compensation_;
1300+
}
1301+
12971302
Renderer& Plugin::renderer() {
12981303
return *renderer_;
12991304
}
@@ -1574,6 +1579,10 @@ void Plugin::UpdatePlanetariumRotation() {
15741579
Bivector<double, PlanetariumFrame>({0, 0, 1}),
15751580
DefinesFrame<AliceSun>{}) *
15761581
to_planetarium;
1582+
camera_compensation_ = Rotation<CameraCompensatedReference, CameraReference>(
1583+
-planetarium_rotation_,
1584+
Bivector<double, CameraReference>({0, 1, 0}),
1585+
DefinesFrame<CameraCompensatedReference>{});
15771586
}
15781587

15791588
Velocity<World> Plugin::VesselVelocity(

Diff for: ‎ksp_plugin/plugin.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ class Plugin {
443443
// this change of basis is all that's required to convert relative velocities
444444
// or displacements between simultaneous events.
445445
virtual Rotation<Barycentric, AliceSun> const& PlanetariumRotation() const;
446+
virtual Rotation<CameraCompensatedReference, CameraReference> const&
447+
CameraCompensation() const;
446448

447449
virtual Renderer& renderer();
448450
virtual Renderer const& renderer() const;
@@ -534,6 +536,8 @@ class Plugin {
534536

535537
Angle planetarium_rotation_;
536538
std::optional<Rotation<Barycentric, AliceSun>> cached_planetarium_rotation_;
539+
std::optional<Rotation<CameraCompensatedReference, CameraReference>>
540+
camera_compensation_;
537541
// The game epoch in real time.
538542
Instant game_epoch_;
539543
// The current in-game universal time.

Diff for: ‎ksp_plugin/renderer.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,19 @@ RigidTransformation<World, Navigation> Renderer::WorldToPlotting(
278278
WorldToBarycentric(time, sun_world_position, planetarium_rotation);
279279
}
280280

281-
Rotation<CameraReference, CelestialSphere> Renderer::CameraReferenceRotation(
281+
Rotation<CameraCompensatedReference, World> Renderer::CameraReferenceRotation(
282282
Instant const& time,
283-
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const {
283+
Rotation<Barycentric, AliceSun> const& planetarium_rotation,
284+
Rotation<CameraCompensatedReference, CameraReference> const&
285+
camera_compensation) const {
284286
Permutation<Barycentric, CelestialSphere> const celestial_mirror(
285287
OddPermutation::XZY);
286288
Permutation<CameraReference, Navigation> const camera_mirror(
287289
OddPermutation::XZY);
288-
auto const result =
289-
celestial_mirror.Forget<OrthogonalMap>() *
290-
GetPlottingFrame()->FromThisFrameAtTime(time).orthogonal_map() *
291-
camera_mirror.Forget<OrthogonalMap>();
292-
return result.AsRotation();
290+
return (BarycentricToWorld(planetarium_rotation) *
291+
GetPlottingFrame()->FromThisFrameAtTime(time).orthogonal_map() *
292+
camera_mirror.Forget<OrthogonalMap>()).AsRotation() *
293+
camera_compensation;
293294
}
294295

295296
void Renderer::WriteToMessage(

Diff for: ‎ksp_plugin/renderer.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ class Renderer {
159159
Position<World> const& sun_world_position,
160160
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const;
161161

162-
virtual Rotation<CameraReference, CelestialSphere> CameraReferenceRotation(
162+
virtual Rotation<CameraCompensatedReference, World> CameraReferenceRotation(
163163
Instant const& time,
164-
Rotation<Barycentric, AliceSun> const& planetarium_rotation) const;
164+
Rotation<Barycentric, AliceSun> const& planetarium_rotation,
165+
Rotation<CameraCompensatedReference, CameraReference> const&
166+
camera_compensation) const;
165167

166168
virtual void WriteToMessage(not_null<serialization::Renderer*> message) const;
167169

Diff for: ‎serialization/geometry.proto

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ message Frame {
159159
BODY_WORLD = 9;
160160
// BUBBLE = 13;
161161
CAMERA = 14;
162+
CAMERA_COMPENSATED_REFERENCE = 21;
162163
CAMERA_REFERENCE = 17;
163164
CELESTIAL_SPHERE = 8;
164165
ECCENTRIC_PART = 20;

0 commit comments

Comments
 (0)
Please sign in to comment.