Colliding with spikes help

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
Murii
Party member
Posts: 216
Joined: Fri Jul 05, 2013 9:58 am
Location: Arad, Romania
Contact:

Colliding with spikes help

Post by Murii »

Hello everyone!

So i have this little piece of code that should serve as colliding with player and spikes :

Code: Select all

function spikes:update(dt)
	for i = 1 , #spikes do 
		for j = 1 , #player do
		if player[j].xPos + player[j].w >= spikes[i].x and player[j].xPos <= spikes[i].x + spikes[i].w and player[j].yPos + player[j].h >= spikes[i].y and player[j].yPos <= spikes[i].y + spikes[i].h then 
			restoreCheckPoint = true
			end
		end		
	end
end
first time i`ve tried without for loop but i succefully collide with one spike , so i thought it will a good ideea to make a foor loop to collide whit all the spikes.But its not working,do you have any ideeas?Thx!
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Colliding with spikes help

Post by substitute541 »

Can you be more specific about what's not working? Also, please post a .love file on what you are working on so we can track the problem faster.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Murii
Party member
Posts: 216
Joined: Fri Jul 05, 2013 9:58 am
Location: Arad, Romania
Contact:

Re: Colliding with spikes help

Post by Murii »

substitute541 wrote:Can you be more specific about what's not working? Also, please post a .love file on what you are working on so we can track the problem faster.
If you start the game you will see that the colliding between player and collision its not working,if you remove the for loops the collision is possible with one spike.The issue its that i want to collide whit all the spikes!
Attachments
MyGame.love
(7.64 KiB) Downloaded 80 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Colliding with spikes help

Post by micha »

It's good that you posted a love-file. The problem is not in the snipped you posted, but in the code around. If I get it correctly, then you don't have a proper list of spikes. In the spikes:draw you loop over the pixels of the level and draw the corresponding images, if the pixel has the correct color.

For the collision to work you need a table with all spikes. The level-image should only be read once in the beginning.
Here is a rough idea how to do this. When the game is started you loop over the level-image and for each spike you add an entry into a table called spikeList:

Code: Select all

function buildMap()
	spikeList = {}
	for x = 0 , world.width-1 do 
		for y = 0 , world.height-1 do 
			red,green,blue,alpha = world.img:getPixel(x,y)
			if red == 81 and green == 116 and blue == 116 then
				table.insert(spikeList,{x=x*9.2*spikes.scale, y = y*9.2*spikes.scale, w = 10, h = 10}
			end
		end
	end
end
Now you have a table. The entries of this table are tables itself, which contain coordinates and height and width.
Next you can do you collision check as you already have it.
Note that in the current version, the collision check is not even called once, because the operator #spikes return 0.
User avatar
Murii
Party member
Posts: 216
Joined: Fri Jul 05, 2013 9:58 am
Location: Arad, Romania
Contact:

Re: [Solved]Colliding with spikes help

Post by Murii »

Awesome , i successfully made it! Thx micha!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 47 guests