I am staring at the code for 3 hours or more, and I can't figure out a fix Need help :\
I have the piece of the code properly set up with console print, so I would really appreciate if someone can take a look at it
The troublesome part is in the helpers.lua file, at the updateShape function, code below.
basically it uses the currSpeed and vector.direction to properly move around the 4 points of my rectangle tank, but if you try to rotate, it start spinning incontrollably.
I think the problem is in this line :
Code: Select all
local angle = math.atan2(shape[i + 1] - self.pivot.y, shape[ i ] - self.pivot.x) + self.vector.direction
And I'm too tired to find the smart solution here I guess
Any suggestions?
Code: Select all
local function updateShape(self, dt)
local shape = self.shape
--update pivot
self.pivot.x = self.pivot.x + self.currSpeed * dt * math.cos(self.vector.direction)
self.pivot.y = self.pivot.y + self.currSpeed * dt * math.sin(self.vector.direction)
--update points
for i = 1, #shape - 1, 2 do
--x,y
shape[i] = shape[i] + self.currSpeed * dt * math.cos(self.vector.direction)
shape[i + 1] = shape[i + 1] + self.currSpeed * dt * math.sin(self.vector.direction)
end
--debug
print("direction: " .. string.format("%.2f",self.vector.direction))
print("pivot X,Y: (" .. string.format("%2d",self.pivot.x) .. ", " .. string.format("%2d",self.pivot.y) .. ")")
--add rotation
local step = 0
--- -[[
for i = 1, #shape - 1, 2 do --check x,y pairs for the 4 points of the rectangle (8 total values)
--distance from current point to pivot
local distance = math.sqrt(math.pow(shape[i] - self.pivot.x, 2) + math.pow(shape[i + 1] - self.pivot.y, 2))
--angle from pivot to point
local angle = math.atan2(shape[i + 1] - self.pivot.y, shape[i] - self.pivot.x) + self.vector.direction
--debug
print("X" .. i - step .. ",Y" .. i - step .. " Point angle:" .. string.format("%.2f",angle)) step = step + 1
--x,y
shape[i] = self.pivot.x + distance * math.cos(angle)
shape[i + 1] = self.pivot.y + distance * math.sin(angle)
end
--]]
end