1 /*
2 Copyright (c) 2017-2020 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.ui.firstpersonview;
29 
30 import std.math;
31 
32 import dlib.math.vector;
33 import dlib.math.matrix;
34 import dlib.math.quaternion;
35 import dlib.math.transformation;
36 import dlib.math.utils;
37 
38 import dagon.core.event;
39 import dagon.core.keycodes;
40 import dagon.core.time;
41 import dagon.graphics.entity;
42 
43 class FirstPersonViewComponent: EntityComponent
44 {
45     int prevMouseX = 0;
46     int prevMouseY = 0;
47     
48     float pitch = 0.0f;
49     float turn = 0.0f;
50 
51     float mouseSensibility = 0.1f;
52     float axisSensibility = 20.0f;
53     
54     bool active = true;
55     bool mouseActive = true;
56     
57     float pitchLimitMax = 60.0f;
58     float pitchLimitMin = -60.0f;
59     
60     Quaternionf baseOrientation = Quaternionf.identity;
61     
62     this(EventManager em, Entity e)
63     {
64         super(em, e);
65         reset();
66     }
67     
68     void reset()
69     {
70         pitch = 0.0f;
71         turn = 0.0f;
72         eventManager.setMouseToCenter();
73         prevMouseX = eventManager.mouseX;
74         prevMouseY = eventManager.mouseY;
75     }
76     
77     override void update(Time time)
78     {
79         processEvents();
80         
81         if (active & mouseActive)
82         {
83             float mouseRelH =  (eventManager.mouseX - prevMouseX) * mouseSensibility;
84             float mouseRelV = (eventManager.mouseY - prevMouseY) * mouseSensibility;
85             float axisV = inputManager.getAxis("vertical") * axisSensibility * mouseSensibility;
86             float axisH = inputManager.getAxis("horizontal") * axisSensibility * mouseSensibility;
87             pitch -= mouseRelV + axisV;
88             turn -= mouseRelH + axisH;
89             
90             if (pitch > pitchLimitMax)
91             {
92                 pitch = pitchLimitMax;
93             }
94             else if (pitch < pitchLimitMin)
95             {
96                 pitch = pitchLimitMin;
97             }
98             
99             eventManager.setMouse(prevMouseX, prevMouseY);
100         }
101         
102         auto rotPitch = rotationQuaternion(Vector3f(1.0f,0.0f,0.0f), degtorad(pitch));
103         auto rotTurn = rotationQuaternion(Vector3f(0.0f,1.0f,0.0f), degtorad(turn));
104         
105         Quaternionf orientation = baseOrientation * rotTurn * rotPitch;
106         
107         entity.transformation =
108             (translationMatrix(entity.position) *
109             orientation.toMatrix4x4 *
110             scaleMatrix(entity.scaling));
111         
112         entity.invTransformation = entity.transformation.inverse;
113         
114         if (entity.parent)
115         {
116             entity.absoluteTransformation = entity.parent.absoluteTransformation * entity.transformation;
117             entity.invAbsoluteTransformation = entity.invTransformation * entity.parent.invAbsoluteTransformation;
118             entity.prevAbsoluteTransformation = entity.parent.prevAbsoluteTransformation * entity.prevTransformation;
119         }
120         else
121         {
122             entity.absoluteTransformation = entity.transformation;
123             entity.invAbsoluteTransformation = entity.invTransformation;
124             entity.prevAbsoluteTransformation = entity.prevTransformation;
125         }
126     }
127     
128     override void onResize(int width, int height)
129     {
130         prevMouseX = width / 2;
131         prevMouseY = height / 2;
132     }
133     
134     override void onFocusGain()
135     {
136         mouseActive = true;
137     }
138     
139     override void onFocusLoss()
140     {
141         mouseActive = false;
142     }
143     
144     void moveForward(float speed)
145     {
146         Vector3f forward = entity.transformation.forward;
147         entity.position -= forward.normalized * speed;
148     }
149     
150     void moveBack(float speed)
151     {
152         Vector3f forward = entity.transformation.forward;
153         entity.position += forward.normalized * speed;
154     }
155     
156     void strafeRight(float speed)
157     {
158         Vector3f right = entity.transformation.right;
159         entity.position += right.normalized * speed;
160     }
161     
162     void strafeLeft(float speed)
163     {
164         Vector3f right = entity.transformation.right;
165         entity.position -= right.normalized * speed;
166     }
167 }