@anonymous/

GrandioseSharpRoutine

Lua

No description

fork
loading
Files
  • main.lua
main.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
62
63
64
65
66
67
68
69
70
71
local vecmeta = {}
local function vec(x,y)
	return setmetatable(
		{
			x = x,
			y = y},
		vecmeta)
end
function vecmeta.__tostring(a)
	return string.format('vec(%f,%f)', a.x, a.y)
end
function vecmeta.__unm(a)
	return vec(-a.x, -a.y)
end
function vecmeta.__add(a,b)
	return vec(a.x+b.x, a.y+b.y)
end
function vecmeta.__sub(a,b)
	return vec(a.x-b.x, a.y-b.y)
end
function vecmeta.__mul(a,b)
	return vec(a.x*b, a.y*b)
end
function vecmeta.__div(a,b)
	return vec(a.x/b, a.y/b)
end
local function dot(a,b)
	return a.x*b.x + a.y*b.y
end
local function left(a)
	return vec(-a.y, a.x)
end
local function project(a, nx,ny)
	return vec(dot(a,nx), dot(a,ny))
end
local function unproject(a, nx,ny)
	return nx*a.x + ny*a.y
end

local radius = 10
local velocity = 1
local baseacceleration = velocity*velocity/radius
local pos = vec(-radius, 0)
local vel = vec(0, velocity)

local posreg_int = vec(0,0)
local velreg_int = vec(0,0)

for i = 1, 30 do
	local posabs = math.sqrt(dot(pos,pos))
	local cdir = pos/posabs
	local ndir = left(cdir)
	local poscn = vec(posabs, 0)
	local velcn = project(vel, cdir,ndir)
	local poscn_target = vec(radius, 0)
	local poscn_err = poscn_target - poscn
	posreg_int = posreg_int + poscn_err
	local velcn_target = vec(0, velocity) + poscn_err * 0.4 + posreg_int * 0.0
	local velcn_err = velcn_target - velcn
	velreg_int = velreg_int + velcn_err
	local acccn = vec(baseacceleration, 0) + velcn_err * 0.4 + velreg_int * 0.0
	local acc = unproject(acccn, cdir,ndir)
	vel = vel + acc
	pos = pos + vel

	local r = math.sqrt(dot(pos,pos))
	local phi = math.atan2(pos.y, pos.x)
	-- print(r,phi)
  print(tostring(velcn))
end