how can I make an infinite starfield?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jewjitsu
Prole
Posts: 2
Joined: Thu Oct 24, 2013 4:08 am

how can I make an infinite starfield?

Post 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. :awesome:
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: how can I make an infinite starfield?

Post 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)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: how can I make an infinite starfield?

Post 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.
When I write def I mean function.
jewjitsu
Prole
Posts: 2
Joined: Thu Oct 24, 2013 4:08 am

Re: how can I make an infinite starfield?

Post by jewjitsu »

Thanks so much for the quick replies :D, 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.
User avatar
Chroteus
Citizen
Posts: 89
Joined: Wed Mar 20, 2013 7:30 pm

Re: how can I make an infinite starfield?

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 4 guests