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