bonk-docs/content/narrow_phase/SweepTest.md

33 lines
990 B
Markdown
Raw Permalink Normal View History

2020-07-18 20:13:59 +00:00
---
title: "Sweep Test"
date: 2020-07-18T12:37:29-07:00
2020-07-18 20:18:51 +00:00
weight: 25
2020-07-18 20:13:59 +00:00
---
Sweep testing is an efficient way to check collision positions on moving objects.
At the moment, Bonk only implements sweep testing on Rectangles.
Example:
```cs
var rectangle = new Rectangle(-2, -2, 4, 4);
var transform = new Transform2D(new Position2D(-6, 0));
var otherRectangle = new Rectangle(-2, -2, 4, 4);
var otherTransform = new Transform2D(new Position2D(6, 0));
var farthestRectangle = new Rectangle(-2, -2, 4, 4);
var farthestTransform = new Transform2D(new Position2D(12, 0));
var downRectangle = new Rectangle(-6, -2, 12, 4);
var downTransform = new Transform2D(new Position2D(-6, 20));
var spatialHash = new SpatialHash<int>(16);
spatialHash.Insert(1, otherRectangle, otherTransform);
spatialHash.Insert(2, farthestRectangle, farthestTransform);
spatialHash.Insert(3, downRectangle, downTransform);
var result = SweepTest.Test(spatialHash, rectangle, transform, new Vector2(12, 0)); // hits
```