3 Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "localplayer.h"
22 #include "main.h" // For g_settings
24 #include "collision.h"
29 #include "util/numeric.h"
35 LocalPlayer::LocalPlayer(IGameDef *gamedef):
37 m_sneak_node(32767,32767,32767),
38 m_sneak_node_exists(false)
40 // Initialize hp to 0, so that no hearts will be shown if server
41 // doesn't support health points
45 LocalPlayer::~LocalPlayer()
49 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
50 core::list<CollisionInfo> *collision_info)
52 INodeDefManager *nodemgr = m_gamedef->ndef();
54 v3f position = getPosition();
55 v3f oldpos = position;
56 v3s16 oldpos_i = floatToInt(oldpos, BS);
58 v3f old_speed = m_speed;
60 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
61 <<oldpos_i.Z<<")"<<std::endl;*/
64 Calculate new position
66 position += m_speed * dtime;
68 // Skip collision detection if a special movement mode is used
69 bool fly_allowed = m_gamedef->checkLocalPrivilege("fly");
70 bool free_move = fly_allowed && g_settings->getBool("free_move");
73 setPosition(position);
81 // Player position in nodes
82 v3s16 pos_i = floatToInt(position, BS);
85 Check if player is in water (the oscillating value)
88 // If in water, the threshold of coming out is at higher y
91 v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
92 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
94 // If not in water, the threshold of going in is at lower y
97 v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
98 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
101 catch(InvalidPositionException &e)
107 Check if player is in water (the stable value)
110 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
111 in_water_stable = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
113 catch(InvalidPositionException &e)
115 in_water_stable = false;
119 Check if player is climbing
123 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
124 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
125 is_climbing = ((nodemgr->get(map.getNode(pp).getContent()).climbable ||
126 nodemgr->get(map.getNode(pp2).getContent()).climbable) && !free_move);
128 catch(InvalidPositionException &e)
134 Collision uncertainty radius
135 Make it a bit larger than the maximum distance of movement
137 //f32 d = pos_max_d * 1.1;
138 // A fairly large value in here makes moving smoother
141 // This should always apply, otherwise there are glitches
142 assert(d > pos_max_d);
144 float player_radius = BS*0.30;
145 float player_height = BS*1.55;
147 // Maximum distance over border for sneaking
148 f32 sneak_max = BS*0.4;
151 If sneaking, player has larger collision radius to keep from
155 player_radius = sneak_max + d*1.1;*/
158 If sneaking, keep in range from the last walked node and don't
161 if(control.sneak && m_sneak_node_exists)
163 f32 maxd = 0.5*BS + sneak_max;
164 v3f lwn_f = intToFloat(m_sneak_node, BS);
165 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
166 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
168 f32 min_y = lwn_f.Y + 0.5*BS;
169 if(position.Y < min_y)
173 //v3f old_speed = m_speed;
180 // Report fall collision
181 if(old_speed.Y < m_speed.Y - 0.1)
184 info.t = COLLISION_FALL;
185 info.speed = m_speed.Y - old_speed.Y;
186 collision_info->push_back(info);
193 Calculate player collision box (new and old)
195 core::aabbox3d<f32> playerbox(
196 position.X - player_radius,
198 position.Z - player_radius,
199 position.X + player_radius,
200 position.Y + player_height,
201 position.Z + player_radius
203 core::aabbox3d<f32> playerbox_old(
204 oldpos.X - player_radius,
206 oldpos.Z - player_radius,
207 oldpos.X + player_radius,
208 oldpos.Y + player_height,
209 oldpos.Z + player_radius
213 If the player's feet touch the topside of any node, this is
216 Player is allowed to jump when this is true.
218 bool touching_ground_was = touching_ground;
219 touching_ground = false;
221 /*std::cout<<"Checking collisions for ("
222 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
224 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
227 bool standing_on_unloaded = false;
230 Go through every node around the player
232 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
233 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
234 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
236 bool is_unloaded = false;
238 // Player collides into walkable nodes
239 if(nodemgr->get(map.getNode(v3s16(x,y,z))).walkable == false)
242 catch(InvalidPositionException &e)
245 // Doing nothing here will block the player from
246 // walking over map borders
249 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
252 See if the player is touching ground.
254 Player touches ground if player's minimum Y is near node's
255 maximum Y and player's X-Z-area overlaps with the node's
258 Use 0.15*BS so that it is easier to get on a node.
261 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
262 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
263 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
264 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
265 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
266 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
268 touching_ground = true;
270 standing_on_unloaded = true;
273 // If player doesn't intersect with node, ignore node.
274 if(playerbox.intersectsWithBox(nodebox) == false)
278 Go through every axis
281 v3f(0,0,1), // back-front
282 v3f(0,1,0), // top-bottom
283 v3f(1,0,0), // right-left
285 for(u16 i=0; i<3; i++)
288 Calculate values along the axis
290 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
291 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
292 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
293 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
294 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
295 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
298 Check collision for the axis.
299 Collision happens when player is going through a surface.
303 // Make it easier to get on top of a node
306 bool negative_axis_collides =
307 (nodemax > playermin && nodemax <= playermin_old + neg_d
308 && m_speed.dotProduct(dirs[i]) < 0);
309 bool positive_axis_collides =
310 (nodemin < playermax && nodemin >= playermax_old - pos_d
311 && m_speed.dotProduct(dirs[i]) > 0);*/
312 bool negative_axis_collides =
313 (nodemax > playermin && nodemax <= playermin_old + d
314 && m_speed.dotProduct(dirs[i]) < 0);
315 bool positive_axis_collides =
316 (nodemin < playermax && nodemin >= playermax_old - d
317 && m_speed.dotProduct(dirs[i]) > 0);
318 bool main_axis_collides =
319 negative_axis_collides || positive_axis_collides;
322 Check overlap of player and node in other axes
324 bool other_axes_overlap = true;
325 for(u16 j=0; j<3; j++)
329 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
330 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
331 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
332 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
333 if(!(nodemax - d > playermin && nodemin + d < playermax))
335 other_axes_overlap = false;
341 If this is a collision, revert the position in the main
344 if(other_axes_overlap && main_axis_collides)
346 //v3f old_speed = m_speed;
348 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
349 position -= position.dotProduct(dirs[i]) * dirs[i];
350 position += oldpos.dotProduct(dirs[i]) * dirs[i];
354 // Report fall collision
355 if(old_speed.Y < m_speed.Y - 0.1)
358 info.t = COLLISION_FALL;
359 info.speed = m_speed.Y - old_speed.Y;
360 collision_info->push_back(info);
369 Check the nodes under the player to see from which node the
370 player is sneaking from, if any.
373 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
374 v2f player_p2df(position.X, position.Z);
375 f32 min_distance_f = 100000.0*BS;
376 // If already seeking from some node, compare to it.
377 /*if(m_sneak_node_exists)
379 v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
380 v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
381 f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
382 f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
383 // Ignore if player is not on the same level (likely dropped)
384 if(d_vert_f < 0.15*BS)
385 min_distance_f = d_horiz_f;
387 v3s16 new_sneak_node = m_sneak_node;
388 for(s16 x=-1; x<=1; x++)
389 for(s16 z=-1; z<=1; z++)
391 v3s16 p = pos_i_bottom + v3s16(x,0,z);
392 v3f pf = intToFloat(p, BS);
393 v2f node_p2df(pf.X, pf.Z);
394 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
395 f32 max_axis_distance_f = MYMAX(
396 fabs(player_p2df.X-node_p2df.X),
397 fabs(player_p2df.Y-node_p2df.Y));
399 if(distance_f > min_distance_f ||
400 max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
404 // The node to be sneaked on has to be walkable
405 if(nodemgr->get(map.getNode(p)).walkable == false)
407 // And the node above it has to be nonwalkable
408 if(nodemgr->get(map.getNode(p+v3s16(0,1,0))).walkable == true)
411 catch(InvalidPositionException &e)
416 min_distance_f = distance_f;
420 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
422 if(control.sneak && m_sneak_node_exists)
425 m_sneak_node = new_sneak_node;
429 m_sneak_node = new_sneak_node;
430 m_sneak_node_exists = sneak_node_found;
434 If sneaking, the player's collision box can be in air, so
435 this has to be set explicitly
437 if(sneak_node_found && control.sneak)
438 touching_ground = true;
444 setPosition(position);
451 // Report fall collision
452 if(old_speed.Y < m_speed.Y - 0.1 && !standing_on_unloaded)
455 info.t = COLLISION_FALL;
456 info.speed = m_speed.Y - old_speed.Y;
457 collision_info->push_back(info);
461 if(!touching_ground_was && touching_ground){
462 MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
463 m_gamedef->event()->put(e);
467 camera_barely_in_ceiling = false;
468 v3s16 camera_np = floatToInt(getEyePosition(), BS);
469 MapNode n = map.getNodeNoEx(camera_np);
470 if(n.getContent() != CONTENT_IGNORE){
471 if(nodemgr->get(n).walkable){
472 camera_barely_in_ceiling = true;
478 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
480 move(dtime, map, pos_max_d, NULL);
483 void LocalPlayer::applyControl(float dtime)
489 f32 walk_acceleration = 4.0 * BS;
490 f32 walkspeed_max = 4.0 * BS;
492 setPitch(control.pitch);
495 v3f move_direction = v3f(0,0,1);
496 move_direction.rotateXZBy(getYaw());
498 v3f speed = v3f(0,0,0);
500 bool fly_allowed = m_gamedef->checkLocalPrivilege("fly");
501 bool fast_allowed = m_gamedef->checkLocalPrivilege("fast");
503 bool free_move = fly_allowed && g_settings->getBool("free_move");
504 bool fast_move = fast_allowed && g_settings->getBool("fast_move");
505 bool continuous_forward = g_settings->getBool("continuous_forward");
507 if(free_move || is_climbing)
509 v3f speed = getSpeed();
514 // Whether superspeed mode is used or not
515 bool superspeed = false;
517 // If free movement and fast movement, always move fast
518 if(free_move && fast_move)
521 // Auxiliary button 1 (E)
526 // In free movement mode, aux1 descends
527 v3f speed = getSpeed();
531 speed.Y = -walkspeed_max;
536 v3f speed = getSpeed();
542 // If not free movement but fast is allowed, aux1 is
549 if(continuous_forward)
550 speed += move_direction;
554 if(continuous_forward)
557 speed += move_direction;
561 speed -= move_direction;
565 speed += move_direction.crossProduct(v3f(0,1,0));
569 speed += move_direction.crossProduct(v3f(0,-1,0));
575 v3f speed = getSpeed();
579 speed.Y = walkspeed_max;
582 else if(touching_ground)
585 NOTE: The d value in move() affects jump height by
586 raising the height at which the jump speed is kept
587 at its starting value
589 v3f speed = getSpeed();
590 if(speed.Y >= -0.5*BS)
595 MtEvent *e = new SimpleTriggerEvent("PlayerJump");
596 m_gamedef->event()->put(e);
599 // Use the oscillating value for getting out of water
600 // (so that the player doesn't fly on the surface)
603 v3f speed = getSpeed();
610 v3f speed = getSpeed();
616 // The speed of the player (Y is ignored)
618 speed = speed.normalize() * walkspeed_max * 5.0;
619 else if(control.sneak)
620 speed = speed.normalize() * walkspeed_max / 3.0;
622 speed = speed.normalize() * walkspeed_max;
624 f32 inc = walk_acceleration * BS * dtime;
626 // Faster acceleration if fast and free movement
627 if(free_move && fast_move)
628 inc = walk_acceleration * BS * dtime * 10;
630 // Accelerate to target speed with maximum increment
631 accelerate(speed, inc);
634 v3s16 LocalPlayer::getStandingNodePos()
636 if(m_sneak_node_exists)
638 return floatToInt(getPosition(), BS);