Collision detection in Löve?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Collision detection in Löve?

Post by Maholain »

I was looking around the documentation and couldn't find anything on hit Tests or collisions? A search found nothing since the term 'collision' seem to be too common. Can anyone help me on how to create some form of collision detection?

Thanks! :megagrin: Nice similes by the way
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Collision detection in Löve?

Post by bartbes »

You might want to check out the docs on love.physics.
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Collision detection in Löve?

Post by Maholain »

So.. is this the only way to detect collision in Löve? Also, when I used the example on collision in the docs, and tried to add in a keypressed callback and later tried to place a love.keyboard in the update(dt) function, it threw me an error.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Collision detection in Löve?

Post by bartbes »

It's not the only way, but it's the only way that you don't have to code yourself. About the error, it all depends on your code and what the specific error was.
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Collision detection in Löve?

Post by Maholain »

My error was main.lua:34: attempt to perform arithmetic on global 'x' (a nil value). I have attached the file below:


Anyways, since I don't really like using Box2d, are there any examples of programming a collision detection without using love.physics?

Thank you.
Attachments
test.love
(11.5 KiB) Downloaded 186 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision detection in Löve?

Post by Robin »

Maholain wrote:My error was main.lua:34: attempt to perform arithmetic on global 'x' (a nil value). I have attached the file below:

Code: Select all

    if love.keyboard.isDown(love.key_right) then
      x = x + (speed * dt)
   elseif love.keyboard.isDown(love.key_left) then
      x = x - (speed * dt)
   end
Setting x doesn't really work if you have no variable called "x". Maybe you wanted to use GetX() and SetX()?

EDIT: tried [GS]etX(), and those work (to a certain extend). The variable "speed" is awfully high. Something else fishy seems to be going on here as well, but I have no clue what it is.
Help us help you: attach a .love.
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Collision detection in Löve?

Post by Maholain »

Robin wrote:
Maholain wrote:My error was main.lua:34: attempt to perform arithmetic on global 'x' (a nil value). I have attached the file below:

Code: Select all

    if love.keyboard.isDown(love.key_right) then
      x = x + (speed * dt)
   elseif love.keyboard.isDown(love.key_left) then
      x = x - (speed * dt)
   end
Setting x doesn't really work if you have no variable called "x". Maybe you wanted to use GetX() and SetX()?
Ah, I keep automatically assuming x is a variable that is already present (like in Flash, etc).

Well, I have no idea to use getx or setx since the documentation has no search. Could anyone point me to that direction?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision detection in Löve?

Post by Robin »

Maholain wrote:Well, I have no idea to use getx or setx since the documentation has no search. Could anyone point me to that direction?
Here you go:
http://love2d.org/docs/Body.html
http://love2d.org/docs/Body_getX_1.html
http://love2d.org/docs/Body_setX_1.html

(btw, I don't think setting the position directly is The Right Thing to do when it comes to moving objects.)
Help us help you: attach a .love.
Maholain
Prole
Posts: 12
Joined: Tue Jun 23, 2009 4:29 am

Re: Collision detection in Löve?

Post by Maholain »

Okay, but since setX and getX isn't used for moving objects, how could I get a way to move objects in my code example (look above a few posts)? Kinda confused here. Basically, if someone could upload an example with working movement, I'd be very happy and learn loads off of it! :ultrahappy:
User avatar
Sardtok
Party member
Posts: 108
Joined: Thu Feb 21, 2008 2:37 pm
Location: Norway/Norge/諾威/挪威 (Yes, I'm teh back!)
Contact:

Re: Collision detection in Löve?

Post by Sardtok »

Have a look at the Body documentation posted above.
There are getters and setters for other physical attributes like velocity which might come in handy.
Take off every Zigg for great rapist.
Now, outgay that!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 2 guests