1
1
/*
2
- Copyright (c) 2017-2022 Timur Gafarov
2
+ Copyright (c) 2017-2025 Timur Gafarov
3
3
4
4
Boost Software License - Version 1.0 - August 17th, 2003
5
5
Permission is hereby granted, free of charge, to any person or organization
@@ -44,7 +44,8 @@ class FreeviewComponent: EntityComponent
44
44
{
45
45
int prevMouseX;
46
46
int prevMouseY;
47
- float mouseTranslateSensibility;
47
+ float mouseTranslationSensibility;
48
+ float mouseRotationSensibility;
48
49
float mouseZoomSensibility;
49
50
50
51
Vector3f target;
@@ -53,37 +54,22 @@ class FreeviewComponent: EntityComponent
53
54
float distanceToTagret;
54
55
float smoothDistanceToTagret;
55
56
57
+ Vector3f rotation;
58
+ Vector3f smoothRotation;
59
+
60
+ float translationStiffness;
61
+ float rotationStiffness;
56
62
float zoomStiffness;
57
- float translateStiffness;
58
63
59
- Quaternionf rotPitch;
60
- Quaternionf rotTurn;
61
- Quaternionf rotRoll;
64
+ Quaternionf orientation;
62
65
Matrix4x4f transform;
63
66
Matrix4x4f invTransform;
64
67
65
- float rotPitchTheta;
66
- float rotTurnTheta;
67
- float rotRollTheta;
68
-
69
- float pitchCurrentTheta;
70
- float pitchTargetTheta;
71
- float turnCurrentTheta;
72
- float turnTargetTheta;
73
- float rollCurrentTheta;
74
- float rollTargetTheta;
75
-
76
- float currentMove;
77
- float targetMove;
78
-
79
- float currentStrafe;
80
- float targetStrafe;
81
-
82
68
bool active;
83
69
84
- bool enableTranslation ;
85
- bool enableRotation ;
86
- bool enableZoom ;
70
+ bool enableMouseTranslation ;
71
+ bool enableMouseRotation ;
72
+ bool enableMouseZoom ;
87
73
88
74
this (EventManager em, Entity e)
89
75
{
@@ -95,7 +81,9 @@ class FreeviewComponent: EntityComponent
95
81
{
96
82
prevMouseX = eventManager.mouseX;
97
83
prevMouseY = eventManager.mouseY;
98
- mouseTranslateSensibility = 0.1f ;
84
+
85
+ mouseTranslationSensibility = 0.1f ;
86
+ mouseRotationSensibility = 1.0f ;
99
87
mouseZoomSensibility = 0.2f ;
100
88
101
89
target = Vector3f(0.0f , 0.0f , 0.0f );
@@ -104,40 +92,22 @@ class FreeviewComponent: EntityComponent
104
92
distanceToTagret = 20.0f ;
105
93
smoothDistanceToTagret = 20.0f ;
106
94
107
- zoomStiffness = 0.5f ;
108
- translateStiffness = 1.0f ;
95
+ rotation = Vector3f( 45.0f , 45.0f , 0.0f ) ;
96
+ smoothRotation = rotation ;
109
97
110
- rotPitch = rotationQuaternion(Vector3f(1.0f , 0.0f , 0.0f ), 0.0f );
111
- rotTurn = rotationQuaternion(Vector3f(0.0f , 1.0f , 0.0f ), 0.0f );
112
- rotRoll = rotationQuaternion(Vector3f(0.0f , 0.0f , 1.0f ), 0.0f );
98
+ translationStiffness = 1.0f ;
99
+ rotationStiffness = 1.0f ;
100
+ zoomStiffness = 1.0f ;
101
+
102
+ orientation = Quaternionf.identity;
113
103
transform = Matrix4x4f.identity;
114
104
invTransform = Matrix4x4f.identity;
115
-
116
- rotPitchTheta = 0.0f ;
117
- rotTurnTheta = 0.0f ;
118
- rotRollTheta = 0.0f ;
119
-
120
- pitchCurrentTheta = 0.0f ;
121
- pitchTargetTheta = 0.0f ;
122
- turnCurrentTheta = 0.0f ;
123
- turnTargetTheta = 0.0f ;
124
- rollCurrentTheta = 0.0f ;
125
- rollTargetTheta = 0.0f ;
126
-
127
- currentMove = 0.0f ;
128
- targetMove = 0.0f ;
129
-
130
- currentStrafe = 0.0f ;
131
- targetStrafe = 0.0f ;
132
-
133
- pitch(45.0f );
134
- turn(45.0f );
135
105
136
106
active = true ;
137
107
138
- enableTranslation = true ;
139
- enableRotation = true ;
140
- enableZoom = true ;
108
+ enableMouseTranslation = true ;
109
+ enableMouseRotation = true ;
110
+ enableMouseZoom = true ;
141
111
142
112
transformEntity();
143
113
}
@@ -148,47 +118,52 @@ class FreeviewComponent: EntityComponent
148
118
149
119
if (active)
150
120
{
151
- if (eventManager.mouseButtonPressed[MB_RIGHT ] && enableTranslation )
121
+ if (eventManager.mouseButtonPressed[MB_RIGHT ] && enableMouseTranslation )
152
122
{
153
- float shiftx = (eventManager.mouseX - prevMouseX) * mouseTranslateSensibility ;
154
- float shifty = - (eventManager.mouseY - prevMouseY) * mouseTranslateSensibility ;
123
+ float shiftx = (eventManager.mouseX - prevMouseX) * mouseTranslationSensibility ;
124
+ float shifty = - (eventManager.mouseY - prevMouseY) * mouseTranslationSensibility ;
155
125
Vector3f trans = up * shifty + right * shiftx;
156
126
target += trans;
157
127
}
158
- else if (eventManager.mouseButtonPressed[MB_LEFT ] && eventManager.keyPressed[KEY_LCTRL ] && enableZoom )
128
+ else if (eventManager.mouseButtonPressed[MB_LEFT ] && eventManager.keyPressed[KEY_LCTRL ] && enableMouseZoom )
159
129
{
160
130
float shiftx = (eventManager.mouseX - prevMouseX) * mouseZoomSensibility;
161
131
float shifty = (eventManager.mouseY - prevMouseY) * mouseZoomSensibility;
162
132
zoom(shiftx + shifty);
163
133
}
164
- else if (eventManager.mouseButtonPressed[MB_LEFT ] && enableRotation )
134
+ else if (eventManager.mouseButtonPressed[MB_LEFT ] && enableMouseRotation )
165
135
{
166
- float t = (eventManager.mouseX - prevMouseX);
167
- float p = (eventManager.mouseY - prevMouseY);
168
- pitchSmooth(p, 0.25f );
169
- turnSmooth(t, 0.25f );
136
+ float turn = (eventManager.mouseX - prevMouseX) * mouseRotationSensibility;
137
+ float pitch = (eventManager.mouseY - prevMouseY) * mouseRotationSensibility;
138
+
139
+ rotation.x += pitch;
140
+ rotation.y += turn;
170
141
}
171
-
142
+
172
143
prevMouseX = eventManager.mouseX;
173
144
prevMouseY = eventManager.mouseY;
174
145
}
175
146
176
- smoothTarget += (target - smoothTarget) * translateStiffness ;
147
+ smoothTarget += (target - smoothTarget) * translationStiffness ;
177
148
smoothDistanceToTagret += (distanceToTagret - smoothDistanceToTagret) * zoomStiffness;
149
+ smoothRotation += (rotation - smoothRotation) * rotationStiffness;
178
150
179
151
transformEntity();
180
152
}
181
153
182
154
void transformEntity ()
183
155
{
184
- rotPitch = rotationQuaternion(Vector3f(1.0f , 0.0f , 0.0f ), degtorad(rotPitchTheta));
185
- rotTurn = rotationQuaternion(Vector3f(0.0f , 1.0f , 0.0f ), degtorad(rotTurnTheta));
186
- rotRoll = rotationQuaternion(Vector3f(0.0f , 0.0f , 1.0f ), degtorad(rotRollTheta));
187
-
188
- Quaternionf q = rotPitch * rotTurn * rotRoll;
189
- Matrix4x4f rot = q.toMatrix4x4();
190
- invTransform = translationMatrix(Vector3f(0.0f , 0.0f , - smoothDistanceToTagret)) * rot * translationMatrix(smoothTarget);
191
-
156
+ Quaternionf qPitch = rotationQuaternion(Vector3f(1.0f , 0.0f , 0.0f ), degtorad(smoothRotation.x));
157
+ Quaternionf qTurn = rotationQuaternion(Vector3f(0.0f , 1.0f , 0.0f ), degtorad(smoothRotation.y));
158
+ Quaternionf qRoll = rotationQuaternion(Vector3f(0.0f , 0.0f , 1.0f ), degtorad(smoothRotation.z));
159
+
160
+ orientation = qPitch * qTurn * qRoll;
161
+ Matrix4x4f orientationMatrix = orientation.toMatrix4x4();
162
+ invTransform =
163
+ translationMatrix(Vector3f(0.0f , 0.0f , - smoothDistanceToTagret)) *
164
+ orientationMatrix *
165
+ translationMatrix(smoothTarget);
166
+
192
167
transform = invTransform.inverse;
193
168
194
169
entity.prevTransformation = entity.transformation;
@@ -202,71 +177,49 @@ class FreeviewComponent: EntityComponent
202
177
203
178
void setRotation (float p, float t, float r)
204
179
{
205
- rotPitchTheta = p;
206
- rotTurnTheta = t;
207
- rotRollTheta = r;
180
+ rotation = Vector3f(p, t, r);
181
+ smoothRotation = rotation;
208
182
}
209
183
210
- void pitch (float theta)
211
- {
212
- rotPitchTheta += theta;
213
- }
214
-
215
- void turn (float theta)
216
- {
217
- rotTurnTheta += theta;
218
- }
219
-
220
- void roll (float theta)
221
- {
222
- rotRollTheta += theta;
223
- }
224
-
225
- float pitch ()
226
- {
227
- return rotPitchTheta;
228
- }
229
-
230
- float turn ()
184
+ // 2:1 isometry
185
+ void setIsometricRotation ()
231
186
{
232
- return rotTurnTheta ;
187
+ setRotation( 30.0f , 45.0f , 0.0f ) ;
233
188
}
234
-
235
- float roll ( )
189
+
190
+ void setRotationSmooth ( float p, float t, float r )
236
191
{
237
- return rotRollTheta ;
192
+ rotation = Vector3f(p, t, r) ;
238
193
}
239
-
240
- void pitchSmooth (float theta , float smooth )
194
+
195
+ void rotate (float p , float t, float r )
241
196
{
242
- pitchTargetTheta += theta;
243
- float pitchTheta = (pitchTargetTheta - pitchCurrentTheta) * smooth;
244
- pitchCurrentTheta += pitchTheta;
245
- pitch(pitchTheta);
197
+ rotation += Vector3f(p, t, r);
246
198
}
247
-
248
- void turnSmooth ( float theta, float smooth )
199
+
200
+ void setTarget (Vector3f pos )
249
201
{
250
- turnTargetTheta += theta;
251
- float turnTheta = (turnTargetTheta - turnCurrentTheta) * smooth;
252
- turnCurrentTheta += turnTheta;
253
- turn(turnTheta);
202
+ target = pos;
203
+ smoothTarget = pos;
254
204
}
255
-
256
- void rollSmooth ( float theta, float smooth )
205
+
206
+ void setTargetSmooth (Vector3f pos )
257
207
{
258
- rollTargetTheta += theta;
259
- float rollTheta = (rollTargetTheta - rollCurrentTheta) * smooth;
260
- rollCurrentTheta += rollTheta;
261
- roll(rollTheta);
208
+ target = pos;
262
209
}
263
210
264
211
void translateTarget (Vector3f pos)
265
212
{
266
213
target += pos;
267
214
}
268
-
215
+
269
216
void setZoom (float z)
217
+ {
218
+ distanceToTagret = z;
219
+ smoothDistanceToTagret = z;
220
+ }
221
+
222
+ void setZoomSmooth (float z)
270
223
{
271
224
distanceToTagret = z;
272
225
}
@@ -281,53 +234,19 @@ class FreeviewComponent: EntityComponent
281
234
return transform.translation();
282
235
}
283
236
284
- Vector3f right ()
285
- {
286
- return transform.right();
287
- }
288
-
289
- Vector3f up ()
290
- {
291
- return transform.up();
292
- }
293
-
294
237
Vector3f direction ()
295
238
{
296
239
return transform.forward();
297
240
}
298
241
299
- void strafe (float speed)
300
- {
301
- Vector3f forward;
302
- forward.x = cos(degtorad(rotTurnTheta));
303
- forward.y = 0.0f ;
304
- forward.z = sin(degtorad(rotTurnTheta));
305
- target += forward * speed;
306
- }
307
-
308
- void strafeSmooth (float speed, float smooth)
309
- {
310
- targetMove += speed;
311
- float movesp = (targetMove - currentMove) / smooth;
312
- currentMove += movesp;
313
- strafe(movesp);
314
- }
315
-
316
- void move (float speed)
242
+ Vector3f right ()
317
243
{
318
- Vector3f dir;
319
- dir.x = cos(degtorad(rotTurnTheta + 90.0f ));
320
- dir.y = 0.0f ;
321
- dir.z = sin(degtorad(rotTurnTheta + 90.0f ));
322
- target += dir * speed;
244
+ return transform.right();
323
245
}
324
246
325
- void moveSmooth ( float speed, float smooth )
247
+ Vector3f up ( )
326
248
{
327
- targetStrafe += speed;
328
- float strafesp = (targetStrafe - currentStrafe) / smooth;
329
- currentStrafe += strafesp;
330
- move(strafesp);
249
+ return transform.up();
331
250
}
332
251
333
252
void screenToWorld (
@@ -378,7 +297,7 @@ class FreeviewComponent: EntityComponent
378
297
379
298
override void onMouseWheel (int x, int y)
380
299
{
381
- if (! active || ! enableZoom )
300
+ if (! active || ! enableMouseZoom )
382
301
return ;
383
302
384
303
zoom(cast (float )y * mouseZoomSensibility);
0 commit comments