From 4a3383a17855ee91836b34cc31c63e1b059eccb8 Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Mon, 17 Jan 2022 09:32:15 -0300 Subject: [PATCH] ramp detection improved --- automobiles/ground_detection.lua | 107 ++++++--------------- automobiles_roadster/init.lua | 2 +- automobiles_roadster/roadster_entities.lua | 2 +- 3 files changed, 32 insertions(+), 79 deletions(-) diff --git a/automobiles/ground_detection.lua b/automobiles/ground_detection.lua index f358f31..ad1c5a4 100644 --- a/automobiles/ground_detection.lua +++ b/automobiles/ground_detection.lua @@ -5,10 +5,11 @@ proximo experimento sera mis complexo: pegar o yaw do carro, colocar o entreeixo --lets assume that the rear axis is at object center, so we will use the distance only for front wheels -function automobiles.ground_get_distances(self, radius, axis_length, axis_distance) +function automobiles.ground_get_distances(self, radius, axis_distance) - local mid_axis = (axis_length / 2)/10 - local hip = (axis_distance / 10) -- + ((axis_distance / 10)/3) + --local mid_axis = (axis_length / 2)/10 + local hip = (axis_distance / 10) + minetest.chat_send_all("entre-eixo "..hip) local pitch = self._pitch --+90 for the calculations local yaw = self.object:get_yaw() @@ -19,10 +20,16 @@ function automobiles.ground_get_distances(self, radius, axis_length, axis_distan local pos = self.object:get_pos() + local r_x, r_z = automobiles.get_xz_from_hipotenuse(pos.x, pos.z, yaw, 0) + local r_y = pos.y + local rear_axis = {x=r_x, y=r_y, z=r_z} + + local rear_obstacle_level = automobiles.get_obstacle(rear_axis) + local f_x, f_z = automobiles.get_xz_from_hipotenuse(pos.x, pos.z, yaw, hip) --local f_y, x = automobiles.get_xz_from_hipotenuse(pos.y, pos.x, 0, hip) --the x is only a mock local front_axis = {x=f_x, y=pos.y, z=f_z} - local front_obstacle_level = automobiles.get_obstacle(front_axis, 0.45) + local front_obstacle_level = automobiles.get_obstacle(front_axis) --[[local left_front = {x=0, y=f_y, z=0} left_front.x, left_front.z = automobiles.get_xz_from_hipotenuse(f_x, f_z, yaw+math.rad(90), mid_axis) @@ -30,10 +37,7 @@ function automobiles.ground_get_distances(self, radius, axis_length, axis_distan local right_front = {x=0, y=f_y, z=0} right_front.x, right_front.z = automobiles.get_xz_from_hipotenuse(f_x, f_z, yaw-math.rad(90), mid_axis)]]-- - --[[local r_x, r_z = automobiles.get_xz_from_hipotenuse(pos.x, pos.z, yaw, 0) - local r_y = pos.y - --r_y, x = automobiles.get_xz_from_hipotenuse(pos.y, pos.x, 0, 0) --the x is only a mock - local rear_axis = {x=r_x, y=r_y, z=r_z} + --[[ local rear_obstacle_level = automobiles.get_obstacle(rear_axis, 0.2, 0.25)]]-- @@ -45,10 +49,11 @@ function automobiles.ground_get_distances(self, radius, axis_length, axis_distan --lets try to get the pitch local deltaX = axis_distance; - local deltaY = pos.y - front_obstacle_level.y; - --minetest.chat_send_all("deutaY "..deltaY) - pitch = math.atan2(deltaY, deltaX); - --minetest.chat_send_all(" pitch ".. math.deg(pitch).." -- r: "..pos.y.." -- f: "..front_obstacle_level.y) + local deltaY = front_obstacle_level.y - rear_obstacle_level.y; + minetest.chat_send_all("deutaY "..deltaY) + local m = (deltaY/deltaX)*10 + pitch = math.atan(m) --math.atan2(deltaY, deltaX); + minetest.chat_send_all("m: "..m.." pitch ".. math.deg(pitch)) self._pitch = pitch end @@ -61,88 +66,36 @@ function automobiles.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance) return x, z end -function automobiles.get_obstacle(ref_pos, y_max) +function automobiles.get_obstacle(ref_pos) --lets clone the table local retval = {x=ref_pos.x, y=ref_pos.y, z=ref_pos.z} --minetest.chat_send_all("aa y: " .. dump(retval.y)) local i_pos = {x=ref_pos.x, y=ref_pos.y, z=ref_pos.z} --minetest.chat_send_all("bb y: " .. dump(retval.y)) - - local e_pos = {x=ref_pos.x, y=ref_pos.y, z=ref_pos.z} - e_pos.y = e_pos.y - y_max - --//upper position - --minetest.chat_send_all("ref y: " .. dump(ref_pos.y) .. " ret Y: ".. dump(retval.y)) - local cast = minetest.raycast(i_pos, e_pos, true, false) + retval.y = eval_interception(i_pos, {x=i_pos.x, y=i_pos.y - 2, z=i_pos.z}) + + --minetest.chat_send_all("y: " .. dump(ref_pos.y) .. " ye: ".. dump(retval.y)) + return retval +end + +function eval_interception(initial_pos, end_pos) + local ret_y = nil + local cast = minetest.raycast(initial_pos, end_pos, true, false) local thing = cast:next() while thing do if thing.type == "node" then local pos = thing.intersection_point if pos then - retval.y = e_pos.y - 1 - --minetest.chat_send_all("ray intercection: " .. dump(pos.y)) - local node_name = minetest.get_node(thing.under).name + ret_y = pos.y + --local node_name = minetest.get_node(thing.under).name --minetest.chat_send_all("ray intercection: " .. dump(pos.y) .. " -- " .. node_name) break end end thing = cast:next() end - - e_pos.y = e_pos.y + (y_max/2) - cast = minetest.raycast(i_pos, e_pos, true, false) - thing = cast:next() - while thing do - if thing.type == "node" then - local pos = thing.intersection_point - if pos then - retval.y = retval.y - 4 - local node_name = minetest.get_node(thing.under).name - --minetest.chat_send_all("ray 2 intercection: " .. dump(pos.y) .. " -- " .. node_name) - break - end - end - thing = cast:next() - end - - --lower position - e_pos = {x=ref_pos.x, y=ref_pos.y, z=ref_pos.z} - e_pos.y = e_pos.y - y_max - 0.20 - - cast = minetest.raycast(i_pos, e_pos, true, false) - thing = cast:next() - if thing == nil then retval.y = retval.y + 2 end - while thing do - if thing.type == "node" then - local pos = thing.intersection_point - if pos then - --retval = pos - local node_name = minetest.get_node(thing.under).name - --minetest.chat_send_all("ray 3 intercection: " .. dump(pos.y) .. " -- " .. node_name) - end - end - thing = cast:next() - end - - e_pos.y = e_pos.y - 0.55 - - cast = minetest.raycast(i_pos, e_pos, true, false) - thing = cast:next() - if thing == nil then retval.y = retval.y + 10 end - while thing do - if thing.type == "node" then - local pos = thing.intersection_point - if pos then - --retval = pos - local node_name = minetest.get_node(thing.under).name - --minetest.chat_send_all("ray 4 intercection: " .. dump(pos.y) .. " -- " .. node_name) - end - end - thing = cast:next() - end - - --minetest.chat_send_all("y: " .. dump(ref_pos.y) .. " ye: ".. dump(retval.y)) - return retval + return ret_y end function automobiles.get_node_below(pos, dist) diff --git a/automobiles_roadster/init.lua b/automobiles_roadster/init.lua index 9ff79b4..4b2def0 100755 --- a/automobiles_roadster/init.lua +++ b/automobiles_roadster/init.lua @@ -6,7 +6,7 @@ roadster.LONGIT_DRAG_FACTOR = 0.16*0.16 roadster.LATER_DRAG_FACTOR = 30.0 roadster.gravity = automobiles.gravity roadster.max_speed = 10 -roadster.max_acc_factor = 6 +roadster.max_acc_factor = 4 roadster.max_fuel = 10 roadster.front_wheel_xpos = 10.26 diff --git a/automobiles_roadster/roadster_entities.lua b/automobiles_roadster/roadster_entities.lua index 923be99..cd6d2d2 100755 --- a/automobiles_roadster/roadster_entities.lua +++ b/automobiles_roadster/roadster_entities.lua @@ -433,7 +433,7 @@ minetest.register_entity("automobiles_roadster:roadster", { self._steering_angle / 30 * turn_rate * automobiles.sign(longit_speed) end - automobiles.ground_get_distances(self, 0.5, 20.52, 24.22) + automobiles.ground_get_distances(self, 0.5, 24.22) --[[if player and is_attached then player:set_look_horizontal(newyaw)