Browse Source

Merge pull request #1 from NiroUwU/master

Added hierarchical gravity, ability to make child objects in space
tags/v0.0.6_dev
Madiwka 2 years ago
committed by GitHub
parent
commit
0f63e34678
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 21 deletions
  1. +9
    -1
      data/planetdata.lua
  2. +10
    -5
      main.lua
  3. +56
    -3
      src/class/Planet.lua
  4. +1
    -12
      src/class/Player.lua

+ 9
- 1
data/planetdata.lua View File

@@ -3,7 +3,15 @@ planetdata = {
x = 0, y = 50000, x = 0, y = 50000,
r = 12000, m = 5e14, r = 12000, m = 5e14,
name = "Testos", name = "Testos",
colour = {42, 04, 20}
colour = {42, 04, 20},
parent = "star"
},
{
x = 30000, y = 50000,
r = 2000, m = 1e14,
name = "Deez",
colour = {50, 50, 50},
parent = 1
} }
} }



+ 10
- 5
main.lua View File

@@ -8,9 +8,13 @@ function love.load()
love.window.setTitle(info.name.." - v"..info.version) love.window.setTitle(info.name.." - v"..info.version)
width, height = love.graphics.getDimensions() width, height = love.graphics.getDimensions()
cam = Camera() cam = Camera()
zoomlevel = 1
zoomlevel = 0.005




-- Loading: -- Loading:
ships = {} --Potentially add other starships in the future?

planet = {} planet = {}
loadPlanets() loadPlanets()


@@ -33,7 +37,8 @@ function loadPlanets()
p.x, p.y, p.x, p.y,
p.r, p.m, p.r, p.m,
p.name, p.name,
p.colour
p.colour,
p.parent
) )
) )
debug(p.name.." is loaded") debug(p.name.." is loaded")
@@ -42,14 +47,13 @@ function loadPlanets()
end end


function updatePlanets() function updatePlanets()
for i=1, #planet do
planet[i]:update()
end
planet[1]:update()
end end


function drawPlanets() function drawPlanets()
for i=1, #planet do for i=1, #planet do
planet[i]:draw() planet[i]:draw()
--debug("Drawing planet " .. i)
end end
end end


@@ -106,6 +110,7 @@ function love.update(dt)
-- Camera: -- Camera:
cam:lookAt(player.x, player.y) cam:lookAt(player.x, player.y)
cameraControls() cameraControls()
--debug(player.x .. " " .. player.y)
end end


function love.draw() function love.draw()


+ 56
- 3
src/class/Planet.lua View File

@@ -1,33 +1,86 @@
Planet = Class {} Planet = Class {}


function Planet:init(tempX, tempY, tempR, tempM, tempName, tempC)
-- Planet Position
function Planet:init(tempX, tempY, tempR, tempM, tempName, tempC, tempP)
-- Planet Position:
self.x = tempX self.x = tempX
self.y = tempY self.y = tempY


-- Planet Radius and Mass
-- Speed:
self.xSpeed = 0
self.ySpeed = 0

-- Speed Change: (throttle 0 - 1; variable) (speed; constant (max speed change))
self.throttle = 0.5
self.speed = 0.05

-- Planet Radius and Mass:
self.r = tempR self.r = tempR
self.m = tempM self.m = tempM


-- Planet Data: -- Planet Data:
self.name = tempName self.name = tempName
self.colour = tempC self.colour = tempC

-- Planet Family:
self.children = {}
if (tempP ~= "star") then
self.ySpeed = 5
self.parent = planet[tempP]
table.insert(planet[tempP].children, self)
end


end end







-- FUNCTIONS -- FUNCTIONS


function Planet:updatePosition()
self.x = self.x + self.xSpeed
self.y = self.y + self.ySpeed
debug("Updating position of planet " .. self.name .. ": " .. self.x .. " " .. self.y)
end


function Planet:attract(dt) --Planet doing the attracting, divided in two parts:
--Attracting children
for i, child in ipairs(self.children) do
local grav = calc.gPull(self, child)
local dist = calc.distance(self.x, self.y, child.x, child.y)
local pull = 20/dist * grav/1e14
child.xSpeed = child.xSpeed - (child.x - self.x)*pull
child.ySpeed = child.ySpeed - (child.y - self.y)*pull
end

--Attracting the player


-- IF IN SPHERE OF INFLUENCE
local grav = calc.gPull(self, player)
local dist = calc.distance(self.x, self.y, player.x, player.y)
local pull = 20/dist * grav

player.xSpeed = player.xSpeed - (player.x - self.x)*pull
player.ySpeed = player.ySpeed - (player.y - self.y)*pull
end




-- MAIN -- MAIN


function Planet:update(dt) function Planet:update(dt)
self:attract(dt)
self:updatePosition()
for i, child in ipairs(self.children) do
child:update(dt)
end
end end



function Planet:draw() function Planet:draw()
local col = self.colour local col = self.colour
love.graphics.setColor(calc.c(col[1]), calc.c(col[2]), calc.c(col[3])) love.graphics.setColor(calc.c(col[1]), calc.c(col[2]), calc.c(col[3]))
love.graphics.circle("fill", self.x, self.y, self.r) love.graphics.circle("fill", self.x, self.y, self.r)
end end

+ 1
- 12
src/class/Player.lua View File

@@ -29,7 +29,7 @@ function Player:init(tempX, tempY)
self.landingspeed = 0 self.landingspeed = 0


-- Mass: -- Mass:
self.m = 1
self.m = 10
end end




@@ -137,17 +137,6 @@ function Player:gravity()
if self:isLanded() then if self:isLanded() then
-- Player is landed: -- Player is landed:
self.xSpeed, self.ySpeed = 0, 0 self.xSpeed, self.ySpeed = 0, 0
else
-- Player is not landed:
for i=1, #planet do
local pla = planet[i]
local grav = calc.gPull(self, pla)
local dist = calc.distance(self.x, self.y, pla.x, pla.y)
local pull = 20/dist * grav

self.xSpeed = self.xSpeed + (pla.x - self.x)*pull
self.ySpeed = self.ySpeed + (pla.y - self.y)*pull
end
end end
end end




Loading…
Cancel
Save