Skip to content

Commit 51104d9

Browse files
kilbithparamat
authored andcommittedJul 2, 2017
Camera: Improve arm inertia
1 parent ab746b0 commit 51104d9

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed
 

‎src/camera.cpp

+51-32
Original file line numberDiff line numberDiff line change
@@ -195,39 +195,51 @@ void Camera::step(f32 dtime)
195195
}
196196
}
197197

198-
void Camera::addArmInertia(f32 player_yaw, f32 frametime)
198+
static inline v2f dir(const v2f &pos_dist)
199199
{
200-
if (m_timer.X == 0.0f) {
201-
// In the limit case where timer is 0 set a static velocity (generic case divide by zero)
202-
m_cam_vel.X = 1.0001f;
200+
f32 x = pos_dist.X - 55.0f;
201+
f32 y = pos_dist.Y + 35.0f;
202+
203+
f32 x_abs = std::fabs(x);
204+
f32 y_abs = std::fabs(y);
205+
206+
if (x_abs >= y_abs) {
207+
y *= (1.0f / x_abs);
208+
x /= x_abs;
203209
}
204-
else
205-
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f;
206210

207-
if (m_timer.Y == 0.0f) {
208-
// In the limit case where timer is 0 set a static velocity (generic case divide by zero)
209-
m_cam_vel.Y = 1.0001f;
211+
if (y_abs >= x_abs) {
212+
x *= (1.0f / y_abs);
213+
y /= y_abs;
210214
}
211-
else
212-
m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y);
215+
216+
return v2f(std::fabs(x), std::fabs(y));
217+
}
218+
219+
void Camera::addArmInertia(f32 player_yaw, f32 frametime)
220+
{
221+
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / frametime) * 0.01f;
222+
m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / frametime);
213223

214224
if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
215225
/*
216-
the arm moves when the camera moves fast enough.
226+
The arm moves relative to the camera speed,
227+
with an acceleration factor.
217228
*/
218229

219230
if (m_cam_vel.X > 1.0f) {
220-
m_timer.X = frametime;
221-
222231
if (m_cam_vel.X > m_cam_vel_old.X)
223232
m_cam_vel_old.X = m_cam_vel.X;
224233

225-
if (m_last_cam_pos.X > player_yaw)
234+
if (m_last_cam_pos.X > player_yaw) {
226235
// right
236+
m_cam_vel.X -= std::fabs(55.0f - m_wieldmesh_offset.X) * 0.1f;
227237
m_wieldmesh_offset.X -= (0.1f + frametime) * m_cam_vel.X;
228-
else
238+
} else {
229239
// left
240+
m_cam_vel.X += std::fabs(55.0f - m_wieldmesh_offset.X) * 0.1f;
230241
m_wieldmesh_offset.X += (0.1f + frametime) * m_cam_vel.X;
242+
}
231243

232244
if (m_last_cam_pos.X != player_yaw)
233245
m_last_cam_pos.X = player_yaw;
@@ -236,47 +248,54 @@ void Camera::addArmInertia(f32 player_yaw, f32 frametime)
236248
}
237249

238250
if (m_cam_vel.Y > 1.0f) {
239-
m_timer.Y = frametime;
240-
241251
if (m_cam_vel.Y > m_cam_vel_old.Y)
242252
m_cam_vel_old.Y = m_cam_vel.Y;
243253

244-
if (m_last_cam_pos.Y > m_camera_direction.Y)
254+
if (m_last_cam_pos.Y > m_camera_direction.Y) {
245255
// down
256+
m_cam_vel.Y -= std::fabs(-35.0f - m_wieldmesh_offset.Y) * 0.1f;
246257
m_wieldmesh_offset.Y += (0.1f + frametime) * m_cam_vel.Y;
247-
else
258+
} else {
248259
// up
260+
m_cam_vel.Y += std::fabs(-35.0f - m_wieldmesh_offset.Y) * 0.1f;
249261
m_wieldmesh_offset.Y -= (0.1f + frametime) * m_cam_vel.Y;
262+
}
250263

251264
if (m_last_cam_pos.Y != m_camera_direction.Y)
252265
m_last_cam_pos.Y = m_camera_direction.Y;
253266

254267
m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y, -45.0f, -30.0f);
255268
}
269+
270+
m_arm_dir = dir(m_wieldmesh_offset);
256271
} else {
257272
/*
258-
the arm now gets back to its default position when the camera stops.
273+
Now the arm gets back to its default position when the camera stops,
274+
following a vector, with a smooth deceleration factor.
259275
*/
260276

261-
if (floor(m_wieldmesh_offset.X) == 55.0f) {
277+
f32 acc_X = (m_cam_vel_old.X * (1.0f + (1.0f - m_arm_dir.X))) /
278+
(20.0f / std::fabs(55.0f - m_wieldmesh_offset.X));
279+
f32 acc_Y = (m_cam_vel_old.Y * (1.0f + (1.0f - m_arm_dir.Y))) /
280+
(15.0f / std::fabs(-35.0f - m_wieldmesh_offset.Y));
281+
282+
if (std::fabs(55.0f - m_wieldmesh_offset.X) < 0.1f)
262283
m_cam_vel_old.X = 0.0f;
263-
m_wieldmesh_offset.X = 55.0f;
264-
}
265284

266285
if (m_wieldmesh_offset.X > 55.0f)
267-
m_wieldmesh_offset.X -= (0.05f + frametime) * m_cam_vel_old.X;
286+
m_wieldmesh_offset.X -= (0.2f + frametime) * acc_X;
287+
268288
if (m_wieldmesh_offset.X < 55.0f)
269-
m_wieldmesh_offset.X += (0.05f + frametime) * m_cam_vel_old.X;
289+
m_wieldmesh_offset.X += (0.2f + frametime) * acc_X;
270290

271-
if (floor(m_wieldmesh_offset.Y) == -35.0f) {
291+
if (std::fabs(-35.0f - m_wieldmesh_offset.Y) < 0.1f)
272292
m_cam_vel_old.Y = 0.0f;
273-
m_wieldmesh_offset.Y = -35.0f;
274-
}
275293

276294
if (m_wieldmesh_offset.Y > -35.0f)
277-
m_wieldmesh_offset.Y -= (0.05f + frametime) * m_cam_vel_old.Y;
295+
m_wieldmesh_offset.Y -= (0.1f + frametime) * acc_Y;
296+
278297
if (m_wieldmesh_offset.Y < -35.0f)
279-
m_wieldmesh_offset.Y += (0.05f + frametime) * m_cam_vel_old.Y;
298+
m_wieldmesh_offset.Y += (0.1f + frametime) * acc_Y;
280299
}
281300
}
282301

@@ -468,7 +487,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
468487
m_cameranode->setAspectRatio(m_aspect);
469488
m_cameranode->setFOV(m_fov_y);
470489

471-
if (m_arm_inertia)
490+
if (m_arm_inertia && frametime > 0.0f)
472491
addArmInertia(player->getYaw(), frametime);
473492

474493
// Position the wielded item

‎src/camera.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Camera
191191
v3s16 m_camera_offset;
192192

193193
v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
194-
v2f m_timer;
194+
v2f m_arm_dir;
195195
v2f m_cam_vel;
196196
v2f m_cam_vel_old;
197197
v2f m_last_cam_pos;

0 commit comments

Comments
 (0)
Please sign in to comment.