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

40 lines
1.7 KiB
Markdown

---
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.
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.
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...
Well, you're in luck, because I wrote a collision detection system called MoonTools.Bonk! https://moontools-docs.github.io/bonk/
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>
```