Page 1 of 1

question about love.graphics.points and starfields

Posted: Tue Dec 06, 2016 11:48 am
by paul54000
main.love
(479 Bytes) Downloaded 117 times
Hi
Is it possible to make the stars scintillate randomly and move ?
If so, how to do it ?

https://love2d.org/wiki/love.graphics.points

Code: Select all

function love.load()
   local screen_width, screen_height = love.graphics.getDimensions()
   local max_stars = 100   -- how many stars we want
 
   stars = {}   -- table which will hold our stars
 
   for i=1, max_stars do   -- generate the coords of our stars
      local x = love.math.random(5, screen_width-5)   -- generate a "random" number for the x coord of this star
      local y = love.math.random(5, screen_height-5)   -- both coords are limited to the screen size, minus 5 pixels of padding
      stars[i] = {x, y}   -- stick the values into the table
   end
end
 
function love.draw()
   love.graphics.points(stars)  -- draw all stars as points
end

Re: question about love.graphics.points and starfields

Posted: Tue Dec 06, 2016 2:39 pm
by raidho36
I believe what you're looking for is particle effects, not "point" rendering mode.

Re: question about love.graphics.points and starfields

Posted: Tue Dec 06, 2016 5:48 pm
by Jimanzium
Can you be more specific in how you want them to move?

If you just want them to scroll right to left infinitely you could use something like this:

Code: Select all

local scrollSpeed = 100
local screenWidth = love.graphics.getWidth()

function love.update(dt)
	for i,v in ipairs(stars) do
		v.x = v.x - scrollSpeed * dt
		if(v.x < 0) then
			v.x = v.x + screenWidth
		end
	end
end
I'd need more information to help you more.

Re: question about love.graphics.points and starfields

Posted: Wed Dec 07, 2016 7:37 am
by paul54000
your code didn't work, could you help me please ?

Code: Select all

function love.load()
   local screen_width, screen_height = love.graphics.getDimensions()
   local max_stars = 100   -- how many stars we want
 
   stars = {}   -- table which will hold our stars
 
   for i=1, max_stars do   -- generate the coords of our stars
      local x = love.math.random(5, screen_width-5)   -- generate a "random" number for the x coord of this star
      local y = love.math.random(5, screen_height-5)   -- both coords are limited to the screen size, minus 5 pixels of padding
      stars[i] = {x, y}   -- stick the values into the table
   end
end

local scrollSpeed = 100
local screenWidth = love.graphics.getWidth()

function love.update(dt)
   for i,v in ipairs(stars) do
      v.x = v.x - scrollSpeed * dt
      if(v.x < 0) then
         v.x = v.x + screenWidth
      end
   end
end
 
function love.draw()
   love.graphics.points(stars)  -- draw all stars as points
end

Re: question about love.graphics.points and starfields

Posted: Wed Dec 07, 2016 8:28 pm
by Sulunia
Finally, this works. I assume this is what you're looking for.
Read the comments, don't just use the snippet for whatever reason. Try to learn from it! ^^

Code: Select all

--Global vars
totalDt = 0
stars = {}   -- table which will hold our stars
screen_width, screen_height = love.graphics.getDimensions()

function love.load()
 
	local max_stars = 500   -- how many stars we want
 
	for i=1, max_stars do   -- generate the coords of our stars
		local x = love.math.random(5, screen_width-5)   -- generate a "random" number for the x coord of this star
		local y = love.math.random(5, screen_height-5)   -- both coords are limited to the screen size, minus 5 pixels of padding
		local r = 255
		local g = 255 -- Set colors to white
		local b = 255
		local a = love.math.random()*254
		stars[i] = {x, y, r, g, b, a}   -- stick the values into the table
	end
end

function love.update(dt)
	totalDt = totalDt + dt
	if totalDt >= 0.05 then --Reduce this comparison value to increase scroll speed/blink speed
		screen_width, screen_height = love.graphics.getDimensions() --Gets current screen dimensions
		for i, v in ipairs(stars) do
			--Iterate over every star and assign a new alpha value for then
			--Assign a new Y value if you want
			stars[i][6] = love.math.random(60, 254)
			stars[i][2] = stars[i][2] - 1
			if stars[i][2] == 0 then --Wraps around screen
				stars[i][1] = love.math.random(5, screen_width-5) --new X
				stars[i][2] = screen_height - 5 --new Y, starting at bottom
			end
		end
		totalDt = 0 --Reset dt time count
	end
end
 
function love.draw()
	love.graphics.points(stars) --Draws all stars on the table
end