Rename game_comp_grav to game_tilt_grav
[neverball] / ball / game_common.c
1 #include "game_common.h"
2 #include "vec3.h"
3
4 const char *status_to_str(int s)
5 {
6     switch (s)
7     {
8     case GAME_NONE:    return _("Aborted");
9     case GAME_TIME:    return _("Time-out");
10     case GAME_GOAL:    return _("Success");
11     case GAME_FALL:    return _("Fall-out");
12     default:           return _("Unknown");
13     }
14 }
15
16 const char *view_to_str(int v)
17 {
18     switch (v)
19     {
20     case VIEW_CHASE:   return _("Chase");
21     case VIEW_LAZY:    return _("Lazy");
22     case VIEW_MANUAL:  return _("Manual");
23     case VIEW_TOPDOWN: return _("Top-Down");
24     default:           return _("Unknown");
25     }
26 }
27
28 /*---------------------------------------------------------------------------*/
29
30 void game_tilt_init(struct game_tilt *tilt)
31 {
32     tilt->x[0] = 1.0f;
33     tilt->x[1] = 0.0f;
34     tilt->x[2] = 0.0f;
35
36     tilt->rx = 0.0f;
37
38     tilt->z[0] = 0.0f;
39     tilt->z[1] = 0.0f;
40     tilt->z[2] = 1.0f;
41
42     tilt->rz = 0.0f;
43 }
44
45 /*
46  * Compute appropriate tilt axes from the view basis.
47  */
48 void game_tilt_axes(struct game_tilt *tilt, float view_e[3][3])
49 {
50     const float Y[3] = { 0.0f, 1.0f, 0.0f };
51
52     v_cpy(tilt->x, view_e[0]);
53     v_cpy(tilt->z, view_e[2]);
54
55     /* Handle possible top-down view. */
56
57     if (fabsf(v_dot(view_e[1], Y)) < fabsf(v_dot(view_e[2], Y)))
58         v_inv(tilt->z, view_e[1]);
59 }
60
61 void game_tilt_grav(float h[3], const float g[3], const struct game_tilt *tilt)
62 {
63     float X[16];
64     float Z[16];
65     float M[16];
66
67     /* Compute the gravity vector from the given world rotations. */
68
69     m_rot (Z, tilt->z, V_RAD(tilt->rz));
70     m_rot (X, tilt->x, V_RAD(tilt->rx));
71     m_mult(M, Z, X);
72     m_vxfm(h, M, g);
73 }
74
75 /*---------------------------------------------------------------------------*/