encompass-cs-docs/content/pong/ball/bouncing/_index.md

40 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2019-05-29 04:05:36 +00:00
---
title: "Bouncing"
date: 2019-05-28T18:47:18-07:00
weight: 100
---
Let's make the ball bounce off the sides of the game window.
I know what you're thinking. "Let's just read the dimensions of the game window. When the ball goes past them, we know it should bounce!"
**NO.**
We don't want the behavior of any object to be directly tied to some state outside of the game simulation. That's just asking for trouble!! What if you want the game boundaries to be different from the window size later? What if a different device has different dimensions? The possibilities are endless!
There's something else going on here too: eventually we're gonna need the ball to bounce off of the paddles as well right? I think what we really need here is a collision system.
2020-07-13 08:45:56 +00:00
All of our objects are rectangles so a simple AABB (axis-aligned bounding box) check will suffice. Essentially we just use non-rotating rectangles and check if they are overlapping.
2019-05-29 04:05:36 +00:00
2020-07-13 08:45:56 +00:00
Hang on a sec though - this is a pretty standard problem right? Pretty much every game in existence uses collision detection. There's got to be something we can use...
2019-05-29 04:05:36 +00:00
2020-12-22 23:02:52 +00:00
Well, you're in luck, because [I wrote a collision detection system called MoonTools.Bonk!](http://moonside.games/docs/bonk/)
2019-05-29 04:05:36 +00:00
2020-07-13 08:45:56 +00:00
Let's start by integrating it. We can add the framework as a *PackageReference* in PongFE.Framework.csproj:
```xml
<ItemGroup>
<PackageReference Include="MoonTools.Structs" Version="3.0.1"/>
<PackageReference Include="MoonTools.Bonk" Version="8.0.0"/>
</ItemGroup>
```
and PongFE.Core.csproj:
```xml
<ItemGroup>
<PackageReference Include="MoonTools.Structs" Version="3.0.1"/>
<PackageReference Include="MoonTools.Bonk" Version="8.0.0"/>
</ItemGroup>
```