collision detection.. [SOLVED]

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
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

collision detection.. [SOLVED]

Post by GijsB »

I've got a it all working, but there is one problem : The guy gets stuck in the yellow box, even when im sure that shouldn't happen, because i do it like :

1)calculate new posistion
2)check collision with new position
3)if no collision change actual position to new position
Attachments
main.love
(698 Bytes) Downloaded 95 times
Last edited by GijsB on Tue Oct 25, 2011 8:12 pm, edited 1 time in total.
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: collision detection..

Post by adnzzzzZ »

I'm no expert, but you are only updating the guy.position with newpos when collision returns false, this means that when a collision is happening the guy's position won't be updated.

So changing:

Code: Select all

if col() then guy.velocity = 0
else guy.position = newpos end
to

Code: Select all

guy.position = newpos 
if col() then guy.velocity = 0 end
should do the trick. But this leads other problems if you wanted a more robust collision detection/response system...
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: collision detection..

Post by GijsB »

that doesnt solve the problem at all.. you now can just walk into it
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: collision detection..

Post by adnzzzzZ »

Well, yes. You are only setting velocity.y to 0. So when your collision hits it from the sides it will just go through with velocity.y = 0. If you want it not to go through then you'll need to have velocity.x set to 0 when your collision happens mainly from left or right of the coin.

Something like this:

Code: Select all

guy.position = newpos
if col() then
        guy.velocity.x = 0 
        guy.velocity.y = 0
end
will work, but then you'll sort of get stuck when you hit the coin. What you need to do now is find a way to make your player be unstuck. This can be done by finding the minimum amount it has to move in order to not collide with the coin anymore...

Assuming you are only hitting from the bottom of the coin:

Code: Select all

guy.position = newpos
if col() then--collision with box
        guy.velocity.x = 0 
        guy.velocity.y = 0
        guy.position.y = guy.position.y - 1
end
Now you're not getting stuck anymore, but only when you hit from the bottom. All you have to do is find a way to do this for the other sides too... And of course handle the resetting of velocities accordingly: when you hit from the sides you don't want velocity.y to be set to 0 and when you hit from bottom or top you don't want velocity.x to be set to 0.

This is the way I do it at least... But I'm sure there are better/simpler ways to achieve what you want.
Last edited by adnzzzzZ on Tue Oct 25, 2011 2:46 pm, edited 1 time in total.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: collision detection..

Post by GijsB »

it kinda works now, thanks :3.

but i want to keep doing it like this D: =

1)calculate new posistion
2)check collision with new position
3)if no collision change actual position to new position
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: collision detection..

Post by kraftman »

col() is checking the current position not the predicted position.

it should be

Code: Select all

	return newpos.x + as.x > bp.x and bp.x + bs.x > newpos.x and newpos.y + as.y > bp.y and bp.y + bs.y > newpos.y
	

by the looks of it.

Just as a note, try and find a nice medium between brevity and clarity:

having two variables called "ap" and "as" may seem like less typing and so easier, but its much less clear than having a single variable containing all of the relevant variables for that object, eg just

Code: Select all

guy = {
			x = 0,
			y = 0,
			width = 20,
			height = 40,}
Also, don't worry too much about optimisation while you are developing a project, as sometimes you can shoot yourself in the foot. (for example the speed increase from creating local variable inside a function vs the cost of having to create those local variables every frame)
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: collision detection..

Post by GijsB »

thanks kraftman :D
oh and anyone who wants it, its a coin box that ONLY gets activated when touched from the bottom :
Attachments
jump.love
(816 Bytes) Downloaded 84 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests