Looks good so far!
I would detect if a ship is speeding up or slowing down by checking the current and last tick, similar to what you wrote. You could just declare a variable in the main():
- Code:
function main()
lastVelocity = nil
end
'nil' means 'nothing'. You could probably remove the nil, but I am not sure you can declare a variable like this.
Then at each tick, you could compare the last velocity with the current one. But watch out! The first time the onTick() runs, you won't have a 'last velocity', so you can just setup another variable in main() to check if this is the first onTick():
Then, in onTick(), you can just set that variable to false when it runs for the first time:
- Code:
function onTick()
-- Get ship here
if(firstTick==true) then
firstTick = false -- Set the variable to false, like that the if will not run again
lastVelocity = ship:getVel() -- Put the ship's velocity in the variable
return -- Get out of the function, so nothing else of that function is ran this time
end
end
Then, in onTick(), under the 'if', you can compare both velocities:
- Code:
local shipVelocityNumber = convertToPositive(ship:getVel().x) + convertToPositive(ship:getVel().y) -- Make sure number is positive
local lastVelocityNumber =convertToPositive(lastVelocity.x) + convertToPositive(lastVelocity.y) -- Make sure number is positive
if(shipVelocityNumber<lastVelocityNumber) then
-- Slowing down!
elseif(shipVelocityNumber>lastVelocityNumber) then
-- Speeding up
else
-- Same speed!
end
The problem is: velocity x and y are sometimes negative, even if you are going fast. So, I am using this little function to make sure the values are positive. You can just include this code somewhere in your code
- Code:
function convertToPositive(value) -- value is a local variable for this function only
if(value<0) then -- If it is negative
return -value -- Return positive value
else
return value -- Return unchanged (positive) value
end
end
But, you need to update the lastVelocity constantly, so just do that again right over the onTicks()'s end:
- Code:
lastVelocity = ship:getVel()
So, the whole function would technically look like this, implying that you have a ship:
- Code:
function onTick()
local shipVelocityNumber = 0 -- Using variables to be more efficient
local lastVelocityNumber = 0
-- Get ship here
if(firstTick==true) then
firstTick = false -- Set the variable to false, like that the if will not run again
lastVelocity = ship:getVel() -- Put the ship's velocity in the variable
return -- Get out of the function, so nothing else of that function is ran this time
end
shipVelocityNumber = convertToPositive(ship:getVel().x) + convertToPositive(ship:getVel().y) -- Make sure number is positive
lastVelocityNumber =convertToPositive(lastVelocity.x) + convertToPositive(lastVelocity.y) -- Make sure number is positive
if(shipVelocityNumber<lastVelocityNumber) then
-- Slowing down!
elseif(shipVelocityNumber>lastVelocityNumber) then
-- Speeding up
else
-- Same speed!
end
lastVelocity = ship:getVel()
end
And the main() would have this:
- Code:
lastVelocity = nil
firstTick = true
I didn't have the time to test this, so it might not work.
If you have any questions, ask away