From 84aadea47adc1faecb1ceffb67974a0f11347886 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 24 Dec 2020 19:44:51 +0000 Subject: [PATCH] Basic Map Operations: Fix distance check being 2d --- _en/map/environment.md | 11 ++++++----- _it/map/environment.md | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/_en/map/environment.md b/_en/map/environment.md index 73f251d..227fce8 100644 --- a/_en/map/environment.md +++ b/_en/map/environment.md @@ -13,11 +13,11 @@ In this chapter, you will learn how to perform basic actions on the map. - [Map Structure](#map-structure) - [Reading](#reading) - - [Reading Nodes](#reading-nodes) - - [Finding Nodes](#finding-nodes) + - [Reading Nodes](#reading-nodes) + - [Finding Nodes](#finding-nodes) - [Writing](#writing) - - [Writing Nodes](#writing-nodes) - - [Removing Nodes](#removing-nodes) + - [Writing Nodes](#writing-nodes) + - [Removing Nodes](#removing-nodes) - [Loading Blocks](#loading-blocks) - [Deleting Blocks](#deleting-blocks) @@ -106,13 +106,14 @@ local pos_list = local grow_speed = 1 for i=1, #pos_list do local delta = vector.subtract(pos_list[i], pos) - if delta.x*delta.x + delta.y*delta.y <= 5*5 then + if delta.x*delta.x + delta.y*delta.y + delta.z*delta.z <= 5*5 then grow_speed = grow_speed + 1 end end ``` Now your code will correctly increase `grow_speed` based on mese nodes in range. + Note how we compared the squared distance from the position, rather than square rooting it to obtain the actual distance. This is because computers find square roots computationally expensive, so you should avoid them as much as possible. diff --git a/_it/map/environment.md b/_it/map/environment.md index 0680a23..450d407 100644 --- a/_it/map/environment.md +++ b/_it/map/environment.md @@ -98,13 +98,14 @@ local lista_pos = local vel_crescita = 1 for i=1, #lista_pos do local delta = vector.subtract(lista_pos[i], pos) - if delta.x*delta.x + delta.y*delta.y <= 5*5 then + if delta.x*delta.x + delta.y*delta.y + delta.z*delta.z <= 5*5 then vel_crescita = vel_crescita + 1 end end ``` Ora il codice aumenterà correttamente `vel_crescita` basandosi su quanti nodi di pietra ci sono in un intervallo. + Notare come si sia comparata la distanza al quadrato dalla posizione, invece che calcolarne la radice quadrata per ottenerne la distanza vera e propria. Questo perché i computer trovano le radici quadrate computazionalmente pesanti, quindi dovresti evitare di usarle il più possibile.