How i put Collision in my Game?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
CloudyYard
Prole
Posts: 12
Joined: Fri Jul 03, 2015 8:54 pm

How i put Collision in my Game?

Post 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.
Attachments
green-world0.1.love
(4.95 KiB) Downloaded 139 times
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: How i put Collision in my Game?

Post 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:
Attachments
AABB.png
AABB.png (5.21 KiB) Viewed 3896 times
CloudyYard
Prole
Posts: 12
Joined: Fri Jul 03, 2015 8:54 pm

Re: How i put Collision in my Game?

Post by CloudyYard »

I do not understand !,
you could pass the adapted code in my project?

thanks.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: How i put Collision in my Game?

Post 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?
CloudyYard
Prole
Posts: 12
Joined: Fri Jul 03, 2015 8:54 pm

Re: How i put Collision in my Game?

Post by CloudyYard »

I am a beginner in Love2D, and I am a Brazilian ....
I'm using google translate and not get it right.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: How i put Collision in my Game?

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How i put Collision in my Game?

Post 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.
User avatar
gomez
Citizen
Posts: 65
Joined: Mon Feb 18, 2013 12:23 am
Location: Sao Luís, Brazil

Re: How i put Collision in my Game?

Post 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
Hey dude :D
Do you want to protect earth from an apocalypse ? Me too o/
Check my new game here: viewtopic.php?f=5&t=81001
Post Reply

Who is online

Users browsing this forum: t3hgreen and 5 guests