Skip to content

Commit 3c1dc2f

Browse files
committed
Simplify FreeviewComponent rotation
1 parent b97cf36 commit 3c1dc2f

File tree

1 file changed

+79
-160
lines changed

1 file changed

+79
-160
lines changed

src/dagon/ui/freeview.d

+79-160
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2017-2022 Timur Gafarov
2+
Copyright (c) 2017-2025 Timur Gafarov
33
44
Boost Software License - Version 1.0 - August 17th, 2003
55
Permission is hereby granted, free of charge, to any person or organization
@@ -44,7 +44,8 @@ class FreeviewComponent: EntityComponent
4444
{
4545
int prevMouseX;
4646
int prevMouseY;
47-
float mouseTranslateSensibility;
47+
float mouseTranslationSensibility;
48+
float mouseRotationSensibility;
4849
float mouseZoomSensibility;
4950

5051
Vector3f target;
@@ -53,37 +54,22 @@ class FreeviewComponent: EntityComponent
5354
float distanceToTagret;
5455
float smoothDistanceToTagret;
5556

57+
Vector3f rotation;
58+
Vector3f smoothRotation;
59+
60+
float translationStiffness;
61+
float rotationStiffness;
5662
float zoomStiffness;
57-
float translateStiffness;
5863

59-
Quaternionf rotPitch;
60-
Quaternionf rotTurn;
61-
Quaternionf rotRoll;
64+
Quaternionf orientation;
6265
Matrix4x4f transform;
6366
Matrix4x4f invTransform;
6467

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-
8268
bool active;
8369

84-
bool enableTranslation;
85-
bool enableRotation;
86-
bool enableZoom;
70+
bool enableMouseTranslation;
71+
bool enableMouseRotation;
72+
bool enableMouseZoom;
8773

8874
this(EventManager em, Entity e)
8975
{
@@ -95,7 +81,9 @@ class FreeviewComponent: EntityComponent
9581
{
9682
prevMouseX = eventManager.mouseX;
9783
prevMouseY = eventManager.mouseY;
98-
mouseTranslateSensibility = 0.1f;
84+
85+
mouseTranslationSensibility = 0.1f;
86+
mouseRotationSensibility = 1.0f;
9987
mouseZoomSensibility = 0.2f;
10088

10189
target = Vector3f(0.0f, 0.0f, 0.0f);
@@ -104,40 +92,22 @@ class FreeviewComponent: EntityComponent
10492
distanceToTagret = 20.0f;
10593
smoothDistanceToTagret = 20.0f;
10694

107-
zoomStiffness = 0.5f;
108-
translateStiffness = 1.0f;
95+
rotation = Vector3f(45.0f, 45.0f, 0.0f);
96+
smoothRotation = rotation;
10997

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;
113103
transform = Matrix4x4f.identity;
114104
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);
135105

136106
active = true;
137107

138-
enableTranslation = true;
139-
enableRotation = true;
140-
enableZoom = true;
108+
enableMouseTranslation = true;
109+
enableMouseRotation = true;
110+
enableMouseZoom = true;
141111

142112
transformEntity();
143113
}
@@ -148,47 +118,52 @@ class FreeviewComponent: EntityComponent
148118

149119
if (active)
150120
{
151-
if (eventManager.mouseButtonPressed[MB_RIGHT] && enableTranslation)
121+
if (eventManager.mouseButtonPressed[MB_RIGHT] && enableMouseTranslation)
152122
{
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;
155125
Vector3f trans = up * shifty + right * shiftx;
156126
target += trans;
157127
}
158-
else if (eventManager.mouseButtonPressed[MB_LEFT] && eventManager.keyPressed[KEY_LCTRL] && enableZoom)
128+
else if (eventManager.mouseButtonPressed[MB_LEFT] && eventManager.keyPressed[KEY_LCTRL] && enableMouseZoom)
159129
{
160130
float shiftx = (eventManager.mouseX - prevMouseX) * mouseZoomSensibility;
161131
float shifty = (eventManager.mouseY - prevMouseY) * mouseZoomSensibility;
162132
zoom(shiftx + shifty);
163133
}
164-
else if (eventManager.mouseButtonPressed[MB_LEFT] && enableRotation)
134+
else if (eventManager.mouseButtonPressed[MB_LEFT] && enableMouseRotation)
165135
{
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;
170141
}
171-
142+
172143
prevMouseX = eventManager.mouseX;
173144
prevMouseY = eventManager.mouseY;
174145
}
175146

176-
smoothTarget += (target - smoothTarget) * translateStiffness;
147+
smoothTarget += (target - smoothTarget) * translationStiffness;
177148
smoothDistanceToTagret += (distanceToTagret - smoothDistanceToTagret) * zoomStiffness;
149+
smoothRotation += (rotation - smoothRotation) * rotationStiffness;
178150

179151
transformEntity();
180152
}
181153

182154
void transformEntity()
183155
{
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+
192167
transform = invTransform.inverse;
193168

194169
entity.prevTransformation = entity.transformation;
@@ -202,71 +177,49 @@ class FreeviewComponent: EntityComponent
202177

203178
void setRotation(float p, float t, float r)
204179
{
205-
rotPitchTheta = p;
206-
rotTurnTheta = t;
207-
rotRollTheta = r;
180+
rotation = Vector3f(p, t, r);
181+
smoothRotation = rotation;
208182
}
209183

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()
231186
{
232-
return rotTurnTheta;
187+
setRotation(30.0f, 45.0f, 0.0f);
233188
}
234-
235-
float roll()
189+
190+
void setRotationSmooth(float p, float t, float r)
236191
{
237-
return rotRollTheta;
192+
rotation = Vector3f(p, t, r);
238193
}
239-
240-
void pitchSmooth(float theta, float smooth)
194+
195+
void rotate(float p, float t, float r)
241196
{
242-
pitchTargetTheta += theta;
243-
float pitchTheta = (pitchTargetTheta - pitchCurrentTheta) * smooth;
244-
pitchCurrentTheta += pitchTheta;
245-
pitch(pitchTheta);
197+
rotation += Vector3f(p, t, r);
246198
}
247-
248-
void turnSmooth(float theta, float smooth)
199+
200+
void setTarget(Vector3f pos)
249201
{
250-
turnTargetTheta += theta;
251-
float turnTheta = (turnTargetTheta - turnCurrentTheta) * smooth;
252-
turnCurrentTheta += turnTheta;
253-
turn(turnTheta);
202+
target = pos;
203+
smoothTarget = pos;
254204
}
255-
256-
void rollSmooth(float theta, float smooth)
205+
206+
void setTargetSmooth(Vector3f pos)
257207
{
258-
rollTargetTheta += theta;
259-
float rollTheta = (rollTargetTheta - rollCurrentTheta) * smooth;
260-
rollCurrentTheta += rollTheta;
261-
roll(rollTheta);
208+
target = pos;
262209
}
263210

264211
void translateTarget(Vector3f pos)
265212
{
266213
target += pos;
267214
}
268-
215+
269216
void setZoom(float z)
217+
{
218+
distanceToTagret = z;
219+
smoothDistanceToTagret = z;
220+
}
221+
222+
void setZoomSmooth(float z)
270223
{
271224
distanceToTagret = z;
272225
}
@@ -281,53 +234,19 @@ class FreeviewComponent: EntityComponent
281234
return transform.translation();
282235
}
283236

284-
Vector3f right()
285-
{
286-
return transform.right();
287-
}
288-
289-
Vector3f up()
290-
{
291-
return transform.up();
292-
}
293-
294237
Vector3f direction()
295238
{
296239
return transform.forward();
297240
}
298241

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()
317243
{
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();
323245
}
324246

325-
void moveSmooth(float speed, float smooth)
247+
Vector3f up()
326248
{
327-
targetStrafe += speed;
328-
float strafesp = (targetStrafe - currentStrafe) / smooth;
329-
currentStrafe += strafesp;
330-
move(strafesp);
249+
return transform.up();
331250
}
332251

333252
void screenToWorld(
@@ -378,7 +297,7 @@ class FreeviewComponent: EntityComponent
378297

379298
override void onMouseWheel(int x, int y)
380299
{
381-
if (!active || !enableZoom)
300+
if (!active || !enableMouseZoom)
382301
return;
383302

384303
zoom(cast(float)y * mouseZoomSensibility);

0 commit comments

Comments
 (0)