Wall collision

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
User avatar
Le_juiceBOX
Citizen
Posts: 71
Joined: Sat Mar 26, 2016 3:07 pm

Wall collision

Post by Le_juiceBOX »

is there a way to have a player "walk" into a wall and stop moving.

i have heard of the lua library bump and i dont really want to use it i want to make it on my own


but when i do this it never works out i cant figure out how to have the right side of the wall stop the player(when the player runs into the right side of the wall it teleports the player to the other side of the wall) please help me out with this.

thanks.
Attachments
WallTest.love
(613 Bytes) Downloaded 84 times
SteamLibrary-like Program For Love2D Games:
take me to the forum thread!
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Wall collision

Post by Plu »

Yeah, the subject is just called "collision detection". That term should get you plenty of tutorials and examples :)

The reason your player teleports to the same location every time is that you just set his x to be on the left side of the wall.

In order to make the player stop when he hits the wall, you need to change the logic. The order of operations is as follows:

- the player is in location A
- the player wishes to move to location B
- check for collisions at location B
- if you find any, return the player to location A

Make sure any draw calls are done after completing all these steps; so in a single call to the update method, you move the player to B, check collisions, and then move him back to A if there are any. This will make sure that your player "stops moving", because the player won't be able to see that the player moved into a wall and then moved back to his previous position.

Note that this is the simple form of collision detection. You might get some glitches with variable speed objects bumping into walls, because they'll be pushed back a bit, and then slowly crawl closer to the wall at a low speed. If you want to fix this, you'll want to calculate a separation vector for the object and the wall and not move back all the way to A, but only just far enough to no longer be touching the wall. But that's probably a second topic; after you manage to make the above work.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Wall collision

Post by ivan »

Plu's post is in the right direction, but I believe a better approach is:
- the player is in location A
- the player moves to location B
- check for collisions at location B
- if you find any, separate the player from any object already positioned in location B
You need to learn how to "separate" overlapping objects in order to produce a good collision response.

HTML5 example: http://2dengine.com/doc/tutorials/html5/index.html
Tutorial: http://2dengine.com/doc/gs_collision.html
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Wall collision

Post by Plu »

That's what I mentioned in my last paragraph, but it's a lot more complex, which is why I suggested doing it the simpler way first :)
Post Reply

Who is online

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