mirror of
https://github.com/APercy/automobiles_pck
synced 2025-08-27 15:16:23 +02:00
function to calculate rotations fixed
This commit is contained in:
parent
6816476cec
commit
2a51414cd8
145
automobiles/automobiles_control.lua
Normal file
145
automobiles/automobiles_control.lua
Normal file
@ -0,0 +1,145 @@
|
||||
--global constants
|
||||
|
||||
automobiles.vector_up = vector.new(0, 1, 0)
|
||||
|
||||
function automobiles.check_road_is_ok(obj, max_acc_factor)
|
||||
local pos_below = obj:get_pos()
|
||||
pos_below.y = pos_below.y - 0.1
|
||||
local node_below = minetest.get_node(pos_below).name
|
||||
--minetest.chat_send_all(node_below)
|
||||
local nodedef = minetest.registered_nodes[node_below]
|
||||
if nodedef.liquidtype == "none" then
|
||||
local slow_nodes = {
|
||||
['default:ice '] = 0.01,
|
||||
['default:cave_ice'] = 0.01,
|
||||
}
|
||||
local acc = slow_nodes[node_below]
|
||||
if acc == nil then acc = max_acc_factor end
|
||||
return acc
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
function automobiles.control(self, dtime, hull_direction, longit_speed, longit_drag, later_drag, accel, max_acc_factor, max_speed, steering_limit, steering_speed)
|
||||
self._last_time_command = self._last_time_command + dtime
|
||||
if self._last_time_command > 1 then self._last_time_command = 1 end
|
||||
|
||||
local player = minetest.get_player_by_name(self.driver_name)
|
||||
local retval_accel = accel;
|
||||
local stop = false
|
||||
|
||||
-- player control
|
||||
if player then
|
||||
local ctrl = player:get_player_control()
|
||||
|
||||
local acc = 0
|
||||
if self._engine_running then
|
||||
--running
|
||||
if longit_speed < max_speed and ctrl.up then
|
||||
--get acceleration factor
|
||||
acc = automobiles.check_road_is_ok(self.object, max_acc_factor)
|
||||
--minetest.chat_send_all('engineacc: '.. engineacc)
|
||||
if acc > 1 and acc < max_acc_factor and longit_speed > 0 then
|
||||
--improper road will reduce speed
|
||||
acc = -1
|
||||
end
|
||||
end
|
||||
else
|
||||
--slow maneuver
|
||||
if longit_speed < roadster.max_speed and ctrl.up then
|
||||
--get acceleration factor
|
||||
acc = automobiles.check_road_is_ok(self.object, max_acc_factor)
|
||||
--minetest.chat_send_all('engineacc: '.. engineacc)
|
||||
if acc > 1 and acc < max_acc_factor and longit_speed > 0 then
|
||||
--improper road will reduce speed
|
||||
acc = -1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--reversing
|
||||
if ctrl.sneak and longit_speed <= 1.0 and longit_speed > -1.0 then
|
||||
acc = -1
|
||||
end
|
||||
|
||||
--break
|
||||
if ctrl.down then
|
||||
--[[if math.abs(longit_speed) > 0 then
|
||||
acc = -5 / (longit_speed / 2) -- lets set a brake efficience based on speed
|
||||
end]]--
|
||||
|
||||
--total stop
|
||||
--wheel break
|
||||
if longit_speed > 0 then
|
||||
acc = -5
|
||||
if (longit_speed + acc) < 0 then
|
||||
acc = longit_speed * -1
|
||||
end
|
||||
end
|
||||
if longit_speed < 0 then
|
||||
acc = 5
|
||||
if (longit_speed + acc) > 0 then
|
||||
acc = longit_speed * -1
|
||||
end
|
||||
end
|
||||
if abs(longit_speed) < 0.2 then
|
||||
stop = true
|
||||
end
|
||||
end
|
||||
|
||||
if acc then retval_accel=vector.add(accel,vector.multiply(hull_direction,acc)) end
|
||||
|
||||
if ctrl.aux1 then
|
||||
--[[
|
||||
--sets the engine running - but sets a delay also, cause keypress
|
||||
if self._last_time_command > 0.3 then
|
||||
self._last_time_command = 0
|
||||
if self._engine_running then
|
||||
self._engine_running = false
|
||||
-- sound and animation
|
||||
if self.sound_handle then
|
||||
minetest.sound_stop(self.sound_handle)
|
||||
self.sound_handle = nil
|
||||
end
|
||||
--self.engine:set_animation_frame_speed(0)
|
||||
|
||||
elseif self._engine_running == false and self._energy > 0 then
|
||||
self._engine_running = true
|
||||
-- sound and animation
|
||||
self.sound_handle = minetest.sound_play({name = "engine"},
|
||||
{object = self.object, gain = 2.0, pitch = 1.0, max_hear_distance = 32, loop = true,})
|
||||
--self.engine:set_animation_frame_speed(30)
|
||||
end
|
||||
end]]--
|
||||
end
|
||||
|
||||
-- steering
|
||||
if ctrl.right then
|
||||
self._steering_angle = math.max(self._steering_angle-steering_speed*dtime,-steering_limit)
|
||||
elseif ctrl.left then
|
||||
self._steering_angle = math.min(self._steering_angle+steering_speed*dtime,steering_limit)
|
||||
else
|
||||
--center steering
|
||||
if longit_speed > 0 then
|
||||
local factor = 1
|
||||
if self._steering_angle > 0 then factor = -1 end
|
||||
local correction = (steering_limit*(longit_speed/100)) * factor
|
||||
local before_correction = self._steering_angle
|
||||
self._steering_angle = self._steering_angle + correction
|
||||
if math.sign(before_correction) ~= math.sign(self._steering_angle) then self._steering_angle = 0 end
|
||||
end
|
||||
end
|
||||
|
||||
local angle_factor = self._steering_angle / 60
|
||||
if angle_factor < 0 then angle_factor = angle_factor * -1 end
|
||||
local deacc_on_curve = longit_speed * angle_factor
|
||||
deacc_on_curve = deacc_on_curve * -1
|
||||
if deacc_on_curve then retval_accel=vector.add(retval_accel,vector.multiply(hull_direction,deacc_on_curve)) end
|
||||
|
||||
end
|
||||
|
||||
return retval_accel, stop
|
||||
end
|
||||
|
||||
|
@ -7,73 +7,53 @@ 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)
|
||||
|
||||
|
||||
--[[local lf = self.lf_wheel:get_pos() -- left front
|
||||
--automobiles.get_wheel_distance_from_ground(lf, radius)
|
||||
|
||||
local rf = self.rf_wheel:get_pos() -- right front
|
||||
--automobiles.get_wheel_distance_from_ground(rf, radius)
|
||||
|
||||
--minetest.chat_send_all("x ".. lf.x .." x " .. rf.x .. "\ny " .. lf.y .. " y " .. rf.y .. "\nz " .. lf.z .. " z " .. rf.z)
|
||||
|
||||
local lr = self.lr_wheel:get_pos() -- left rear
|
||||
automobiles.get_wheel_distance_from_ground(lr, radius)
|
||||
|
||||
local rr = self.rr_wheel:get_pos() -- right rear
|
||||
automobiles.get_wheel_distance_from_ground(rr, radius)]]--
|
||||
|
||||
--[[
|
||||
--minetest.raycast(pos1, pos2, objects, liquids)
|
||||
|
||||
for pointed_thing in minetest.raycast(p_pos, e_pos, true, false) do
|
||||
automobiles.handle_ray(reference,pointed_thing)
|
||||
end]]--
|
||||
|
||||
--segunda tentativa fracassada:
|
||||
--[[local node = automobiles.get_node_below(self.object)
|
||||
if node then
|
||||
minetest.chat_send_all(node.name)
|
||||
end]]--
|
||||
|
||||
local mid_axis = (axis_length / 2)/10
|
||||
local hip = axis_distance / 10
|
||||
local hip = (axis_distance / 10) -- + ((axis_distance / 10)/3)
|
||||
|
||||
local yaw = self.object:get_yaw()
|
||||
local deg_yaw = math.deg(yaw)
|
||||
local yaw_turns = math.floor(deg_yaw / 360)
|
||||
deg_yaw = deg_yaw - (yaw_turns * 360)
|
||||
yaw = math.rad(deg_yaw)
|
||||
|
||||
local pos = self.object:get_pos()
|
||||
|
||||
local f_x, f_z = automobiles.get_xz_from_hipotenuse(pos.x, pos.z, yaw, hip)
|
||||
local x, f_y = automobiles.get_xz_from_hipotenuse(pos.x, pos.y, self._pitch, hip) --the x is only a mock
|
||||
|
||||
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-90, mid_axis)
|
||||
left_front.x, left_front.z = automobiles.get_xz_from_hipotenuse(f_x, f_z, yaw+math.rad(90), mid_axis)
|
||||
|
||||
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+90, mid_axis)
|
||||
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 = 0
|
||||
x, r_y = automobiles.get_xz_from_hipotenuse(pos.x, pos.y, self._pitch, 0) --the x is only a mock
|
||||
|
||||
local left_rear = {x=0, y=r_y, z=0}
|
||||
left_rear.x, left_rear.z = automobiles.get_xz_from_hipotenuse(r_x, r_z, yaw-90, mid_axis)
|
||||
left_rear.x, left_rear.z = automobiles.get_xz_from_hipotenuse(r_x, r_z, yaw+math.rad(90), mid_axis)
|
||||
|
||||
local right_rear = {x=0, y=r_y, z=0}
|
||||
right_rear.x, right_rear.z = automobiles.get_xz_from_hipotenuse(r_x, r_z, yaw+90, mid_axis)
|
||||
right_rear.x, right_rear.z = automobiles.get_xz_from_hipotenuse(r_x, r_z, yaw-math.rad(90), mid_axis)
|
||||
|
||||
|
||||
--minetest.chat_send_all("front x ".. right_front.x .." x " .. left_front.x .. " --- z " .. right_front.z .. " z " .. left_front.z .. " Y: " .. right_front.y)
|
||||
--minetest.chat_send_all("x ".. f_x .. " --- z " .. f_z .. " || " .. math.deg(yaw))
|
||||
--minetest.chat_send_all("front x ".. right_front.x .. " - z " .. right_front.z .. " Yaw: " .. math.deg(yaw-math.rad(90)) .. " ||| x " .. left_front.x .. " - z " .. left_front.z .. " Yaw: " .. math.deg(yaw+math.rad(90)))
|
||||
--minetest.chat_send_all("rear x ".. right_rear.x .." x " .. left_rear.x .. " --- z " .. right_rear.z .. " z " .. left_rear.z .. " Y: " .. right_rear.y)
|
||||
|
||||
local node_bellow = automobiles.get_node_below(left_front, 0.5)
|
||||
if node_bellow then
|
||||
--minetest.chat_send_all("bellow: " .. node_bellow.name)
|
||||
local node = minetest.get_node(left_front)
|
||||
local node = automobiles.get_node_below(left_front, -0.5)
|
||||
--minetest.chat_send_all("level: " .. node.name)
|
||||
--minetest.chat_send_all(dump(mobkit.nodeatpos(left_front).node_box))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function automobiles.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance)
|
||||
--cara, o minetest é bizarro, ele considera o eixo no sentido ANTI-HORÁRIO... Então pra equação funcionar, subtrair o angulo de 360 antes
|
||||
yaw = math.rad(360) - yaw
|
||||
local z = (math.cos(yaw)*distance) + orig_z
|
||||
local x = (math.sin(yaw)*distance) + orig_x
|
||||
return x, z
|
||||
|
Loading…
Reference in New Issue
Block a user