-
Notifications
You must be signed in to change notification settings - Fork 7
/
game.cpp
203 lines (156 loc) · 7.32 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright © 2015, The Network Protocol Company, Inc. All Rights Reserved.
#include "game.h"
#include "world.h"
void game_process_player_input( World & world, const Input & input, int player_id )
{
if ( !world.active )
return;
const double t = world.time * 60;
assert( world.entity_manager );
assert( world.physics_manager );
const int player_entity_index = ENTITY_PLAYER_BEGIN + player_id;
assert( world.entity_manager->GetType( player_entity_index ) == ENTITY_TYPE_CUBE );
CubeEntity * player_cube = (CubeEntity*) world.entity_manager->GetEntity( player_entity_index );
assert( player_cube );
const float strafe_force = 120.0f;
float force_x = 0.0f;
float force_y = 0.0f;
force_x -= strafe_force * (int) input.left;
force_x += strafe_force * (int) input.right;
force_y += strafe_force * (int) input.up;
force_y -= strafe_force * (int) input.down;
vec3f force( force_x, force_y, 0.0f );
if ( !input.push )
{
if ( dot( player_cube->linear_velocity, force ) < 100.0f )
force *= 2.0f;
}
world.physics_manager->ApplyForce( player_cube->physics_index, force );
if ( input.push )
{
// bobbing force on player cube
{
const float wobble_x = sin(t*0.1+1) + sin(t*0.05f+3) + sin(t+10);
const float wobble_y = sin(t*0.1+2) + sin(t*0.05f+4) + sin(t+11);
const float wobble_z = sin(t*0.1+3) + sin(t*0.05f+5) + sin(t+12);
vec3f force = vec3f( wobble_x, wobble_y, wobble_z ) * 2.0f;
world.physics_manager->ApplyForce( player_cube->physics_index, force );
}
// bobbing torque on player cube
{
const float wobble_x = sin(t*0.05+10) + sin(t*0.05f+22) + sin(t*1.1f);
const float wobble_y = sin(t*0.09+5) + sin(t*0.045f+16) + sin(t*1.11f);
const float wobble_z = sin(t*0.05+4) + sin(t*0.055f+9) + sin(t*1.12f);
vec3f torque = vec3f( wobble_x, wobble_y, wobble_z ) * 1.5f;
world.physics_manager->ApplyTorque( player_cube->physics_index, torque );
}
// calculate velocity tilt for player cube
vec3f target_up(0,0,1);
{
vec3f strafe( force.x(), force.y(), 0 );
if ( length_squared( strafe ) > 0 )
{
vec3f axis = normalize( cross( -force, vec3f(0,0,1) ) );
float tilt = 0.2 + 0.1f * length( player_cube->linear_velocity );
if ( tilt > 0.4f )
tilt = 0.4f;
target_up = transform( quat4f::axis_rotation( tilt, axis ), target_up );
}
}
// stay upright torque on player cube
{
vec3f current_up = transform( player_cube->orientation, vec3f(0,0,1) );
vec3f axis = cross( target_up, current_up );
float angle = acos( dot( target_up, current_up ) );
if ( angle > 0.5f )
angle = 0.5f;
vec3f torque = - 100 * axis * angle;
world.physics_manager->ApplyTorque( player_cube->physics_index, torque );
}
// apply linear/angular drag to player while in push mode
player_cube->angular_velocity *= 0.99f;
player_cube->linear_velocity *= vec3f( 0.99f, 0.99f, 0.99975f );
}
vec3f push_origin = vec3f( player_cube->position.x(), player_cube->position.y(), 0 );
push_origin += vec3f( 0, 0, -0.1f );
const int player_authority = ENTITY_PLAYER_BEGIN + player_id;
if ( input.push || input.pull )
{
for ( int i = 0; i < MaxEntities; ++i )
{
if ( ( world.entity_manager->GetFlag( i ) & ENTITY_FLAG_ALLOCATED ) == 0 )
continue;
if ( world.entity_manager->GetType( i ) != ENTITY_TYPE_CUBE )
continue;
auto cube = (CubeEntity*) world.entity_manager->GetEntity( i );
assert( cube );
const float mass = cube->scale * cube->scale * cube->scale;
if ( input.push )
{
vec3f difference = cube->position - push_origin;
if ( cube == player_cube )
{
const float d = 1.25f;
const float e = 0.1f;
if ( difference.z() > d + e )
difference -= vec3f( 0, 0, d );
else
difference = vec3f( difference.x(), difference.y(), e );
}
const float distance_squared = length_squared( difference );
if ( distance_squared > 0.0001f && distance_squared < 4.0f )
{
const float distance = sqrt( distance_squared );
vec3f direction = difference / distance;
float magnitude = 1.0f / distance_squared * 150.0f;
if ( magnitude > 500.0f )
magnitude = 500.0f;
vec3f force = direction * magnitude;
if ( cube != player_cube )
force *= mass;
else
force *= 1.0f;
if ( player_cube->linear_velocity.z() > 25 )
{
const float factor = 1 / ( 1 + player_cube->linear_velocity.z() - 25 );
force *= factor;
}
const int authority = world.entity_manager->GetAuthority( i );
if ( authority == 0 || authority == player_authority )
{
world.physics_manager->ApplyForce( cube->physics_index, force );
world.entity_manager->SetAuthority( i, player_authority );
}
}
}
else if ( input.pull )
{
if ( cube != player_cube )
{
vec3f origin = player_cube->position;
vec3f difference = cube->position - origin;
const float distance_squared = length_squared( difference );
const float effective_radius_squared = 1.8f * 1.8f;
if ( distance_squared > 0.2f*0.2f && distance_squared < effective_radius_squared )
{
const float distance = sqrt( distance_squared );
vec3f direction = difference / distance;
float magnitude = 1.0f / distance_squared * 200.0f;
if ( magnitude > 1000.0f )
magnitude = 1000.0f;
vec3f force = - direction * magnitude;
const int authority = world.entity_manager->GetAuthority( i );
if ( authority == player_authority || authority == 0 )
{
world.physics_manager->ApplyForce( cube->physics_index, force * mass );
world.entity_manager->SetAuthority( i, player_authority );
}
}
}
}
}
// apply base linear/angular drag to player
player_cube->angular_velocity *= 0.99999f;
player_cube->linear_velocity *= vec3f( 0.99999f, 0.99999f, 0.99999f );
}
}