Need Help Collision Code

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
Ruffa-Duffa
Prole
Posts: 4
Joined: Fri Jul 11, 2014 9:29 am

Need Help Collision Code

Post by Ruffa-Duffa »

My collision code isn't really working so I need help please. I know there's probably some better code already made by someone on the internet but frankly, I put a lot of effort into it and I would like to finish my collision code since I'm pretty sure it's easily fixable. I could describe it but it would probably be easier for someone to just give it a shot and see what's wrong with it.

I can't seem to upload more than 3 files but i need to upload the game, and I'm new at this so I don't know how to upload it as one file you can separate so you can view the code. I'll upload the subsequent files in more posts. The collision code is all in the player file though so really unless you need to test it you'll only need that one.

Anyway, thanks for the help, really appreciate it.
Attachments
platforms.lua
(1.06 KiB) Downloaded 123 times
main.lua
(1.87 KiB) Downloaded 125 times
player.lua
(5.34 KiB) Downloaded 130 times
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Need Help Collision Code

Post by Plu »

This topic explains how to make a .love file for your game, which will allow you to upload everything in a single, easy-to-run file.

http://love2d.org/forums/viewtopic.php?f=4&t=451

It'll save you the trouble of uploading all the files on their own.
Ruffa-Duffa
Prole
Posts: 4
Joined: Fri Jul 11, 2014 9:29 am

Re: Need Help Collision Code

Post by Ruffa-Duffa »

Plu wrote:This topic explains how to make a .love file for your game, which will allow you to upload everything in a single, easy-to-run file.

http://love2d.org/forums/viewtopic.php?f=4&t=451

It'll save you the trouble of uploading all the files on their own.
Thank you for your response. I knew about making a .love file but I hadn't done it in a while, and I couldn't think of any way you would be able to access the .lua files within. Obviously you can though. I should probably warn anyone you takes a look at this though, there is some strange data I had in the corners for testing purposes, some of it will make sense, or some, like the green 3 in the corner, make no sense whatsoever. I hadn't touched this for a while until yesterday so I'm still getting caught up on where I was up to. One last thing, I just remembered I haven't tested this in 0.9.1 yet so if it fails, it should work in 0.9.0.
Thanks for the help, the .love file should be attached.
Attachments
Corgi Game.love
(70.92 KiB) Downloaded 147 times
User avatar
Jimanzium
Party member
Posts: 104
Joined: Sun Jun 03, 2012 2:39 pm
Contact:

Re: Need Help Collision Code

Post by Jimanzium »

Hello, firstly this didn't work in 0.9.1 but the only change needed was "setDefaultImageFilter" to "setDefaultFilter" on line 8 in main.lua. Secondly, I can't really help you much as you didn't really explain what was wrong with the collision detection just that it wasn't working, everything seemed to work fine for me. What part of it is broken? What do you have to do for it to not work? The only thing I could see that you could collide with was the ground and that worked fine.
Ruffa-Duffa
Prole
Posts: 4
Joined: Fri Jul 11, 2014 9:29 am

Re: Need Help Collision Code

Post by Ruffa-Duffa »

Jimanzium wrote:Hello, firstly this didn't work in 0.9.1 but the only change needed was "setDefaultImageFilter" to "setDefaultFilter" on line 8 in main.lua. Secondly, I can't really help you much as you didn't really explain what was wrong with the collision detection just that it wasn't working, everything seemed to work fine for me. What part of it is broken? What do you have to do for it to not work? The only thing I could see that you could collide with was the ground and that worked fine.
Thanks for the reply.
Maybe I put on the wrong version? That's very strange because in my current version there's the red ground but also a purple block. Colliding with the block from its side puts the corgi on top of it and not being on the block stops you from jumping. I've attached what I've tested to be the right version.
I'll keep on looking at my problems but helping should be possible now. Sorry for the mishap.
EDIT: So I did a bit of digging and it was an older version because love.graphics.setDefaultImageFilter was changed to love.graphics.setDefaultFilter in version 0.9.0. The version I uploaded earlier must have been the version I had been I was developing in 0.8.0. Also, the jumping thing is fixed by removing lines 123 through to 126 in player.lua, however that is there to prevent an issue where if you run off a platform you will not fall until you jump. Obviously my prevention strategy is pretty lousy though, because it screws up jumping on platforms that aren't the highest on the map. I'll keep looking at it, but I'd love to hear your thoughts.
Attachments
Corgi Game.love
(18.81 KiB) Downloaded 128 times
User avatar
Jimanzium
Party member
Posts: 104
Joined: Sun Jun 03, 2012 2:39 pm
Contact:

Re: Need Help Collision Code

Post by Jimanzium »

Ok, I got everything that wasn't working working by making a few small changes:

Code: Select all


if player.y + player.picHeight > v.y and player.y < v.y + v.picHeight then -- Prevents teleporting to the top of the platform
	-- stuff
					player.x = v.x - player.picWidth - 1 -- Prevents moving into stuff whilst falling
	--stuff

					player.x = v.x + v.picWidth + 1 -- Prevents moving into stuff whilst falling
	--stuff	
end

-- Lets you jump while on the ground
if love.keyboard.isDown(" ")then
	if player.yvel <= 0 then
		player.state = "jumping"
		player.yvel = 600
	end
end

if(player.state == "jumping" or player.state == "falling") then
	-- the other stuff that was above
end
I might not have listed everything I changed so I've also added the .love
Attachments
NewCorgi.love
(18.81 KiB) Downloaded 135 times
Ruffa-Duffa
Prole
Posts: 4
Joined: Fri Jul 11, 2014 9:29 am

Re: Need Help Collision Code

Post by Ruffa-Duffa »

Jimanzium wrote:Ok, I got everything that wasn't working working by making a few small changes:

Code: Select all


if player.y + player.picHeight > v.y and player.y < v.y + v.picHeight then -- Prevents teleporting to the top of the platform
	-- stuff
					player.x = v.x - player.picWidth - 1 -- Prevents moving into stuff whilst falling
	--stuff

					player.x = v.x + v.picWidth + 1 -- Prevents moving into stuff whilst falling
	--stuff	
end

-- Lets you jump while on the ground
if love.keyboard.isDown(" ")then
	if player.yvel <= 0 then
		player.state = "jumping"
		player.yvel = 600
	end
end

if(player.state == "jumping" or player.state == "falling") then
	-- the other stuff that was above
end
I might not have listed everything I changed so I've also added the .love
Okay, cool....
EXCEPT
The biggest one is jumping seems to be available whenever the y velocity is below zero. Meaning when it goes into the negatives (falling) you can jump again. Meaning you can double jump once you start falling... and then triple jump... and the quadruple jump... etc.
Secondly although it works fine there's clearly something wrong and extremely messy with my ground collision. I feel like I could get to the result through a much simpler route and far easier. What I mean is, when on the red platform, the y velocity is -1 meaning he should be falling. I suspect I put this in at some point to set off a falling. The gravity works exponentially or something like that (obviously, because that's how gravity works) so at 0 the gravity shouldn't affect anything but at -1 it should set off a chain reaction. I should probably know how my own code works and be able to figure out if it's necessary but I imagine it must be redundant to the y velocity at -1 and therefore constantly be stopping the falling as opposed to just being able to set the velocity to 0. The other problem is, when on the pink/purple block the corgi's y velocity is constantly flickering between 0 and 1 which cannot be necessary. The last thing is (and you'll have to confirm this so I know I'm not just going crazy) that if you look very closely you can see the corgi sort of jerking backwards and forwards a bit as he jumps. It might only be if you're not on the right side of the block but that could be wrong. I added in a fps counter so I could make sure it wasn't the computer and the fps counter maintains a constant 60fps, only very occasionally dropping into 59fps. I won't re-upload the .love file with that in it because it's only one line of code so if you need to see it shouldn't be hard to add that in. If you don't know how then just look here:
http://love2d.org/wiki/love.timer.getFPS

ON THE BRIGHT SIDE the wall collision works completely fine. After this I'll have to add in roof collision and maybe edit the hitbox (that's what you call it right?) to make more sense and it might not end up being a hitbox but more like a hitirregularshape.

Thanks for all the help and if you don't want to help anymore just say so, because I don't want to force you to make my own game for me and I kind of feel a bit pathetic at this point.
User avatar
Jimanzium
Party member
Posts: 104
Joined: Sun Jun 03, 2012 2:39 pm
Contact:

Re: Need Help Collision Code

Post by Jimanzium »

I woke in my sleep when I realised this. I did spend a bit of time on it to no avail, your collision is a bit weird and I didn't look at everything you were doing. I also noticed the odd behaviour, the player jittering could be down to using oldx and x but I'm not sure.

Anyway, no problem, I might look into it again.
Post Reply

Who is online

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