Plus, I'm getting acceleration from who-knows-where, probably the planet but not in the direction of the planet.
It's a little hard to describe, just run the code and it'll be fairly obvious.
Controls: arrow keys
Spoiler
local function radians( x1, y1, x2, y2 )
return math.atan2( x2-x1, (y2-y1) ) --#might add 0.66 compensate for strange CC pixels
end
local function getXY( r, mag )
return mag * math.cos( r ), mag * math.sin( r )
end
local function fGrav( m1, x1, y1, m2, x2, y2 )
return (m1*m2)/math.sqrt((x2 - x1)^2 - ((y2 - y1)*0.66)^2)^2 --#might add0.66 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( "X: " .. object.x )
term.setCursorPos( 1, 3 )
term.write( "Y: " .. object.y )
end
local tControls = {}
local id = os.startTimer( 0.1 )
while true 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
local mag = fGrav( mass( 1 ) * 2, 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 - 0.66
end
if tControls[ keys.down ] then
vy = vy + 0.66
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
PS: Anyone know a good way to generate an infinite map? My attempts have been… not so great. If you feel interested in contributing, reply here or PM me.