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 53 bool active = true; 54 55 this(EventManager em, Entity e) 56 { 57 super(em, e); 58 reset(); 59 } 60 61 void reset() 62 { 63 pitch = 0.0f; 64 turn = 0.0f; 65 eventManager.setMouseToCenter(); 66 prevMouseX = eventManager.mouseX; 67 prevMouseY = eventManager.mouseY; 68 } 69 70 override void update(Time time) 71 { 72 processEvents(); 73 74 if (active) 75 { 76 float turn_m = (eventManager.mouseX - prevMouseX) * mouseSensibility; 77 float pitch_m = (eventManager.mouseY - prevMouseY) * mouseSensibility; 78 79 pitch -= pitch_m; 80 turn -= turn_m; 81 82 float pitchLimitMax = 60.0f; 83 float pitchLimitMin = -60.0f; 84 if (pitch > pitchLimitMax) 85 { 86 pitch = pitchLimitMax; 87 } 88 else if (pitch < pitchLimitMin) 89 { 90 pitch = pitchLimitMin; 91 } 92 93 eventManager.setMouse(prevMouseX, prevMouseY); 94 } 95 96 auto rotPitch = rotationQuaternion(Vector3f(1.0f,0.0f,0.0f), degtorad(pitch)); 97 auto rotTurn = rotationQuaternion(Vector3f(0.0f,1.0f,0.0f), degtorad(turn)); 98 99 Quaternionf q = rotTurn * rotPitch; 100 101 entity.transformation = 102 translationMatrix(entity.position) * 103 q.toMatrix4x4 * 104 scaleMatrix(entity.scaling); 105 106 entity.invTransformation = entity.transformation.inverse; 107 108 if (entity.parent) 109 { 110 entity.absoluteTransformation = entity.parent.absoluteTransformation * entity.transformation; 111 entity.invAbsoluteTransformation = entity.invTransformation * entity.parent.invAbsoluteTransformation; 112 entity.prevAbsoluteTransformation = entity.parent.prevAbsoluteTransformation * entity.prevTransformation; 113 } 114 else 115 { 116 entity.absoluteTransformation = entity.transformation; 117 entity.invAbsoluteTransformation = entity.invTransformation; 118 entity.prevAbsoluteTransformation = entity.prevTransformation; 119 } 120 } 121 }