mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 10:03:45 +01:00
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
This commit is contained in:
parent
05fe3b06c8
commit
a90d27e1e2
@ -322,9 +322,12 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
|
|||||||
}
|
}
|
||||||
std::vector<aabb3f> nodeboxes;
|
std::vector<aabb3f> nodeboxes;
|
||||||
n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
|
n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
|
||||||
|
|
||||||
|
// Calculate float position only once
|
||||||
|
v3f posf = intToFloat(p, BS);
|
||||||
for (auto box : nodeboxes) {
|
for (auto box : nodeboxes) {
|
||||||
box.MinEdge += intToFloat(p, BS);
|
box.MinEdge += posf;
|
||||||
box.MaxEdge += intToFloat(p, BS);
|
box.MaxEdge += posf;
|
||||||
cinfo.emplace_back(false, false, n_bouncy_value, p, box);
|
cinfo.emplace_back(false, false, n_bouncy_value, p, box);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -437,7 +440,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
|
|||||||
Go through every nodebox, find nearest collision
|
Go through every nodebox, find nearest collision
|
||||||
*/
|
*/
|
||||||
for (u32 boxindex = 0; boxindex < cinfo.size(); boxindex++) {
|
for (u32 boxindex = 0; boxindex < cinfo.size(); boxindex++) {
|
||||||
NearbyCollisionInfo box_info = cinfo[boxindex];
|
const NearbyCollisionInfo &box_info = cinfo[boxindex];
|
||||||
// Ignore if already stepped up this nodebox.
|
// Ignore if already stepped up this nodebox.
|
||||||
if (box_info.is_step_up)
|
if (box_info.is_step_up)
|
||||||
continue;
|
continue;
|
||||||
|
@ -108,7 +108,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
|
|||||||
{
|
{
|
||||||
// Maximum radius of a block. The magic number is
|
// Maximum radius of a block. The magic number is
|
||||||
// sqrt(3.0) / 2.0 in literal form.
|
// sqrt(3.0) / 2.0 in literal form.
|
||||||
const f32 block_max_radius = 0.866025403784 * MAP_BLOCKSIZE * BS;
|
static constexpr const f32 block_max_radius = 0.866025403784f * MAP_BLOCKSIZE * BS;
|
||||||
|
|
||||||
v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
|
v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;
|
||||||
|
|
||||||
@ -125,16 +125,16 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
|
|||||||
// Total distance
|
// Total distance
|
||||||
f32 d = MYMAX(0, blockpos_relative.getLength() - block_max_radius);
|
f32 d = MYMAX(0, blockpos_relative.getLength() - block_max_radius);
|
||||||
|
|
||||||
if(distance_ptr)
|
if (distance_ptr)
|
||||||
*distance_ptr = d;
|
*distance_ptr = d;
|
||||||
|
|
||||||
// If block is far away, it's not in sight
|
// If block is far away, it's not in sight
|
||||||
if(d > range)
|
if (d > range)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// If block is (nearly) touching the camera, don't
|
// If block is (nearly) touching the camera, don't
|
||||||
// bother validating further (that is, render it anyway)
|
// bother validating further (that is, render it anyway)
|
||||||
if(d == 0)
|
if (d == 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Adjust camera position, for purposes of computing the angle,
|
// Adjust camera position, for purposes of computing the angle,
|
||||||
@ -166,12 +166,13 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
|
|||||||
s16 adjustDist(s16 dist, float zoom_fov)
|
s16 adjustDist(s16 dist, float zoom_fov)
|
||||||
{
|
{
|
||||||
// 1.775 ~= 72 * PI / 180 * 1.4, the default on the client
|
// 1.775 ~= 72 * PI / 180 * 1.4, the default on the client
|
||||||
const float default_fov = 1.775f;
|
static constexpr const float default_fov = 1.775f / 2.0f;
|
||||||
// heuristic cut-off for zooming
|
// heuristic cut-off for zooming
|
||||||
if (zoom_fov > default_fov / 2.0f)
|
if (zoom_fov > default_fov)
|
||||||
return dist;
|
return dist;
|
||||||
|
|
||||||
// new_dist = dist * ((1 - cos(FOV / 2)) / (1-cos(zoomFOV /2))) ^ (1/3)
|
// new_dist = dist * ((1 - cos(FOV / 2)) / (1-cos(zoomFOV /2))) ^ (1/3)
|
||||||
return round(dist * std::cbrt((1.0f - std::cos(default_fov / 2.0f)) /
|
// note: FOV is calculated at compilation time
|
||||||
|
return round(dist * std::cbrt((1.0f - std::cos(default_fov)) /
|
||||||
(1.0f - std::cos(zoom_fov / 2.0f))));
|
(1.0f - std::cos(zoom_fov / 2.0f))));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user