I have object "rimmyx".
In rimmyx there is: variable
Code: Select all
self.angle
Code: Select all
self.main_angles
Why I did this? I need to calculate a points where I need to draw a picure,
So for each point I calculate anlge:
Code: Select all
self.angle + self.main_angles[i]
Code: Select all
self.main_angles
Player can rotate rimmyx with mouse. In this case, only
Code: Select all
self.angle
Also, I have a function
Code: Select all
self:findPart(_angle)
Code: Select all
self.main_angles
Code: Select all
self.main_angles
The problem is, My program crashes (sometimes), because of find function.
Exactly here:
Code: Select all
math.abs(_angle - (v[1] + self.angle))
Code: Select all
for i, v in ipairs(self.main_angles)
At some moment minimum
Code: Select all
math.abs(_angle - (v[1] + self.angle))
So, how I need to write it correctly?
Some art(no): Logs: whole code:
(sorry for non-english comments)
Code: Select all
rimmyx = Object:extend()
require "part"
angle_measure = 0
function rimmyx:new(_x, _y, _len, _is_left, _name)
angle_measure = 2 * math.pi / 6
self.x = _x
self.y = _y
self.main_len = _len
self.angle = 0
self.secondary_len = _len / math.cos(angle_measure / 2) -- вторая длина. До ВТОРОГО ряда деталей.
self.is_left = _is_left
self.delta_angle = 0 -- разница между углом до позиции мыши, и нынешним углом, используется в фунциях mousePressed и mouseReleased
self.target_angle = 0 -- куда нужно "довернуть колесо". Измеряется в радиантах
self.mouse_is_down = false
self.is_attached = nil
self.is_stabilized = true
self.attachment_index = nil
self.name = _name
-- creating a list of angles
self.main_angles = {}
self.secondary_angles = {}
self.img_main_angles = {}
self.img_secondary_angles = {}
-- filling list of angles
for i = 1,6 do
self.main_angles[i] = {(i - 1) * angle_measure, part((i - 1) * angle_measure, "cube.png")}
self.secondary_angles[i] = i * angle_measure - 1 * math.pi / 6
end
if not _is_left then
self.main_angles[4][2] = nil
self.is_attached = false
else
self.is_attached = true
end
--self.main_angles[1][2].angle = angle_measure * 4
end
function rimmyx:isAttached()
return self.is_attached
end
function rimmyx:findPart(_angle)--ищем угол с угловым значением _angle(в радианах).
print("trying to find angle: " .. _angle)
local m = angle_measure * 2
local ind = nil
for i, v in ipairs(self.main_angles) do
print("delta: " .. math.abs(_angle - (v[1] + self.angle)) .. " |self.angle, v[1], i|: " .. self.angle .. " " .. v[1] .. " " .. i)
if math.abs(_angle - (v[1] + self.angle)) <= 0.1 then -- math.abs(_angle - (v[1] + self.angle)) <= 0.5 or math.abs(math.abs(_angle - (v[1] + self.angle)) - 2 * math.pi) <= 0.5
ind = i
m = _angle - (v[1] + self.angle)
break
end
end
print("result index: " .. ind)
return ind
end
function rimmyx:popPart(_angle)
print("going to pop: " .. _angle .. " from " .. self.name)
local ind = self:findPart(_angle)
local returnable_part = self.main_angles[ind][2]
self.main_angles[ind][2] = nil
print("popped from " .. ind)
return returnable_part
end
function rimmyx:addPart(_part, _angle)
print("going to add: " .. _angle .. " to " .. self.name)
local ind = self:findPart(_angle)
print("added to " .. ind)
print("==============================================================================================")
_part.angle = _part.angle - self.angle
self.main_angles[ind][2] = _part
end
function rimmyx:addMainPart(_angle, _part)
--for i, v in ipairs(self.main_angles)
end
function rimmyx:addSecondaryPart (_angle, _part)
end
function rimmyx:mousePressed(x, y, button, isTouch)
self.delta_angle = math.atan2(y - self.y, x - self.x) - self.angle
self.mouse_is_down = true
end
function rimmyx:mouseReleased()
local a_angle = math.floor(self.angle / (2 * math.pi / 6))--находим 2 ближайших правильных угла поворота фигуры
local b_angle = math.ceil(self.angle / (2 * math.pi / 6))
self.is_stabilized = false
if math.abs(self.angle - a_angle) > math.abs(self.angle - b_angle) then -- их них находим ближайший
self.target_angle = b_angle * 2 * math.pi / 6
else
self.target_angle = a_angle * 2 * math.pi / 6
end
self.mouse_is_down = false
end
function rimmyx:update(dt)
if self.mouse_is_down == true then
mouse_x, mouse_y = love.mouse.getPosition()
self.angle = math.atan2(mouse_y - self.y, mouse_x - self.x) - self.delta_angle
else
if math.abs(self.angle - self.target_angle) < 0.001 and self.is_stabilized == false then
self.angle = self.target_angle
self.is_stabilized = true
else
self.angle = self.angle - (self.angle - self.target_angle) / 50
if self.angle >= 2 * math.pi then
self.angle = self.angle % (2 * math.pi)
end
end
end
end
function rimmyx:draw()
love.graphics.setColor(0, 1, 0, 1)
for i, v in ipairs(self.main_angles) do
if v[2] ~= nil then
local x, y = self.x + self.main_len * math.cos(v[1] + self.angle), self.y + self.main_len * math.sin(v[1] + self.angle)
love.graphics.line(self.x, self.y, x, y)
v[2]:changePosition(x, y)
love.graphics.setColor(1, 1, 1, 1)
v[2]:draw(self.angle)
love.graphics.setColor(1, 0, 0, 1)
love.graphics.print(v[2].angle .. " " .. (v[2].angle + self.angle) .. " " .. (v[1] + self.angle) .. " " .. i , x, y)
love.graphics.setColor(0, 1, 0, 1)
end
end
love.graphics.setColor(1, 0, 0, 1)
for i, v in ipairs(self.secondary_angles) do
local x, y = self.x + self.secondary_len * math.cos(v + self.angle), self.y + self.secondary_len * math.sin(v + self.angle)
love.graphics.line(self.x, self.y, x, y)
end
end