How to make your character collide with terrain

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
n1ghtk1n9
Prole
Posts: 10
Joined: Sat Dec 22, 2012 12:23 am

How to make your character collide with terrain

Post by n1ghtk1n9 »

How can I make my character not collide with the terrain? This is probably a noob question, but I started Love just yesterday.
Also how can I make an image not able to pass through. To elaborate, if I had a tree image in the middle of my screen, how can I make it so if I move my character/player up to that image, it couldn't/wouldn't pass through it?
Links to Code
main: http://pastebin.com/GQ8M8bJ7
world: http://pastebin.com/kEKkEdav
player: http://pastebin.com/7ckikz9x

Sorry if its hard to understand, I'm horrible at explaining things.
User avatar
Spec0pAssassin
Prole
Posts: 8
Joined: Sun Jul 01, 2012 6:47 pm
Location: My head, USA

Re: How to make your character collide with terrain

Post by Spec0pAssassin »

If you could, could you post a .love of what you're working on. A quote from the rules on posting
Give us a .love of your game. This allows us to find the error, see it in context and possibly try out some candidate solutions.
But I get what you're asking, you want your player to collide with the tree and not pass through right? As if you were to walk into a tree in real life?
-Spec0pAssassin
Wait. What?
n1ghtk1n9
Prole
Posts: 10
Joined: Sat Dec 22, 2012 12:23 am

Re: How to make your character collide with terrain

Post by n1ghtk1n9 »

Spec0pAssassin wrote:If you could, could you post a .love of what you're working on. A quote from the rules on posting
Give us a .love of your game. This allows us to find the error, see it in context and possibly try out some candidate solutions.
But I get what you're asking, you want your player to collide with the tree and not pass through right? As if you were to walk into a tree in real life?
I was going to provide a .love, but it would be quite useless, since it's quite self-explanatory what happens(the guy passes through the tree)
mathacka
Prole
Posts: 8
Joined: Tue Sep 25, 2012 7:45 pm

Re: How to make your character collide with terrain

Post by mathacka »

You need a collision function -- something that will compare two rectangles and see if they intersect, here's some pseudocode:

Code: Select all

Function Collision(x0,y0,w0, h0, x2, y2, w2, h2)
     If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
     If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False 
     Return true
End
Post Reply

Who is online

Users browsing this forum: Bing [Bot], YaCy [Bot] and 4 guests