# Collision 2D Game

**URL:** <https://discourse.libcinder.org/t/collision-2d-game/1421>\
**Category:** Using Cinder\
**Created:** [February 5, 2019, 11:58am UTC](https://discourse.libcinder.org/t/collision-2d-game/1421 "2019-02-05T11:58:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theo](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Theo](https://discourse.libcinder.org/u/Theo)\
**Post date:** [February 5, 2019, 11:58am UTC](https://discourse.libcinder.org/t/collision-2d-game/1421/1 "2019-02-05T11:58:50Z")

</div>

hi guys, it’s me again with my 2D game.  
I have to implement the collisions, and I wanted to ask you if Cinder implemented the collisions between 2 TextureRef because I thought to check with each movement all the TextureRef present in my window to know if it was in collision with my character, is it the right way to do?  
thanks

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [February 5, 2019, 1:55pm UTC](https://discourse.libcinder.org/t/collision-2d-game/1421/2 "2019-02-05T13:55:16Z")

</div>

Collision detection and response should be completely separate from your rendering, i.e your `gl::TextureRef`s only display what your collision system already determined your entities’ locations to be. If it’s a 2D game, would you classify it as being tile based? If so, [here’s](https://jonathanwhiting.com/tutorial/collision/) a couple of [tutorials](http://www.metanetsoftware.com/technique/tutorialB.html) you can look at that will hopefully get you started.

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [February 5, 2019, 9:00pm UTC](https://discourse.libcinder.org/t/collision-2d-game/1421/3 "2019-02-05T21:00:48Z")

</div>

It would not perform very well to check 2 textures pixel by pixel whether they collide. It’s better to use a simplified version of your character to check collisions. The simplest I can think of is using the rectangle, aligned with the x and y axes (so the texture has not been rotated).

Cinder has a [Rectf](https://github.com/rarevolume/Cinder/blob/master/include/cinder/Rect.h) class that can help you out a bit. You can obtain the bounds of the texture like this:

```auto
auto bounds = mTexture->getBounds(); // returns Rectf( 0, 0, width, height );

```

You should then apply the transformations of your character (scale, position and _anchor_, the latter being the point on the texture you use as your origin) to the bounds to calculate its bounds in the window:

```auto
// Combine all transformation into a single matrix:
auto transform = glm::translate( mPosition ) * glm::scale( mScale ) 
                     * glm::translate( -mAnchor );
// Apply it to the bounds:
bounds.transform( transform );

```

Do that for the other character as well. And then you can do this:

```auto
if( bounds.intersects( enemy ) )
    app::console() << "I'm hit!" << std::endl;

```

Apart from `Rectf`, Cinder also has a `Circle` class that can help you out. For 3D games, we have `AxisAlignedBox` and `Sphere`. If you need more than that (e.g. collision detection between two arbitrary polygons, cones, cylinders, etc.), you better start looking for a nice 2D or 3D physics engine.

-Paul

P.S.: for polygons, you could use the `PolyLine` class. It has a `contains( vec2 )` method to check if a point is inside the polygon.

---

<div class="post-metadata">

**Author:** ![Theo](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Theo](https://discourse.libcinder.org/u/Theo)\
**Post date:** [February 7, 2019, 7:59am UTC](https://discourse.libcinder.org/t/collision-2d-game/1421/4 "2019-02-07T07:59:47Z")

</div>

thanks guys i’ll implement that.  
I’ll keep you informed
