Page 1 of 1

Wall collision

Posted: Mon Jul 25, 2016 10:14 pm
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.

Re: Wall collision

Posted: Tue Jul 26, 2016 7:26 am
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.

Re: Wall collision

Posted: Tue Jul 26, 2016 7:56 am
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

Re: Wall collision

Posted: Tue Jul 26, 2016 8:16 am
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 :)