# World Rotation in 3D

**URL:** https://discourse.vtk.org/t/world-rotation-in-3d/4020
**Category:** Support
**Created:** [August 19, 2020, 1:08pm UTC](https://discourse.vtk.org/t/world-rotation-in-3d/4020 "2020-08-19T13:08:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AlexG](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@AlexG](https://discourse.vtk.org/u/AlexG)
#### Post date: [August 19, 2020, 1:08pm UTC](https://discourse.vtk.org/t/world-rotation-in-3d/4020/1 "2020-08-19T13:08:37Z")

</div>

Hi,

I have a question concerning the rotation of the whole VR Environment.  
With the currently implemented OnPinch() and OnPan() Methods the whole environment can be scaled and moved. Now i want to use the OnRotate() method to rotate the whole world.

I think this is not working as in the OnPinch() method the vtkInteractorStyle3D::SetScale() method is called. Here the position of the HMD should be moved to compensate for the scaling. In a comment it is noted:

> “New camera properties are overridden by virtual reality render window if head-mounted display is tracked”

Is this also the case for the rotation of the camera?

I tried:

> vtkRenderWindowInteractor3D\* rwi = static\_cast\<vtkRenderWindowInteractor3D\*\>(this-\>Interactor);  
> vtkCamera\* camera = this-\>CurrentRenderer-\>GetActiveCamera();  
> double angle = rwi-\>GetRotation() - rwi-\>GetLastRotation();

- `camera->Roll(angle);`
- `vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();`  
`transform->PostMultiply();`  
`transform->Translate(0,0,0);`  
`transform->RotateY(angle);`  
`transform->Translate(camerPos[0], camerPos[1], camerPos[2]);`  
-`camera->SetPosition(transform->TransformPoint(camera->GetPosition()))`

But nothing moves? Is there a workaround besides rotating every actor in the scene?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 19, 2020, 1:52pm UTC](https://discourse.vtk.org/t/world-rotation-in-3d/4020/2 "2020-08-19T13:52:24Z")

</div>

This is implemented in [3D Slicer’s VirtualReality extension](https://github.com/KitwareMedical/SlicerVirtualReality). You may consider porting it back to VTK.

---

<div class="post-metadata">

### Author: ![AlexG](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@AlexG](https://discourse.vtk.org/u/AlexG)
#### Post date: [August 24, 2020, 5:57am UTC](https://discourse.vtk.org/t/world-rotation-in-3d/4020/3 "2020-08-24T05:57:32Z")

</div>

Thank you very much for the reply!!  
I have taken a look at your extension. I tried to get the rotation working by setting the PhysicalToWorldMatrix:

> vtkOpenVRRenderWindowInteractor\* rwi = static\_cast\<vtkOpenVRRenderWindowInteractor\*\>(this-\>Interactor);  
> vtkOpenVRRenderWindow\* rw = static\_cast\<vtkOpenVRRenderWindow\*\>(rwi-\>GetRenderWindow());
> 
> vtkCamera\* camera = this-\>CurrentRenderer-\>GetActiveCamera();  
> double\* cameraPos = camera-\>GetPosition();  
> double angle = rwi-\>GetRotation() - rwi-\>GetLastRotation();  
> double physicalScale = rwi-\>GetPhysicalScale();
> 
> vtkSmartPointer m = vtkSmartPointer::New();  
> rw-\>GetPhysicalToWorldMatrix(m);
> 
> vtkSmartPointer transform = vtkSmartPointer::New();  
> transform-\>PostMultiply();  
> transform-\>SetMatrix(m);  
> transform-\>RotateY(angle);
> 
> vtkSmartPointer tMInv = vtkSmartPointer::New();  
> vtkMatrix4x4::Invert(transform-\>GetMatrix(), tMInv);
> 
> rw-\>SetPhysicalToWorldMatrix(transform-\>GetMatrix());

The problem is, although the world can now be rotated, I see everything double until the end of the rotation. That means the original orientation of the Wolrd and its Objects remains visible as well as its currently rotated state until i exit the rotation mode - then i only see the correct rotated objects.  
(I start the rotation by pressing the grip button on both controllers which calls the OnRotate() method)

Did you run into the same problems?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 24, 2020, 3:04pm UTC](https://discourse.vtk.org/t/world-rotation-in-3d/4020/4 "2020-08-24T15:04:26Z")

</div>

> [@AlexG](#):
>
> The problem is, although the world can now be rotated, I see everything double until the end of the rotation.

This should not happen. Maybe you need to update OpenVR driver to latest version and use very latest Slicer Preview Release.
