Page 1 of 1
how can I make an infinite starfield?
Posted: Thu Oct 24, 2013 4:35 am
by jewjitsu
Hi, I am new to the community and have been taking Sockmunkee's tutorials. At the same time I would like to get started on a small project idea that I have, it is a turn based space game where all the ships will be flying in a fleet formation(basically just in rows) and will have no movement of there own except for some small simulated space movement such as moving back and forward and once a while maneuvering side to side but not within the characters control. So basically they will always be right on the screen, in the background I would like to have an infinitely scrolling star field to simulate movement, how would I go about achieving this?
Off topic I would like to say from what I've read while looking for an answer using the search function as well as other random browsing I really like the community here and am glad to be taking my first step to become a part of it.
Re: how can I make an infinite starfield?
Posted: Thu Oct 24, 2013 6:04 am
by micha
I suggest a pseudo-infinite solution: Make a starfield of the size of the screen and wrap it around as you scroll.
First create a table of star locations randomly (in love.load):
Code: Select all
stars = {}
for i = 1,nStars do
stars[i] = {}
stars[i].x = math.random()*screenWidth
stars[i].y = math.random()*screenHeight
end
To keep track of the scrolling, create a variable called starOffset and increase it with time (in love.update)
Code: Select all
starOffset = (starOffset + dt) % screenWidth
And drawing the starfield is as follows:
Code: Select all
for i = 1,nStars do
local thisX = (stars[i].x + Offset) % screenWidth
local thisY = stars[i].y
-- either draw an image:
love.graphics.draw(starImage,thisX,thisY)
-- or a circle
love.graphics.circle(fill,thisX,thisY,radius,segments)
end
This %-operator wraps the numbers such that they are always between zero and screenWidth.
I used a couple of variables, without defining them. You have to define them first (screenWidth, screenHeight, nStars, radius, segments and starImage)
Re: how can I make an infinite starfield?
Posted: Thu Oct 24, 2013 8:55 am
by kikito
My favorite resource for rendering starfields (with nebulae etc) is
http://www.sea-of-memes.com/LetsCode10/LetsCode10.html .
However, it's 3d, not 2d. But some of the techniques here are extrapolable.
Re: how can I make an infinite starfield?
Posted: Thu Oct 24, 2013 7:15 pm
by jewjitsu
Thanks so much for the quick replies
, I used micha's approach twice to simulate different sizes of stars and I am debating a third layer if they aren't too large, I also put less of them as they get larger which helps make it look like space is vast and random. Thanks again for your help.
Re: how can I make an infinite starfield?
Posted: Thu Nov 07, 2013 3:11 pm
by Chroteus
Luckily enough, I have a starfield.lua consisting of 3 layers of stars. They all have different speeds. Note that it's a pretty old code (5 months old).
To use it, require the module itself. Afterwards call loadStars() in load function, updateStars() in update function, and drawStars() in draw function.
http://rghost.net/50005646