[Not solved] using Pixel effect with tables

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
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

[Not solved] using Pixel effect with tables

Post by bramblez »

Hello, lovers! :>
I have this awesome pixeleffect and I am trying to get it to work with bullets table, but I don't quite understand how. As you see from pixeleffect code, there are some local variables. We can see that POS is used for player position, so my question is - how do I use this effect for bullets table? so that effect would affect every bullet, that creates from this table. should I just use new pos = { bullet.x, bullet.y } ?

Code: Select all

local canvas	= love.graphics.newCanvas()

local lumens	= 1.2 --torchpower
local speed		= 100
local nTrees	= 50
local sizeTile	= 50
local sizeView	= { love.graphics.getWidth(), love.graphics.getHeight() }
local pos		= { ( sizeView[1] - sizeTile )/2, ( sizeView[2] - sizeTile)/2 }
local tree		= love.graphics.newImage( 'tree.png' )

local effect	= love.graphics.newPixelEffect
		[[
			extern number ts;//Tile size
			extern number pwr;
			extern vec2 pos;//Player position

			vec4 effect(vec4 colour,Image img,vec2 percent,vec2 pixel)

			{
			return Texel(img,percent)*(ts/length(pos-pixel)*pwr);
			}
		]]

canvas:renderTo(function()

	love.graphics.setColor(128,128,128,128)
	love.graphics.rectangle("fill",0,0,sizeView[1],sizeView[2])
	love.graphics.setColor(100,255,100,255)
	for n=1,nTrees do
		love.graphics.draw(tree,math.random()*(sizeView[1]-sizeTile),math.random()*(sizeView[2]-sizeTile),0,0.4,0.4)
	end
	
end)
function love.load()

	love.graphics.setCaption('Arrow keys move light - <w/s> changes light diameter - <a/d> fails to change intensity (only spot size)')
	math.randomseed(os.clock())
	effect:send("ts",sizeTile)
	effect:send("pwr",lumens)
	effect:send("pos",pos)
	
end
function love.draw()

	love.graphics.setPixelEffect( effect )
	love.graphics.setColor(255,255,255,255)
	love.graphics.draw(canvas)
	love.graphics.setPixelEffect()
	love.graphics.setColor(255,255,100,255)
	love.graphics.circle( "fill", pos[1], sizeView[2]-pos[2], sizeTile/2, sizeTile/2 )
	
end
function love.update(t)

	if love.keyboard.isDown 'up' then
		pos[2]=math.min(sizeView[2]-sizeTile/4,pos[2]+t*speed)	effect:send("pos",pos)
	elseif love.keyboard.isDown 'left'	then
		pos[1]=math.max(sizeTile/4,pos[1]-t*speed)	effect:send("pos",pos)
	elseif love.keyboard.isDown 'down'	then
		pos[2]=math.max(sizeTile/4,pos[2]-t*speed)	effect:send("pos",pos)
	elseif love.keyboard.isDown 'right'	then
		pos[1]=math.min(sizeView[1]-sizeTile/4,pos[1]+t*speed) effect:send("pos",pos)
	elseif love.keyboard.isDown 'w' then
		sizeTile=sizeTile+t*speed 				effect:send("ts",sizeTile)
	elseif love.keyboard.isDown 's' then
		sizeTile=sizeTile-t*speed				effect:send("ts",sizeTile)
	elseif love.keyboard.isDown 'a' then
		lumens=lumens-t*speed/10				effect:send("pwr",lumens)
	elseif love.keyboard.isDown 'd' then
		lumens=lumens+t*speed/10				effect:send("pwr",lumens)
	end

end
function love.keypressed( key )

	if key == 'escape'	then 
		love.event.quit() 	
	end
	
end
Last edited by bramblez on Sat Jun 15, 2013 7:15 am, edited 1 time in total.
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: [Not solved] using Pixel effect with tables

Post by RedHot »

I'm sorry but I don't really get what you are trying to achieve. And the fact that there is no bullets array in your code isn't helping either :)
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

Re: [Not solved] using Pixel effect with tables

Post by bramblez »

I want each bullet to be drawn with pixeleffect from my first post :) And this is a bullet table

Code: Select all

function bullets_load()

	bulletspeed = 100
	bullets = {
	x = 0,
	y = 0,
	dx = 0,
	dy = 0,
	bullr = 2
	}

end
function bullets_update(dt)

	for i,v in ipairs(bullets) do
		v.x = v.x + (v.dx * dt)
		v.y = v.y + (v.dy * dt)
		if checkCircularCollision(v.x,v.y,cx1,cy1,2,cr1) then
			print("HIT!")
		end
	end
	
end
function bullets_draw()

	love.graphics.setColor(0,0,0)
	
	if shooting == 1 then
		for i,v in ipairs(bullets) do
			love.graphics.circle("fill",v.x,v.y,2)
		end
	end
	
end
function checkCircularCollision(ax, ay, bx, by, ar, br)

    local dx = bx - ax
    local dy = by - ay
    local dist = math.sqrt(dx * dx + dy * dy)
    return dist < ar + br
	
end
User avatar
RedHot
Citizen
Posts: 87
Joined: Mon May 27, 2013 2:43 pm
Location: Poland

Re: [Not solved] using Pixel effect with tables

Post by RedHot »

I am no pro when it comes to LUA but in your case bullets is a structure/array of numbers specific for a bullet object not an array of objects.

To have some class-like functionality read : http://lua-users.org/wiki/SimpleLuaClasses
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

Re: [Not solved] using Pixel effect with tables

Post by bramblez »

I don't quite get it anyway :(
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

Re: [Not solved] using Pixel effect with tables

Post by bramblez »

that topic is just so very sad, haha :D
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], ChocolateLoxtl, Google [Bot] and 77 guests