Page 1 of 1

How i put Collision in my Game?

Posted: Fri Jul 03, 2015 9:06 pm
by CloudyYard
Hello everyone,
well I'm creating a RPG game in Love2D 0.9.2 but I have no idea how to create a collision in the game, for example: When you touch the wall and do not pass it to the wall. If you help me I am very grateful!

if you want to modify my game adding the collision and let the file for me to download.

Re: How i put Collision in my Game?

Posted: Sat Jul 04, 2015 12:00 am
by Cookie10monster
First off, this should go in support and development, but that's beside the point. I would recommend using either the bump library, which I haven't used but have heard good things about, or using AABB collision detection. AABB collision detection is fairly simple for rectangles and circles. For rectangles, you need the location of the X and Y of the top left and bottom right of both rectangles. Here is the code behind it

Code: Select all

local RightCollision = false 
local LeftCollision = false 
local DownCollision = false 
local UpCollision = false 
local Box1X = Number--X of top left of box #1 
local Box1Y = Number--Y of top left of box #1 
local Box1XX = Number--X of bottom right of box#1 
local Box1YY = Number--Y of bottom right of box #1 

local Box2X = Number--X of top left of box #2 
local Box2Y = Number--Y or top left of box #2 
local Box2XX = Number--X of bottom right corner of box #2 
local Box2YY = Number--Y of bottom right corner of box #2 



if Box1XX>=Box2X and Box1Y<=Box2YY and Box1YY>Box2Y then
collisionRight = false 
end 
if Box1X<=Box2XX and Box1Y<=Box2YY and Box1YY>Box2Y then 
collisionLeft = false 
end 
if Box1YY>=Box2Y and Box1XX>Box2X and Box1X<Box2XX then 
collisionDown = true 
end 
if Box1Y<=Box2YY and Box1XX>Box2X and Box1X<Box2XX then 
collisionUp = true 
end 

I think this should work, but I might have messed up XD
Anyone feel free to correct me

Here is a picture explaining where the variables are(the rectangles can be any size)
Good Luck :awesome:

Re: How i put Collision in my Game?

Posted: Sat Jul 04, 2015 12:21 am
by CloudyYard
I do not understand !,
you could pass the adapted code in my project?

thanks.

Re: How i put Collision in my Game?

Posted: Sat Jul 04, 2015 10:09 am
by Jeeper
CloudyYard wrote:I do not understand !,
you could pass the adapted code in my project?

thanks.
No one will want to sit and code the game for you...

You got an excellent explanation from Cookie10monster, if you don't want to manually add your own AABB collision then check out a library as mentioned or even the Löve built in physics (based on box2d).

If you do not understand, then you will just have to learn. Maybe some basic geometry?

Re: How i put Collision in my Game?

Posted: Sat Jul 04, 2015 8:14 pm
by CloudyYard
I am a beginner in Love2D, and I am a Brazilian ....
I'm using google translate and not get it right.

Re: How i put Collision in my Game?

Posted: Sun Jul 05, 2015 6:11 am
by Positive07
That answer is super complete, if you still dont get it, Google is your friend, search AABB collisions it is rather simple stuff

Re: How i put Collision in my Game?

Posted: Sun Jul 05, 2015 8:05 am
by T-Bone
Löve is more of a game framework than a game engine. It does not come with "batteries included" so to speak. Löve is designed to allow the game developer maximum freedom, by not forcing the developer to make their game in a specific way. For a beginner, it might be easier to use a game engine with more functionality built-in and easier to use.

Re: How i put Collision in my Game?

Posted: Sun Jul 05, 2015 1:32 pm
by gomez
with few seconds on google, i've searched something for you:

http://www.dcc.ufrj.br/~abdalla/tut_arq/colisao.htm -> AABB Algorithm is the 3rd. (in portuguese)
http://www.gamefromscratch.com/post/201 ... g-box.aspx
https://developer.mozilla.org/en-US/doc ... _detection

If you don't get it, maybe you need to learn some math for game development( basically it's just geometry):
http://www.fabricadejogos.net/posts/tut ... ordenadas/ (in portuguese)

This blog has a serie about math for game development.

Btw, im brazilian too..

Good luck with your game :D