If a user doesn't have build rights, don't allow them to move items to and from their...
[oweals/minetest.git] / src / collision.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "collision.h"
21 #include "mapblock.h"
22 #include "map.h"
23
24 collisionMoveResult collisionMoveSimple(Map *map, f32 pos_max_d,
25                 const core::aabbox3d<f32> &box_0,
26                 f32 dtime, v3f &pos_f, v3f &speed_f)
27 {
28         collisionMoveResult result;
29
30         v3f oldpos_f = pos_f;
31         v3s16 oldpos_i = floatToInt(oldpos_f, BS);
32
33         /*
34                 Calculate new position
35         */
36         pos_f += speed_f * dtime;
37
38         /*
39                 Collision detection
40         */
41         
42         // position in nodes
43         v3s16 pos_i = floatToInt(pos_f, BS);
44         
45         /*
46                 Collision uncertainty radius
47                 Make it a bit larger than the maximum distance of movement
48         */
49         f32 d = pos_max_d * 1.1;
50         // A fairly large value in here makes moving smoother
51         //f32 d = 0.15*BS;
52
53         // This should always apply, otherwise there are glitches
54         assert(d > pos_max_d);
55         
56         /*
57                 Calculate collision box
58         */
59         core::aabbox3d<f32> box = box_0;
60         box.MaxEdge += pos_f;
61         box.MinEdge += pos_f;
62         core::aabbox3d<f32> oldbox = box_0;
63         oldbox.MaxEdge += oldpos_f;
64         oldbox.MinEdge += oldpos_f;
65
66         /*
67                 If the object lies on a walkable node, this is set to true.
68         */
69         result.touching_ground = false;
70         
71         /*
72                 Go through every node around the object
73                 TODO: Calculate the range of nodes that need to be checked
74         */
75         for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
76         for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
77         for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
78         {
79                 try{
80                         // Object collides into walkable nodes
81                         MapNode n = map->getNode(v3s16(x,y,z));
82                         if(content_features(n).walkable == false)
83                                 continue;
84                 }
85                 catch(InvalidPositionException &e)
86                 {
87                         // Doing nothing here will block the object from
88                         // walking over map borders
89                 }
90
91                 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
92                 
93                 /*
94                         See if the object is touching ground.
95
96                         Object touches ground if object's minimum Y is near node's
97                         maximum Y and object's X-Z-area overlaps with the node's
98                         X-Z-area.
99
100                         Use 0.15*BS so that it is easier to get on a node.
101                 */
102                 if(
103                                 //fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < d
104                                 fabs(nodebox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS
105                                 && nodebox.MaxEdge.X-d > box.MinEdge.X
106                                 && nodebox.MinEdge.X+d < box.MaxEdge.X
107                                 && nodebox.MaxEdge.Z-d > box.MinEdge.Z
108                                 && nodebox.MinEdge.Z+d < box.MaxEdge.Z
109                 ){
110                         result.touching_ground = true;
111                 }
112                 
113                 // If object doesn't intersect with node, ignore node.
114                 if(box.intersectsWithBox(nodebox) == false)
115                         continue;
116                 
117                 /*
118                         Go through every axis
119                 */
120                 v3f dirs[3] = {
121                         v3f(0,0,1), // back-front
122                         v3f(0,1,0), // top-bottom
123                         v3f(1,0,0), // right-left
124                 };
125                 for(u16 i=0; i<3; i++)
126                 {
127                         /*
128                                 Calculate values along the axis
129                         */
130                         f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
131                         f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
132                         f32 objectmax = box.MaxEdge.dotProduct(dirs[i]);
133                         f32 objectmin = box.MinEdge.dotProduct(dirs[i]);
134                         f32 objectmax_old = oldbox.MaxEdge.dotProduct(dirs[i]);
135                         f32 objectmin_old = oldbox.MinEdge.dotProduct(dirs[i]);
136                         
137                         /*
138                                 Check collision for the axis.
139                                 Collision happens when object is going through a surface.
140                         */
141                         bool negative_axis_collides =
142                                 (nodemax > objectmin && nodemax <= objectmin_old + d
143                                         && speed_f.dotProduct(dirs[i]) < 0);
144                         bool positive_axis_collides =
145                                 (nodemin < objectmax && nodemin >= objectmax_old - d
146                                         && speed_f.dotProduct(dirs[i]) > 0);
147                         bool main_axis_collides =
148                                         negative_axis_collides || positive_axis_collides;
149                         
150                         /*
151                                 Check overlap of object and node in other axes
152                         */
153                         bool other_axes_overlap = true;
154                         for(u16 j=0; j<3; j++)
155                         {
156                                 if(j == i)
157                                         continue;
158                                 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
159                                 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
160                                 f32 objectmax = box.MaxEdge.dotProduct(dirs[j]);
161                                 f32 objectmin = box.MinEdge.dotProduct(dirs[j]);
162                                 if(!(nodemax - d > objectmin && nodemin + d < objectmax))
163                                 {
164                                         other_axes_overlap = false;
165                                         break;
166                                 }
167                         }
168                         
169                         /*
170                                 If this is a collision, revert the pos_f in the main
171                                 direction.
172                         */
173                         if(other_axes_overlap && main_axis_collides)
174                         {
175                                 speed_f -= speed_f.dotProduct(dirs[i]) * dirs[i];
176                                 pos_f -= pos_f.dotProduct(dirs[i]) * dirs[i];
177                                 pos_f += oldpos_f.dotProduct(dirs[i]) * dirs[i];
178                         }
179                 
180                 }
181         } // xyz
182         
183         return result;
184 }
185
186 collisionMoveResult collisionMovePrecise(Map *map, f32 pos_max_d,
187                 const core::aabbox3d<f32> &box_0,
188                 f32 dtime, v3f &pos_f, v3f &speed_f)
189 {
190         collisionMoveResult final_result;
191
192         // Maximum time increment (for collision detection etc)
193         // time = distance / speed
194         f32 dtime_max_increment = pos_max_d / speed_f.getLength();
195         
196         // Maximum time increment is 10ms or lower
197         if(dtime_max_increment > 0.01)
198                 dtime_max_increment = 0.01;
199         
200         // Don't allow overly huge dtime
201         if(dtime > 2.0)
202                 dtime = 2.0;
203         
204         f32 dtime_downcount = dtime;
205
206         u32 loopcount = 0;
207         do
208         {
209                 loopcount++;
210
211                 f32 dtime_part;
212                 if(dtime_downcount > dtime_max_increment)
213                 {
214                         dtime_part = dtime_max_increment;
215                         dtime_downcount -= dtime_part;
216                 }
217                 else
218                 {
219                         dtime_part = dtime_downcount;
220                         /*
221                                 Setting this to 0 (no -=dtime_part) disables an infinite loop
222                                 when dtime_part is so small that dtime_downcount -= dtime_part
223                                 does nothing
224                         */
225                         dtime_downcount = 0;
226                 }
227
228                 collisionMoveResult result = collisionMoveSimple(map, pos_max_d,
229                                 box_0, dtime_part, pos_f, speed_f);
230
231                 if(result.touching_ground)
232                         final_result.touching_ground = true;
233         }
234         while(dtime_downcount > 0.001);
235                 
236
237         return final_result;
238 }
239
240