From e6fbd5d4001db7e97b2ad87fd994f31350a476f6 Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Fri, 11 Feb 2022 11:25:27 -0300 Subject: [PATCH] added collision in front axis --- automobiles_buggy/buggy_entities.lua | 19 +++----- automobiles_coupe/coupe_entities.lua | 20 +++----- automobiles_lib/ground_detection.lua | 56 +++++++++++++--------- automobiles_roadster/roadster_entities.lua | 19 +++----- 4 files changed, 54 insertions(+), 60 deletions(-) diff --git a/automobiles_buggy/buggy_entities.lua b/automobiles_buggy/buggy_entities.lua index 3d7a8f4..a854a30 100755 --- a/automobiles_buggy/buggy_entities.lua +++ b/automobiles_buggy/buggy_entities.lua @@ -559,18 +559,6 @@ minetest.register_entity("automobiles_buggy:buggy", { self._steering_angle / 30 * turn_rate * automobiles_lib.sign(longit_speed) end - self._last_ground_check = self._last_ground_check + dtime - if self._last_ground_check > 0.18 then - self._last_ground_check = 0 - automobiles_lib.ground_get_distances(self, 0.372, 2.3) - end - - --[[if player and is_attached then - player:set_look_horizontal(newyaw) - end]]-- - - local newpitch = self._pitch --velocity.y * math.rad(6) - --[[ accell correction under some circunstances the acceleration exceeds the max value accepted by set_acceleration and @@ -622,6 +610,13 @@ minetest.register_entity("automobiles_buggy:buggy", { end end + self._last_ground_check = self._last_ground_check + dtime + if self._last_ground_check > 0.18 then + self._last_ground_check = 0 + automobiles_lib.ground_get_distances(self, 0.372, 2.3) + end + local newpitch = self._pitch --velocity.y * math.rad(6) + if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=0}) end --saves last velocity for collision detection (abrupt stop) diff --git a/automobiles_coupe/coupe_entities.lua b/automobiles_coupe/coupe_entities.lua index 0c30e4b..4d177ef 100755 --- a/automobiles_coupe/coupe_entities.lua +++ b/automobiles_coupe/coupe_entities.lua @@ -586,19 +586,6 @@ minetest.register_entity("automobiles_coupe:coupe", { self._turn_light_timer = 1 end - - self._last_ground_check = self._last_ground_check + dtime - if self._last_ground_check > 0.18 then - self._last_ground_check = 0 - automobiles_lib.ground_get_distances(self, 0.372, 2.3) - end - - --[[if player and is_attached then - player:set_look_horizontal(newyaw) - end]]-- - - local newpitch = self._pitch --velocity.y * math.rad(6) - --[[ accell correction under some circunstances the acceleration exceeds the max value accepted by set_acceleration and @@ -650,6 +637,13 @@ minetest.register_entity("automobiles_coupe:coupe", { end end + self._last_ground_check = self._last_ground_check + dtime + if self._last_ground_check > 0.18 then + self._last_ground_check = 0 + automobiles_lib.ground_get_distances(self, 0.372, 2.3) + end + local newpitch = self._pitch --velocity.y * math.rad(6) + if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=0}) end --saves last velocity for collision detection (abrupt stop) diff --git a/automobiles_lib/ground_detection.lua b/automobiles_lib/ground_detection.lua index 69cdeea..550d9fb 100644 --- a/automobiles_lib/ground_detection.lua +++ b/automobiles_lib/ground_detection.lua @@ -1,6 +1,5 @@ --lets assume that the rear axis is at object center, so we will use the distance only for front wheels function automobiles_lib.ground_get_distances(self, radius, axis_distance) - --local mid_axis = (axis_length / 2)/10 local hip = axis_distance --minetest.chat_send_all("entre-eixo "..hip) @@ -21,6 +20,10 @@ function automobiles_lib.ground_get_distances(self, radius, axis_distance) local rear_obstacle_level = automobiles_lib.get_obstacle(rear_axis) --minetest.chat_send_all("rear"..dump(rear_obstacle_level)) + if not self._last_front_detection then + self._last_front_detection = rear_obstacle_level.y + end + local f_x, f_z = automobiles_lib.get_xz_from_hipotenuse(pos.x, pos.z, yaw, hip) local f_y = pos.y --local x, f_y = automobiles_lib.get_xz_from_hipotenuse(f_x, r_y, pitch - math.rad(90), hip) --the x is only a mock @@ -56,29 +59,36 @@ function automobiles_lib.ground_get_distances(self, radius, axis_distance) --lets try to get the pitch if front_obstacle_level.y ~= nil and rear_obstacle_level.y ~= nil then - local deltaX = axis_distance; - local deltaY = front_obstacle_level.y - rear_obstacle_level.y; - --minetest.chat_send_all("deutaY "..deltaY) - local m = (deltaY/deltaX) - pitch = math.atan(m) --math.atan2(deltaY, deltaX); - --minetest.chat_send_all("m: "..m.." pitch ".. math.deg(pitch)) - - --[[if mid_car_level then - deltaX = axis_distance/2; - if mid_car_level.y ~= nil then - local deltaY_mid = mid_car_level.y - rear_obstacle_level.y; - if deltaY >= 1 and deltaY_mid < (deltaY / 2) then - --self.initial_properties.stepheight - pitch = math.rad(0) - end - --[[m = (deltaY_mid/deltaX) - local midpitch = math.atan(m) - if math.abs(math.deg(pitch) - math.deg(midpitch)) < 20 then - pitch = pitch + ((pitch - midpitch) / 2) - end]]-- - --[[end - end]]-- + if (front_obstacle_level.y-self._last_front_detection) <= self.initial_properties.stepheight then + self._last_front_detection = front_obstacle_level.y + local deltaX = axis_distance; + local deltaY = front_obstacle_level.y - rear_obstacle_level.y; + --minetest.chat_send_all("deutaY "..deltaY) + local m = (deltaY/deltaX) + pitch = math.atan(m) --math.atan2(deltaY, deltaX); + --minetest.chat_send_all("m: "..m.." pitch ".. math.deg(pitch)) + --[[if mid_car_level then + deltaX = axis_distance/2; + if mid_car_level.y ~= nil then + local deltaY_mid = mid_car_level.y - rear_obstacle_level.y; + if deltaY >= 1 and deltaY_mid < (deltaY / 2) then + --self.initial_properties.stepheight + pitch = math.rad(0) + end + --[[m = (deltaY_mid/deltaX) + local midpitch = math.atan(m) + if math.abs(math.deg(pitch) - math.deg(midpitch)) < 20 then + pitch = pitch + ((pitch - midpitch) / 2) + end]]-- + --[[end + end]]-- + else + self._last_front_detection = rear_obstacle_level.y + --here we need to set the collision effect + self.object:set_acceleration({x=0,y=0,z=0}) + self.object:set_velocity({x=0,y=0,z=0}) + end else pitch = math.rad(0) end diff --git a/automobiles_roadster/roadster_entities.lua b/automobiles_roadster/roadster_entities.lua index b835d82..5bb1829 100755 --- a/automobiles_roadster/roadster_entities.lua +++ b/automobiles_roadster/roadster_entities.lua @@ -532,18 +532,6 @@ minetest.register_entity("automobiles_roadster:roadster", { self._steering_angle / 30 * turn_rate * automobiles_lib.sign(longit_speed) end - self._last_ground_check = self._last_ground_check + dtime - if self._last_ground_check > 0.18 then - self._last_ground_check = 0 - automobiles_lib.ground_get_distances(self, 0.5, 2.422) - end - - --[[if player and is_attached then - player:set_look_horizontal(newyaw) - end]]-- - - local newpitch = self._pitch --velocity.y * math.rad(6) - --[[ accell correction under some circunstances the acceleration exceeds the max value accepted by set_acceleration and @@ -595,6 +583,13 @@ minetest.register_entity("automobiles_roadster:roadster", { end end + self._last_ground_check = self._last_ground_check + dtime + if self._last_ground_check > 0.18 then + self._last_ground_check = 0 + automobiles_lib.ground_get_distances(self, 0.5, 2.422) + end + local newpitch = self._pitch --velocity.y * math.rad(6) + if newyaw~=yaw or newpitch~=pitch then self.object:set_rotation({x=newpitch,y=newyaw,z=0}) end --saves last velocity for collision detection (abrupt stop)