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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
21 #include "main.h" // For g_settings
23 #include "collision.h"
33 LocalPlayer::LocalPlayer(IGameDef *gamedef):
35 m_sneak_node(32767,32767,32767),
36 m_sneak_node_exists(false)
38 // Initialize hp to 0, so that no hearts will be shown if server
39 // doesn't support health points
43 LocalPlayer::~LocalPlayer()
47 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
48 core::list<CollisionInfo> *collision_info)
50 INodeDefManager *nodemgr = m_gamedef->ndef();
52 v3f position = getPosition();
53 v3f oldpos = position;
54 v3s16 oldpos_i = floatToInt(oldpos, BS);
56 v3f old_speed = m_speed;
58 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
59 <<oldpos_i.Z<<")"<<std::endl;*/
62 Calculate new position
64 position += m_speed * dtime;
66 // Skip collision detection if a special movement mode is used
67 bool fly_allowed = m_gamedef->checkLocalPrivilege("fly");
68 bool free_move = fly_allowed && g_settings->getBool("free_move");
71 setPosition(position);
79 // Player position in nodes
80 v3s16 pos_i = floatToInt(position, BS);
83 Check if player is in water (the oscillating value)
86 // If in water, the threshold of coming out is at higher y
89 v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
90 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
92 // If not in water, the threshold of going in is at lower y
95 v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
96 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
99 catch(InvalidPositionException &e)
105 Check if player is in water (the stable value)
108 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
109 in_water_stable = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
111 catch(InvalidPositionException &e)
113 in_water_stable = false;
117 Check if player is climbing
121 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
122 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
123 is_climbing = ((nodemgr->get(map.getNode(pp).getContent()).climbable ||
124 nodemgr->get(map.getNode(pp2).getContent()).climbable) && !free_move);
126 catch(InvalidPositionException &e)
132 Collision uncertainty radius
133 Make it a bit larger than the maximum distance of movement
135 //f32 d = pos_max_d * 1.1;
136 // A fairly large value in here makes moving smoother
139 // This should always apply, otherwise there are glitches
140 assert(d > pos_max_d);
142 float player_radius = BS*0.30;
143 float player_height = BS*1.55;
145 // Maximum distance over border for sneaking
146 f32 sneak_max = BS*0.4;
149 If sneaking, player has larger collision radius to keep from
153 player_radius = sneak_max + d*1.1;*/
156 If sneaking, keep in range from the last walked node and don't
159 if(control.sneak && m_sneak_node_exists)
161 f32 maxd = 0.5*BS + sneak_max;
162 v3f lwn_f = intToFloat(m_sneak_node, BS);
163 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
164 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
166 f32 min_y = lwn_f.Y + 0.5*BS;
167 if(position.Y < min_y)
171 //v3f old_speed = m_speed;
178 // Report fall collision
179 if(old_speed.Y < m_speed.Y - 0.1)
182 info.t = COLLISION_FALL;
183 info.speed = m_speed.Y - old_speed.Y;
184 collision_info->push_back(info);
191 Calculate player collision box (new and old)
193 core::aabbox3d<f32> playerbox(
194 position.X - player_radius,
196 position.Z - player_radius,
197 position.X + player_radius,
198 position.Y + player_height,
199 position.Z + player_radius
201 core::aabbox3d<f32> playerbox_old(
202 oldpos.X - player_radius,
204 oldpos.Z - player_radius,
205 oldpos.X + player_radius,
206 oldpos.Y + player_height,
207 oldpos.Z + player_radius
211 If the player's feet touch the topside of any node, this is
214 Player is allowed to jump when this is true.
216 bool touching_ground_was = touching_ground;
217 touching_ground = false;
219 /*std::cout<<"Checking collisions for ("
220 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
222 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
225 bool standing_on_unloaded = false;
228 Go through every node around the player
230 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
231 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
232 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
234 bool is_unloaded = false;
236 // Player collides into walkable nodes
237 if(nodemgr->get(map.getNode(v3s16(x,y,z))).walkable == false)
240 catch(InvalidPositionException &e)
243 // Doing nothing here will block the player from
244 // walking over map borders
247 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
250 See if the player is touching ground.
252 Player touches ground if player's minimum Y is near node's
253 maximum Y and player's X-Z-area overlaps with the node's
256 Use 0.15*BS so that it is easier to get on a node.
259 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
260 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
261 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
262 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
263 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
264 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
266 touching_ground = true;
268 standing_on_unloaded = true;
271 // If player doesn't intersect with node, ignore node.
272 if(playerbox.intersectsWithBox(nodebox) == false)
276 Go through every axis
279 v3f(0,0,1), // back-front
280 v3f(0,1,0), // top-bottom
281 v3f(1,0,0), // right-left
283 for(u16 i=0; i<3; i++)
286 Calculate values along the axis
288 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
289 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
290 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
291 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
292 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
293 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
296 Check collision for the axis.
297 Collision happens when player is going through a surface.
301 // Make it easier to get on top of a node
304 bool negative_axis_collides =
305 (nodemax > playermin && nodemax <= playermin_old + neg_d
306 && m_speed.dotProduct(dirs[i]) < 0);
307 bool positive_axis_collides =
308 (nodemin < playermax && nodemin >= playermax_old - pos_d
309 && m_speed.dotProduct(dirs[i]) > 0);*/
310 bool negative_axis_collides =
311 (nodemax > playermin && nodemax <= playermin_old + d
312 && m_speed.dotProduct(dirs[i]) < 0);
313 bool positive_axis_collides =
314 (nodemin < playermax && nodemin >= playermax_old - d
315 && m_speed.dotProduct(dirs[i]) > 0);
316 bool main_axis_collides =
317 negative_axis_collides || positive_axis_collides;
320 Check overlap of player and node in other axes
322 bool other_axes_overlap = true;
323 for(u16 j=0; j<3; j++)
327 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
328 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
329 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
330 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
331 if(!(nodemax - d > playermin && nodemin + d < playermax))
333 other_axes_overlap = false;
339 If this is a collision, revert the position in the main
342 if(other_axes_overlap && main_axis_collides)
344 //v3f old_speed = m_speed;
346 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
347 position -= position.dotProduct(dirs[i]) * dirs[i];
348 position += oldpos.dotProduct(dirs[i]) * dirs[i];
352 // Report fall collision
353 if(old_speed.Y < m_speed.Y - 0.1)
356 info.t = COLLISION_FALL;
357 info.speed = m_speed.Y - old_speed.Y;
358 collision_info->push_back(info);
367 Check the nodes under the player to see from which node the
368 player is sneaking from, if any.
371 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
372 v2f player_p2df(position.X, position.Z);
373 f32 min_distance_f = 100000.0*BS;
374 // If already seeking from some node, compare to it.
375 /*if(m_sneak_node_exists)
377 v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
378 v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
379 f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
380 f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
381 // Ignore if player is not on the same level (likely dropped)
382 if(d_vert_f < 0.15*BS)
383 min_distance_f = d_horiz_f;
385 v3s16 new_sneak_node = m_sneak_node;
386 for(s16 x=-1; x<=1; x++)
387 for(s16 z=-1; z<=1; z++)
389 v3s16 p = pos_i_bottom + v3s16(x,0,z);
390 v3f pf = intToFloat(p, BS);
391 v2f node_p2df(pf.X, pf.Z);
392 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
393 f32 max_axis_distance_f = MYMAX(
394 fabs(player_p2df.X-node_p2df.X),
395 fabs(player_p2df.Y-node_p2df.Y));
397 if(distance_f > min_distance_f ||
398 max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
402 // The node to be sneaked on has to be walkable
403 if(nodemgr->get(map.getNode(p)).walkable == false)
405 // And the node above it has to be nonwalkable
406 if(nodemgr->get(map.getNode(p+v3s16(0,1,0))).walkable == true)
409 catch(InvalidPositionException &e)
414 min_distance_f = distance_f;
418 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
420 if(control.sneak && m_sneak_node_exists)
423 m_sneak_node = new_sneak_node;
427 m_sneak_node = new_sneak_node;
428 m_sneak_node_exists = sneak_node_found;
432 If sneaking, the player's collision box can be in air, so
433 this has to be set explicitly
435 if(sneak_node_found && control.sneak)
436 touching_ground = true;
442 setPosition(position);
449 // Report fall collision
450 if(old_speed.Y < m_speed.Y - 0.1 && !standing_on_unloaded)
453 info.t = COLLISION_FALL;
454 info.speed = m_speed.Y - old_speed.Y;
455 collision_info->push_back(info);
459 if(!touching_ground_was && touching_ground){
460 MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
461 m_gamedef->event()->put(e);
465 camera_barely_in_ceiling = false;
466 v3s16 camera_np = floatToInt(getEyePosition(), BS);
467 MapNode n = map.getNodeNoEx(camera_np);
468 if(n.getContent() != CONTENT_IGNORE){
469 if(nodemgr->get(n).walkable){
470 camera_barely_in_ceiling = true;
476 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
478 move(dtime, map, pos_max_d, NULL);
481 void LocalPlayer::applyControl(float dtime)
487 f32 walk_acceleration = 4.0 * BS;
488 f32 walkspeed_max = 4.0 * BS;
490 setPitch(control.pitch);
493 v3f move_direction = v3f(0,0,1);
494 move_direction.rotateXZBy(getYaw());
496 v3f speed = v3f(0,0,0);
498 bool fly_allowed = m_gamedef->checkLocalPrivilege("fly");
499 bool fast_allowed = m_gamedef->checkLocalPrivilege("fast");
501 bool free_move = fly_allowed && g_settings->getBool("free_move");
502 bool fast_move = fast_allowed && g_settings->getBool("fast_move");
503 bool continuous_forward = g_settings->getBool("continuous_forward");
505 if(free_move || is_climbing)
507 v3f speed = getSpeed();
512 // Whether superspeed mode is used or not
513 bool superspeed = false;
515 // If free movement and fast movement, always move fast
516 if(free_move && fast_move)
519 // Auxiliary button 1 (E)
524 // In free movement mode, aux1 descends
525 v3f speed = getSpeed();
529 speed.Y = -walkspeed_max;
534 v3f speed = getSpeed();
540 // If not free movement but fast is allowed, aux1 is
547 if(continuous_forward)
548 speed += move_direction;
552 if(continuous_forward)
555 speed += move_direction;
559 speed -= move_direction;
563 speed += move_direction.crossProduct(v3f(0,1,0));
567 speed += move_direction.crossProduct(v3f(0,-1,0));
573 v3f speed = getSpeed();
577 speed.Y = walkspeed_max;
580 else if(touching_ground)
583 NOTE: The d value in move() affects jump height by
584 raising the height at which the jump speed is kept
585 at its starting value
587 v3f speed = getSpeed();
588 if(speed.Y >= -0.5*BS)
593 MtEvent *e = new SimpleTriggerEvent("PlayerJump");
594 m_gamedef->event()->put(e);
597 // Use the oscillating value for getting out of water
598 // (so that the player doesn't fly on the surface)
601 v3f speed = getSpeed();
608 v3f speed = getSpeed();
614 // The speed of the player (Y is ignored)
616 speed = speed.normalize() * walkspeed_max * 5.0;
617 else if(control.sneak)
618 speed = speed.normalize() * walkspeed_max / 3.0;
620 speed = speed.normalize() * walkspeed_max;
622 f32 inc = walk_acceleration * BS * dtime;
624 // Faster acceleration if fast and free movement
625 if(free_move && fast_move)
626 inc = walk_acceleration * BS * dtime * 10;
628 // Accelerate to target speed with maximum increment
629 accelerate(speed, inc);
632 v3s16 LocalPlayer::getStandingNodePos()
634 if(m_sneak_node_exists)
636 return floatToInt(getPosition(), BS);