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 this(EventManager em, Entity e) 61 { 62 super(em, e); 63 reset(); 64 } 65 66 void reset() 67 { 68 pitch = 0.0f; 69 turn = 0.0f; 70 eventManager.setMouseToCenter(); 71 prevMouseX = eventManager.mouseX; 72 prevMouseY = eventManager.mouseY; 73 } 74 75 override void update(Time time) 76 { 77 processEvents(); 78 79 if (active & mouseActive) 80 { 81 float mouseRelH = (eventManager.mouseX - prevMouseX) * mouseSensibility; 82 float mouseRelV = (eventManager.mouseY - prevMouseY) * mouseSensibility; 83 float axisV = inputManager.getAxis("vertical") * axisSensibility * mouseSensibility; 84 float axisH = inputManager.getAxis("horizontal") * axisSensibility * mouseSensibility; 85 pitch -= mouseRelV + axisV; 86 turn -= mouseRelH + axisH; 87 88 if (pitch > pitchLimitMax) 89 { 90 pitch = pitchLimitMax; 91 } 92 else if (pitch < pitchLimitMin) 93 { 94 pitch = pitchLimitMin; 95 } 96 97 eventManager.setMouse(prevMouseX, prevMouseY); 98 } 99 100 auto rotPitch = rotationQuaternion(Vector3f(1.0f,0.0f,0.0f), degtorad(pitch)); 101 auto rotTurn = rotationQuaternion(Vector3f(0.0f,1.0f,0.0f), degtorad(turn)); 102 103 Quaternionf q = rotTurn * rotPitch; 104 105 entity.transformation = 106 translationMatrix(entity.position) * 107 q.toMatrix4x4 * 108 scaleMatrix(entity.scaling); 109 110 entity.invTransformation = entity.transformation.inverse; 111 112 if (entity.parent) 113 { 114 entity.absoluteTransformation = entity.parent.absoluteTransformation * entity.transformation; 115 entity.invAbsoluteTransformation = entity.invTransformation * entity.parent.invAbsoluteTransformation; 116 entity.prevAbsoluteTransformation = entity.parent.prevAbsoluteTransformation * entity.prevTransformation; 117 } 118 else 119 { 120 entity.absoluteTransformation = entity.transformation; 121 entity.invAbsoluteTransformation = entity.invTransformation; 122 entity.prevAbsoluteTransformation = entity.prevTransformation; 123 } 124 } 125 126 override void onResize(int width, int height) 127 { 128 prevMouseX = width / 2; 129 prevMouseY = height / 2; 130 } 131 132 override void onFocusGain() 133 { 134 mouseActive = true; 135 } 136 137 override void onFocusLoss() 138 { 139 mouseActive = false; 140 } 141 142 void moveForward(float speed) 143 { 144 Vector3f forward = entity.transformation.forward; 145 entity.position -= forward.normalized * speed; 146 } 147 148 void moveBack(float speed) 149 { 150 Vector3f forward = entity.transformation.forward; 151 entity.position += forward.normalized * speed; 152 } 153 154 void strafeRight(float speed) 155 { 156 Vector3f right = entity.transformation.right; 157 entity.position += right.normalized * speed; 158 } 159 160 void strafeLeft(float speed) 161 { 162 Vector3f right = entity.transformation.right; 163 entity.position -= right.normalized * speed; 164 } 165 }