"Questions that don't deserve their own thread" thread

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.
Locked
User avatar
unixfreak
Citizen
Posts: 82
Joined: Thu Oct 15, 2015 6:25 am
Location: Bristol, UK
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by unixfreak »

Hi! I'm trying to loop over a bunch of files in a directory to load as textures. However, some of the files are not images, and should be ignored
Hence i get this:

Code: Select all

Could not decode file 'textures/LICENSE.txt' to ImageData: unsupported file format
How can i test if a file is an image before loading it with love.graphics?

Here's a snippet;

Code: Select all

textures = {}
local i = 1
for _,t in ipairs(love.filesystem.getDirectoryItems("textures/")) do
	textures[i] = love.graphics.newImage("textures/"..t)
	i = i +1
end
I have looked at the :type() function for loaded objects, but obviously cannot test if it returns "Image" because it will throw an error before anything is even stored in the table.
Zireael
Party member
Posts: 139
Joined: Fri Sep 02, 2016 10:52 am

Re: "Questions that don't deserve their own thread" thread

Post by Zireael »

According to the wiki, love.filesystem.getDirectoryItems() returns a table.
So loop over the returned table like this

Code: Select all

function blah()
textures = {}
local i = 1
local files = love.filesystem.getDirectoryItems("textures/")
	for i,file in ipairs(files) do
		--file is a string containing the name of the file including extension
		--so we are checking if the string DOES NOT contains .txt (the extension)
		if not file:find(".txt") do
		   textures[i] = love.graphics.newImage("textures/"..t)
		   i = i +1
		end
	end
end
User avatar
Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

Re: "Questions that don't deserve their own thread" thread

Post by Jack Dandy »

Hey, I need a tip.
Up until now, when I wanted to move an object, it was in orthogonal directions and was as such relatively simple.
But now I want to move objects freely from any set of coordinates to another.

Let's say I want to move an object from point (x1,y1) to point (x2,y2), at a constant speed.

What kinda math mumbo-jumbo is required for that? (And better yet, is there a tutorial somewhere that specifices these sort of things? Explains the logic and such?)
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: "Questions that don't deserve their own thread" thread

Post by Tjakka5 »

Have a look at lerping:
https://en.wikipedia.org/wiki/Linear_interpolation

Or, if you want more advanced functionality, a tweening library such as 'flux':
viewtopic.php?t=77904
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: "Questions that don't deserve their own thread" thread

Post by pgimeno »

First, get the direction vector, then normalize it to get a vector of length 1 that points in the given direction, then advance x1, y1 in that direction with the given speed.

Code: Select all

-- First, get the direction vector:
local vx, vy = x2-x1, y2-y1
-- Then normalize it:
local mag = math.sqrt(vx^2+vy^2)
if mag ~= 0 then
  vx, vy = vx/mag, vy/mag
end
-- Then advance x1, y1 in that direction with the given speed:
if mag ~= 0 then
  x1 = x1 + vx * speed * dt
  y1 = y1 + vy * speed * dt
end
'speed' should be in units/s (pixels/s if your x1,y1 are in pixels), assuming dt is in seconds.

In another thread I've seen an example using atan2 and sin/cos, but I feel that's overkill.

Properly speaking, the 'if' can cover both sections, but my intention was to separate the steps clearly.

Edit: Here's a fiddle: http://lovefiddle.com/bcFPLasMQpMEpvuCc
User avatar
Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

Re: "Questions that don't deserve their own thread" thread

Post by Jack Dandy »

Thank you both. I will look into these things.
MartinEden
Prole
Posts: 1
Joined: Tue Oct 04, 2016 10:23 am

Re: "Questions that don't deserve their own thread" thread

Post by MartinEden »

Hi guys!

I'm studying love and wish to add to wiki a code snippet which tracks calls to event handlers. But do not see appropriate place to add it.

https://love2d.org/wiki/Category:Callbacks looks like a bad idea for tutorial code.
User avatar
evgiz
Citizen
Posts: 83
Joined: Mon Aug 29, 2016 11:05 pm
Contact:

Re: "Questions that don't deserve their own thread" thread

Post by evgiz »

pgimeno wrote:In another thread I've seen an example using atan2 and sin/cos, but I feel that's overkill.
I've never seen your method before, is it a lot more efficient than using atan2 and cos+sin? I feel trig is way more obvious than your method, i wouldnt have understood it at first if I didn't know what you were talking about.
Computer science student and part time game dev! Currently working on Depths of Limbo! :cool:

Check out the game website DepthsOfLimbo.com! :ultrahappy:
And my personal website with all my projects evgiz.net! :megagrin:
User avatar
Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

Re: "Questions that don't deserve their own thread" thread

Post by Jack Dandy »

I have another question for now, related to synchronization.

Is the only thing that instantiates new threads the "love.thread.newThread " command?

I'm asking this because of the following problem:
I think there are cases in which a certain function A is called before another function B is finished (The call isn't made from within B, but by another thing), and that it messes up some variables.

Should I worry about synchronization if I'm not using love.threads?

EDIT: To be more specific- let's say I have these 2 functions.

Code: Select all

function gamefuncs.location_update (x, y, player)
  --require("mobdebug").on()
  map_vacant[player.grid_y_sqr][player.grid_x_sqr] = 0
  
  player.grid_x = x - (x % 32)
  player.grid_x_sqr = player.grid_x/32
  player.grid_y = y - (y % 32)
  player.grid_y_sqr = player.grid_y/32
  player.tile_x, player.tile_y = 1+(math.floor (player.grid_x_sqr/4)),1+(math.floor (player.grid_y_sqr/4))
  
  map_vacant[player.grid_y_sqr][player.grid_x_sqr] = 'ent'
  
end

Code: Select all

function gamefuncs.Check_EnemyDeath() 
 
   ---[[
  for i, ent in pairs(Enemies) do
    if Enemies[i].HP <= 0 then
      map_vacant[Enemies[i].grid_y_sqr][Enemies[i].grid_x_sqr] = 0
      Enemies[i] = nil
    end
    
  end
  --]]
  gamefuncs.generateVisible()
  
end
Is it possible for the first one to be called, and then have the second called as well before the first finishes?
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: "Questions that don't deserve their own thread" thread

Post by pgimeno »

evgiz wrote:I've never seen your method before, is it a lot more efficient than using atan2 and cos+sin? I feel trig is way more obvious than your method, i wouldnt have understood it at first if I didn't know what you were talking about.
It's not a lot more efficient because these things are usually peanuts in comparison with the rest of the program, but it's one function call vs. three. Also, the square root used to take less cycles than trig functions; I haven't looked into the current status of things in more modern CPUs, though.

Inherently, the method should have a bit more precision this way, especially when not going right, because math.sin(math.pi) is not exactly zero due to rounding errors. That's also peanuts and it would be unnoticeable in most applications.

I made the mistake of calling the variable 'mag' (for magnitude) instead of 'distance', which should have made the code more obvious: scaling the segment between the two points by vel / distance makes the segment's length be exactly 'vel'.
Locked

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 7 guests