local function radians( x1, y1, x2, y2 )
return math.atan2( x2-x1, (y2-y1) ) --0.66 (maybe) compensate for strange CC pixels
end
local function getXY( r, mag )
return mag * math.sin( r ), mag * -math.cos( r )
end
local function fGrav( m1, x1, y1, m2, x2, y2 )
return (m1*m2)/((x2 - x1)^2 - (y2 - y1)^2) --0.66 (maybe) compensate for strange CC pixels
end
local function mass( r )
return math.pi * r^2
end
local maxx, maxy = term.getSize()
local gravPoints = {}
gravPoints[ #gravPoints + 1 ] = { x = maxx, y = math.random( 1, maxy ), magnitude = math.random( 2, 4 ) }
--Thanks to TheOriginalBIT for this circle code
local function drawCircle( centerX, centerY, radius, col, solid )
solid = solid or false
local isAdvanced = term.isColor and term.isColor()
local char = isAdvanced and " " or "|"
if isAdvanced then term.setBackgroundColor( col ) end
local radStep = 1/(1.5*radius)
for angle = 1, math.pi+radStep, radStep do
local pX = math.cos( angle ) * radius * 1.5
local pY = math.sin( angle ) * radius
if solid then
local chord = 2*math.abs(pX)
term.setCursorPos( centerX - chord/2, centerY - pY )
write( char:rep( chord ) )
term.setCursorPos( centerX - chord/2, centerY + pY )
write( char:rep( chord ) )
else
for i=-1,1,2 do
for j=-1,1,2 do
term.setCursorPos( centerX + i*pX, centerY + j*pY )
write( char )
end
end
end
end
end
local object = { x = 1, y = 1, mag = 0, angle = 0 }
local function draw()
term.setBackgroundColor( colors.lightGray )
term.clear()
--draw gravity points
for _, point in pairs( gravPoints ) do
drawCircle( point.x - object.x, point.y - object.y, point.magnitude, colors.gray, true )
end
--draw player
term.setBackgroundColor( colors.green )
term.setCursorPos( maxx / 2, maxy / 2 )
term.write( " " )
term.setBackgroundColor( colors.lightGray )
term.setTextColor( colors.black )
term.setCursorPos( 1, 1 )
term.write( "Speed: " .. tostring(object.mag):match( "%d+%.%d?%d?" ) )
term.setCursorPos( 1, 2 )
term.write( "Angle: " .. tostring(object.angle * 180/math.pi):match( "%d+%.%d?%d?" ) )
term.setCursorPos( 1, 3 )
term.write( "X: " .. tostring( object.x ):match( "%d+%.%d?%d?" ) )
term.setCursorPos( 1, 4 )
term.write( "Y: " .. tostring( object.y ):match( "%d+%.%d?%d?" ) )
end
local tControls = {}
local bRunning = true
local id = os.startTimer( 0.1 )
while bRunning do
local event = { os.pullEvent() }
if event[ 1 ] == "timer" and event[ 2 ] == id then
--calc movement on object
local vx, vy = getXY( object.angle, object.mag )
id = os.startTimer( 0.1 )
for _, point in pairs( gravPoints ) do
if math.sqrt( (point.x - object.x)^2 - ((point.y - object.y)*0.66)^2 ) <= object.mag then
bRunning = false
end
local mag = fGrav( mass( 1 ), object.x, object.y, mass( point.magnitude / 2 ), point.x, point.y )
local angle = radians( object.x, object.y, point.x, point.y )
local x, y = getXY( angle, mag )
vx = vx + x
vy = vy + y
end
--add player forces
if tControls[ keys.up ] then
vy = vy - 1
end
if tControls[ keys.down ] then
vy = vy + 1
end
if tControls[ keys.right ] then
vx = vx + 1
end
if tControls[ keys.left ] then
vx = vx - 1
end
tControls = {}
object.x = object.x + vx
object.y = object.y + vy
object.angle = math.atan2( vx, vy )
object.mag = math.sqrt(vx^2 + vy^2)
draw()
elseif event[ 1 ] == "key" then
tControls[ event[ 2 ] ] = true
end
end