File: PsychGetPositionYawMatrix.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (55 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function M = PsychGetPositionYawMatrix(translation, yawRotation)
% M = PsychGetPositionYawMatrix(translation, yawRotation);
%
% Builds a 4x4 OpenGL compatible (right hand side post-multiply)
% matrix for a 3D translation followed by a rotation around the
% y-axis. Useful for positioning an observer in 3D space, then
% giving the observer a specific "looking direction" or heading.
%
% You can also use this function for incremental position and
% orientation updates by simply post-multiplying the result of
% this function to the result of a previous call to this function,
% e.g.:
%
% 1. Select start position and orientation:
%
%    M = PsychGetPositionYawMatrix(startPosition, startHeading);
%
% 2. Update location relative to current heading direction, then
%    change new heading by deltaHeading degrees for initiating a
%    turn:
%
%    M = M * PsychGetPositionYawMatrix(PositionIncrement, deltaHeading);
%
% Initializing an observer position with step 1, then updating it
% by repeating step 2, e.g., driven by keyboard input, mouse or
% gamepad input, or some other input device or "game logic" allows
% to move the observers body through the scene in a "natural" way.
%
%
% Parameters:
%
% 'translation' a 3 component translation vector [tx, ty, tz].
%
% 'yawRotation' a rotation angle in degrees along the y-axis, also
% known as heading direction or upright direction.
%

% History:
% 28-Sep-2015  mk  Written.

% Translation matrix:
T = diag([1 1 1 1]);
T(1:3, 4) = translation;

% Rotation matrix around y axis:
alpha = yawRotation / 180 * pi;
R = [cos(alpha)  0  sin(alpha) 0 ; ...
     0           1  0          0 ; ...
     -sin(alpha) 0  cos(alpha) 0 ; ...
     0           0  0          1];

% First translate, then rotate:
M = T * R;

end