Lines and 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
pr0sh4wn123
Prole
Posts: 4
Joined: Sun May 18, 2014 12:15 pm

Lines and Tables?

Post by pr0sh4wn123 »

Hello~ I am a beginner to love2d and lua in general. I am trying to make it so the user will be able to click with the left mouse button to create and drag a line to make their own custom line. I have this working fine (sort of) but I was wondering how would I make it so the user could do this with multiple lines. Like maybe finalizing the position by pressing space bar and creating a new line from where you left off to create a polygon! or maybe there is an easier way? Any help would be greatly appreciated and because I am new I was wondering if I could add anyone to Skype or somewhere so I can bounce ideas off them and ask for help? Okie thanks! here is my code so far:

Code: Select all

function love.load()
	mouse = {}
	love.graphics.setBackgroundColor(255, 255, 255)
	mouse.x, mouse.y = 0
	x, y = 600, 375
	x2, y2 = 600, 375
end

function love.draw()
	love.graphics.setColor(0, 0, 0)
	love.graphics.line(x, y, x2, y2)
end

function love.update(dt)
	mouse.x, mouse.y = love.mouse.getPosition()
	if love.mouse.isDown('l') then
		x = mouse.x
		y = mouse.y
	end
	if love.mouse.isDown('r') then
		x2 = mouse.x
		y2 = mouse.y
	end
end
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Lines and Tables?

Post by Zilarrezko »

This is a pretty simple fix, although there are several routes you can take.

My skype is zilarrezko_natvyr and I can answer some simple questions to the intermediate. However I'm pretty busy right now so I'll try and make this quick!

I made this actually earlier, but this is just a concept and kinda sucks, I would suggest finding a way to not use var and not use x2's and y2's and the like. but here's my code, pretty crappy. But one of the smarter guys will swing by and probably help you sooo much better.

Code: Select all

function love.load()
	line = {
	}
        var1 = false
end

function love.draw()
	if var1 then
		love.graphics.setColor(255,255,255,255)
		love.graphics.setLineWidth(10)
		love.graphics.line(varx, vary, mousex, mousey)
	end
	for k, v in ipairs(line) do
		love.graphics.setColor(255,255,255,255)
		love.graphics.setLineWidth(10)
		love.graphics.line(v.x, v.y, v.x2, v.y2)
	end
end

function love.update(dt)
	mousex, mousey = love.mouse.getPosition()
end

function love.mousepressed(x, y, b)
	if b == "l" then
		var1 = true
		varx, vary = x, y
	end
end

function love.mousereleased(x, y, b)
	if b == "l" then
		var1 = false
		table.insert(line, {
			x = varx,
			y = vary,
			x2 = x,
			y2 = y
		})
	end
end
I'll be available in about two days, because the next couple of days I'm graduating and quite busy. Have fun with LÖVE!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lines and Tables?

Post by Roland_Yonaba »

Hi,

One thing interesting with love.graphics.line is it can takes either a vararg (variable number of arguments) and deduces couple of coordinates from it for drawing, or a table of coordinates positions, in array form.

So basically, if you want the user to draw a line linking all vertices, point by point, i would suggest implementing it as-is: create an empty table, and make it so that anytime the user clicks, it appends to this table the coordinates where the mouse was clicked.
Then in your draw loop, you will just have to pass that very table to love.graphics.line.

Code: Select all

function love.load()
  vertices = {}
end

function love.draw()
  love.graphics.line(vertices ) -- draws the line linking all vertices
 
  -- draws a line from the last vertex to the actual mouse position
  local mx, my = love.mouse.getPosition()
  love.graphics.line(vertices [#vertices -2],vertices [#vertices -1], mx, my)
end

-- callback called anytime the user presses a mouse button
function love.mousepressed(x, y, button)
  if button == 'l' then  -- if left-mouse click
    table.insert(vertices , x) 
    table.insert(vertices , y)
  end
end
Hope this helps.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Lines and Tables?

Post by HugoBDesigner »

I think what you want is this:

Code: Select all

function love.load()
	current = {}
	lines = {}
	pressing = false
	love.graphics.setLineWidth(3)
end

function love.update(dt)
	if pressing then
		current[3] = love.mouse.getX()
		current[4] = love.mouse.getY()
	end
end

function love.draw()
	love.graphics.setColor(255, 255, 255, 255)
	for i, v in pairs(lines) do
		love.graphics.line(unpack(v))
	end
	love.graphics.setColor(205, 205, 255, 255)
	if pressing then
		love.graphics.line(unpack(current))
	end
end

function love.mousepressed(x, y, b)
	if b == "l" then
		pressing = true
		current[1] = x
		current[2] = y
	end
end

function love.mousereleased(x, y, b)
	if b == "l" then
		pressing = false
		table.insert(lines, table.copy(current))
		current = {}
	end
end

function table.copy(t)
	local r = {}
	for i, v in pairs(t) do
		r[i] = v
	end
	return r
end
This method allows multiple lines...
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
pr0sh4wn123
Prole
Posts: 4
Joined: Sun May 18, 2014 12:15 pm

Re: Lines and Tables?

Post by pr0sh4wn123 »

Thank you for all the replies, but sadly I am getting nowhere, because I can not understand some parts of the code, for example this stuff:

Code: Select all

for k, v in ipairs(line) do
      love.graphics.setColor(255,255,255,255)
      love.graphics.setLineWidth(10)
      love.graphics.line(v.x, v.y, v.x2, v.y2)
   end
or this stuff:
(mainly the #s ??? I don't have a clue what you are doing there)

Code: Select all

love.graphics.line(vertices [#vertices -2],vertices [#vertices -1], mx, my)
and even this:

Code: Select all

function table.copy(t)
   local r = {}
   for i, v in pairs(t) do
      r[i] = v
   end
   return r
end
This is completely over my head, and I am sure I need a tutorial, but like I said I am a beginner. I am not sure how this works 100% yet.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Lines and Tables?

Post by davisdude »

What do you not understand?
As for the "#s" that gets the length, or the number of items in a table/array (not counting nil values).
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
pr0sh4wn123
Prole
Posts: 4
Joined: Sun May 18, 2014 12:15 pm

Re: Lines and Tables?

Post by pr0sh4wn123 »

OH Ok I see, the number signs make sense, but now the only part I am confused on is doing the "for" function. Are there any proper/good tutorials for them? Or do you think you could explain them? Sorry for all the hassle >.<
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Lines and Tables?

Post by Karai17 »

There are two types of for loops in Lua: One is a foreach loop, the other is a normal for loop.

In a normal for loop, you pick a starting point and an end point and the loop will add or subtract 1 each time until the limit is reached:

Code: Select all

for index=1, 100 do
    print(index) -- 1, 2, 3...98, 99, 100
end
In the foreach loop, we iterate through a table and pull out all the data within it, one row at a time. ipairs is used for indexed tables (there the keys are integers starting from 1), pairs can be used for both indexed or associated tables (the keys can be of any format)

Code: Select all

for key, value in ipairs(indexed_table) do
    print(key, value) -- 1, some data; 2, more data; 3, even more
end

for key, value in pairs(associated_table) do
    print(key, value) -- x, 123; name, Billy; y, 456; 11, wurds
end

STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Lines and Tables?

Post by Roland_Yonaba »

pr0sh4wn123 wrote: This is completely over my head, and I am sure I need a tutorial, but like I said I am a beginner. I am not sure how this works 100% yet.
So maybe you should concentrate your efforts first to get comfortable with Lua's syntax ?
You can use this quick tutorial, this "cheat-sheet" (direct pdf link), BlackBullet's Lua for Programmers series, and best PiL (you do not even need to read everything in it). :crazy:
It will help getting comfortable with the language first. And it won't be that long, since Lua is a little language and the syntax is easy to grasp.
User avatar
pr0sh4wn123
Prole
Posts: 4
Joined: Sun May 18, 2014 12:15 pm

Re: Lines and Tables?

Post by pr0sh4wn123 »

Well I rather like learning while doing a project, so by doing this line project I was hoping to learn more lua, but thanks everyone for all the help, with all this I am sure I can complete what I wanted to do~!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests