# How to make one-way collision

**URL:** <https://discourse.libsdl.org/t/how-to-make-one-way-collision/310>\
**Category:** Game Development\
**Created:** [February 24, 2016, 10:12pm UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310 "2016-02-24T22:12:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![samleo](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@samleo](https://discourse.libsdl.org/u/samleo)\
**Post date:** [February 24, 2016, 10:12pm UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/1 "2016-02-24T22:12:37Z")

</div>

Hi, someone know the algorithm to make one-way collisions with 2 rects? It’s the collision did like in old mario’s games and in doodle jump. I’m making a jump game and I don’t know how to make this.  
Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [February 25, 2016, 2:26pm UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/2 "2016-02-25T14:26:15Z")

</div>

Do you mean the kind of platforms (in Super Mario-games) that Mario can jump through from under the platform and then can stand on top of it?  
Platform: [http://themushroomkingdom.net/images/remakes/smb/smb\_w4-3.png](http://themushroomkingdom.net/images/remakes/smb/smb_w4-3.png)

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [February 25, 2016, 3:05pm UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/3 "2016-02-25T15:05:49Z")

</div>

Okey, bad example in my earlier post, since Mario can’t jump through those platforms.  
Correct image: [http://higherorderfun.com/blog/wp-content/uploads/2012/05/Super\_Mario\_World\_One\_Way.png](http://higherorderfun.com/blog/wp-content/uploads/2012/05/Super_Mario_World_One_Way.png)

---

<div class="post-metadata">

**Author:** ![samleo](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@samleo](https://discourse.libsdl.org/u/samleo)\
**Post date:** [February 26, 2016, 6:33pm UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/4 "2016-02-26T18:33:00Z")

</div>

[quote=“Naith”]Okey, bad example in my earlier post, since Mario can’t jump through those platforms.  
Correct image: [http://higherorderfun.com/blog/wp-content/uploads/2012/05/Super\_Mario\_World\_One\_Way.png](http://higherorderfun.com/blog/wp-content/uploads/2012/05/Super_Mario_World_One_Way.png)[/quote

It’s exactly what I need. I tried an ugly code and works, but when the player go from horizontal sides to the platform, so he is put abruptly on up side. Then, I created a new code with a current platform and solved the problem, but when the player go from horizontal sides jumping so he is put abruptly on up side.

I don’t know how to do this, if you know how to do this, please, help me.

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [March 3, 2016, 12:47am UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/5 "2016-03-03T00:47:16Z")

</div>

Sorry for late answer. I’m afraid I don’t have enough knowledge when it comes to one-way platforms but I googled some for you and found this site:  
[http://www.gamedev.net/page/resources/\_/technical/game-programming/the-guide-to-implementing-2d-platformers-r2936](http://www.gamedev.net/page/resources/_/technical/game-programming/the-guide-to-implementing-2d-platformers-r2936)  
Scroll down on the page and you’ll find a short text about one-way platforms and how to think when developing it.

As has been written on the page, you shouldn’t check for collision on the x-axis, since the tile is never an obstacle.

So what you should probably do is, whenever the player is jumping and the collision rectangle (let’s call it Player\_Rectangle) is colliding with the tile’s collision rectangle (let’s call it Tile\_Rectangle), check if the Player\_Rectangle’s lowest part is 1 pixel above the Tile\_Rectangle’s highest part, ie, check if **Player\_Rectangle.y + Player\_Rectangle.h \< Tile\_Rectangle.y**.  
If this is the case, make sure that the player is stopped when falling down (due to gravity).  
The reason you’re gonna check the above situation is that the player should be able to jump into the tile, from below, and then fall back down if the player doesn’t jump high enough to reach above the tile.

I’m not very good in explanations but I hope you’ll understand what I’m trying to say.

---

<div class="post-metadata">

**Author:** ![samleo](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@samleo](https://discourse.libsdl.org/u/samleo)\
**Post date:** [March 3, 2016, 1:30am UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/6 "2016-03-03T01:30:11Z")

</div>

Okay, thank you for the link. I made some weird code and got the right “answer”, it isn’t that I’ll show here but works “perfectly”.  
Thank you for the explanation.

---

<div class="post-metadata">

**Author:** ![maosk21](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@maosk21](https://discourse.libsdl.org/u/maosk21)\
**Post date:** [March 5, 2016, 1:41am UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/7 "2016-03-05T01:41:42Z")

</div>

I don’t really know what you mean. Though. you could allways create small rectangles to put on fixed positions of your objects. Then calculate if the rectangles overlaps each other!

---

<div class="post-metadata">

**Author:** ![samleo](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@samleo](https://discourse.libsdl.org/u/samleo)\
**Post date:** [March 7, 2016, 7:40am UTC](https://discourse.libsdl.org/t/how-to-make-one-way-collision/310/8 "2016-03-07T07:40:22Z")

</div>

maoski21, I just used two rects and a vector of rects with no collision and solved the problem, for me, an ugly solution.
