Problem with Collision

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
JHB
Prole
Posts: 30
Joined: Tue Feb 12, 2013 6:15 pm

Problem with Collision

Post by JHB »

Hi,
I have some problem with collision detecting.

Code: Select all

function love.update(dt)

joysticks = love.joystick.getNumJoysticks( )  		-- Anzahl der Joysticks zählen


i = 1
while i  <= joysticks do
	left_x = love.joystick.getAxis(i, 1 )*3  		-- X Werte des Linken Joysticks errechnen
	left_y = love.joystick.getAxis(i, 2 )*3 		 -- Y Werte des Linken Joysticks errechnen
	
        spieler[i].pos_x = spieler[i].pos_x+left_x
	spieler[i].pos_y = spieler[i].pos_y+left_y

		for c=1, (coll_count) do
		
			if spieler[i].pos_x < (tile[c].x+tile_w) and tile[c].x > (spieler[i].pos_x+spieler_width) and spieler[i].pos_y < (tile[c].y+tile_h) and tile[c].y > (spieler[i].pos_y+spieler_height) then    -- Collision Feststellen
			coll = "Fuck Yeah, Collision!!!"
			 else coll = "Damn... No Collision." end

		end
    i = i + 1
end
end
Here is an image that shows my Problem,
https://love2d.org/imgmirrur/KuN7lS4.png

i dont know what more to say, even my english is not that good.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Problem with Collision

Post by micha »

It is difficult to say, what going wrong, because the code you posted is so short. Can you please pack your game into a .love and upload it? Also please state more precisely what is going wrong. Does the game detect a collision even though it should not? Or the other way round?

One thing I noticed is that you have the collision check in a for loop (over c). Within each loop the last value of "coll" is overwritten, so in fact you only get the collision result with the very last tile: tile[coll_count]
JHB
Prole
Posts: 30
Joined: Tue Feb 12, 2013 6:15 pm

Re: Problem with Collision

Post by JHB »

"Does the game detect a collision even though it should not. " Yes, it does, but not all the time.

I Uploadetd a .love file, but you need a joystick/gamepad to run the game without error message.
Attachments
game.love
game
(2.67 KiB) Downloaded 190 times
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Problem with Collision

Post by Helvecta »

Hey JHB, I fixed up the love for you, the collision should run smoothly now. I'd go into detail about what I fixed, but I'm in a bit of a hurry, sorry! :oops:

I hope this helps!

(Oh, I changed/added a few variables in there while I was working, I hope you don't mind)
Attachments
Fixed Game.love
(4.01 KiB) Downloaded 184 times
"Bump." -CMFIend420
JHB
Prole
Posts: 30
Joined: Tue Feb 12, 2013 6:15 pm

Re: Problem with Collision

Post by JHB »

Wow Thanks for that, but now i have the problem that if the table "tile" contains more than one value, i get an error message, i have no idea why.
JHB
Prole
Posts: 30
Joined: Tue Feb 12, 2013 6:15 pm

Re: Problem with Collision

Post by JHB »

Hey, sorry for "pushing" this, but the problem is still there and i still dont get whats wrong.
Could somebody please help me with this?
Post Reply

Who is online

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