-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinertia.lua
61 lines (54 loc) · 1.84 KB
/
inertia.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- Copyright 2018 Sadkit
--
-- Corona SDK Inertia Framework
--
-- Usage: local inertia = require("inertia")
module(..., package.seeall)
-- Usage: object = object involved in rotation, time = time in millis for a 360 degree rotation (period), fps of the app
rotate = function (object, fps)
if (object.rotation >= 360) then
object.rotation = 0
end
local time = 4000 / (1000/fps)
object.rotation = object.rotation + 360/time
end
-- Usage: object = object involved in rotation, newX = target x, newY = target y, speed = speed of the movement (8 is a good value), fps of the app
moveTo = function(object, newX, newY, speed, fps)
if (object.x ~= newX) or (object.y ~= newY) then
local spaceX = math.abs(object.x - newX)
local spaceY = math.abs(object.y - newY)
local calculatedTime = (( spaceX + spaceY ) * speed) / (1000/fps)
local moveX = false
local moveY = false
if not moveX then
if object.x > newX then
object.x = object.x - spaceX/calculatedTime
if object.x <= newX then
moveX = true
end
else
object.x = object.x + spaceX/calculatedTime
if object.x >= newX then
moveX = true
end
end
end
if not moveY then
if object.y > newY then
object.y = object.y - spaceY/calculatedTime
if object.y <= newY then
moveY = true
end
else
object.y = object.y + spaceY/calculatedTime
if object.y >= newY then
moveY = true
end
end
end
if (moveY and moveX) then
object.x = newX
object.y = newY
end
end
end