Top Down Collision Detection and Reaction

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
Noacos
Prole
Posts: 5
Joined: Sun Mar 22, 2015 4:51 pm

Top Down Collision Detection and Reaction

Post by Noacos »

Hello. I'm sure this "problem" has been solved somewhere, but I just can't find it anywhere (yes, I searched with google also directly in the love2D forum). I'm pretty new to game programming and I tryed everything I could think of, but it is still not working right. In my last attempt (which I've attached) collision only works right with the top panels (which is buggy sometimes).
Attachments
TDS.love
(1.97 KiB) Downloaded 135 times
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Top Down Collision Detection and Reaction

Post by Bindie »

Hey, I would recommend bump.lua, thread: viewtopic.php?f=4&t=78729&hilit=how+to+use+bump.lua
also: viewtopic.php?f=5&t=78086&hilit=bump.lua

I tried to code collision like this as well, hard stuff.
Noacos
Prole
Posts: 5
Joined: Sun Mar 22, 2015 4:51 pm

Re: Top Down Collision Detection and Reaction

Post by Noacos »

Hmm.. I wanted to create my own collision system, but if it's really hard then I better try my luck with a library. Just to be sure, if one day I wanted to use my game for commercial use, can I still do it even if I used bump?
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: Top Down Collision Detection and Reaction

Post by Bindie »

I think so, in here: https://github.com/kikito/bump.lua I guess there is an answer, otherwise ask Kikito here on the forum.

Also don't count my word as the last, I tried making a collision system however i'm kinda of a rookie and I'm sure some user here has the advice you seek.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Top Down Collision Detection and Reaction

Post by s-ol »

Bindie wrote:I think so, in here: https://github.com/kikito/bump.lua I guess there is an answer, otherwise ask Kikito here on the forum.

Also don't count my word as the last, I tried making a collision system however i'm kinda of a rookie and I'm sure some user here has the advice you seek.
bump.lua is licensed under the MIT License, like most things LÖVE-related are.
Copyright (c) 2012 Enrique García Cota

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
So yes, you can, but you need to keep the MIT license around and enable user to see bump.lua is licensed under it.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Skeiks
Citizen
Posts: 51
Joined: Wed Jan 28, 2015 1:51 pm

Re: Top Down Collision Detection and Reaction

Post by Skeiks »

I can't look at your code right now but collision resolution for problems like this isn't that hard.

Imagine you have a function that you made, that can detect if a point is colliding with the world. This function will be called colWorld(x,y). I'll be using pseudocode.

Code: Select all

function colWorld(x,y)
   if(this point is touching the world) then 
        return true 
    end
   return false
end
Ok. From here you'll be able to do just about every type of world to object collision you'll ever need in a beginners 2D game like this. All you have to do is implement a function like this.

So from here, how do we apply this to your box game? If your box is 50x50, with the origin at the top left, this is how.

Code: Select all

function updateBox()
 if upIsPressed then
  box.y = box.y - 2
  while colWorld(box.x+3, box.y) or colWorld(box.x+47, box.y) then 
   box.y = box.y+1
  end
end
Rinse and repeat for every direction. You can do this more elegantly, but for now lets look at it like this. If up is being pressed, move the box up. If you detect a collision at the top of the box at 2 different points, move the box down by 1 pixel. We have 2 points for the 2 corners of the box. Why are the corners at (3,0) and (47,0)? Because if the box is ALSO moving to the right or left, we need a buffer for the horizontal movement. We'd need to add more points to detect if the box is wider than the smallest object in the world. But that's ok, your colWorld function should be fast enough to be called hundreds of times per frame without slowdown.

If this makes any sense let me know. I can always come back and clarify when I have an opportunity to open your love and look at your code.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Top Down Collision Detection and Reaction

Post by Jasoco »

I would definitely second the Bump recommendation. It'll save you a lot of frustration and let you just make the game and focus on the rest.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Top Down Collision Detection and Reaction

Post by ivan »

A while ago I wrote something that looks a lot like your demo.
But yeah, you probably want to go with bump or hardon collider.
Noacos
Prole
Posts: 5
Joined: Sun Mar 22, 2015 4:51 pm

Re: Top Down Collision Detection and Reaction

Post by Noacos »

Thank you all! I decided to use bump this time, but later, when I have more time I'll definetly try to code my own collision system mainly with help of ivan's tutorial.
Post Reply

Who is online

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