Skip to content

Commit a90d27e

Browse files
authoredApr 3, 2018
Optimize a little bit isBlockInSight, adjustDist & collisions (#7193)
* Use constexpr + unroll some calculations to cache definitively some calculations * Unroll some calls in collision code & use a constref instead of a copy in one occurence
1 parent 05fe3b0 commit a90d27e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed
 

‎src/collision.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,12 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
322322
}
323323
std::vector<aabb3f> nodeboxes;
324324
n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
325+
326+
// Calculate float position only once
327+
v3f posf = intToFloat(p, BS);
325328
for (auto box : nodeboxes) {
326-
box.MinEdge += intToFloat(p, BS);
327-
box.MaxEdge += intToFloat(p, BS);
329+
box.MinEdge += posf;
330+
box.MaxEdge += posf;
328331
cinfo.emplace_back(false, false, n_bouncy_value, p, box);
329332
}
330333
} else {
@@ -437,7 +440,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
437440
Go through every nodebox, find nearest collision
438441
*/
439442
for (u32 boxindex = 0; boxindex < cinfo.size(); boxindex++) {
440-
NearbyCollisionInfo box_info = cinfo[boxindex];
443+
const NearbyCollisionInfo &box_info = cinfo[boxindex];
441444
// Ignore if already stepped up this nodebox.
442445
if (box_info.is_step_up)
443446
continue;

‎src/util/numeric.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
108108
{
109109
// Maximum radius of a block. The magic number is
110110
// sqrt(3.0) / 2.0 in literal form.
111-
const f32 block_max_radius = 0.866025403784 * MAP_BLOCKSIZE * BS;
111+
static constexpr const f32 block_max_radius = 0.866025403784f * MAP_BLOCKSIZE * BS;
112112

113113
v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
114114

@@ -125,16 +125,16 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
125125
// Total distance
126126
f32 d = MYMAX(0, blockpos_relative.getLength() - block_max_radius);
127127

128-
if(distance_ptr)
128+
if (distance_ptr)
129129
*distance_ptr = d;
130130

131131
// If block is far away, it's not in sight
132-
if(d > range)
132+
if (d > range)
133133
return false;
134134

135135
// If block is (nearly) touching the camera, don't
136136
// bother validating further (that is, render it anyway)
137-
if(d == 0)
137+
if (d == 0)
138138
return true;
139139

140140
// Adjust camera position, for purposes of computing the angle,
@@ -166,12 +166,13 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
166166
s16 adjustDist(s16 dist, float zoom_fov)
167167
{
168168
// 1.775 ~= 72 * PI / 180 * 1.4, the default on the client
169-
const float default_fov = 1.775f;
169+
static constexpr const float default_fov = 1.775f / 2.0f;
Has comments. Original line has comments.
170170
// heuristic cut-off for zooming
171-
if (zoom_fov > default_fov / 2.0f)
171+
if (zoom_fov > default_fov)
Has comments. Original line has comments.
172172
return dist;
173173

174174
// new_dist = dist * ((1 - cos(FOV / 2)) / (1-cos(zoomFOV /2))) ^ (1/3)
175-
return round(dist * std::cbrt((1.0f - std::cos(default_fov / 2.0f)) /
175+
// note: FOV is calculated at compilation time
176+
return round(dist * std::cbrt((1.0f - std::cos(default_fov)) /
Has comments. Original line has comments.
176177
(1.0f - std::cos(zoom_fov / 2.0f))));
177178
}

0 commit comments

Comments
 (0)
Please sign in to comment.