writing about transforms

main
Evan Hemsley 2019-09-14 23:39:26 -07:00
parent c714660ca7
commit a968e8ca74
12 changed files with 149 additions and 12 deletions

View File

@ -10,6 +10,8 @@ Bonk **is** designed to help you figure out if two shapes are overlapping and by
Bonk **is not** a physics simulator and it will not help you execute collision responses.
Bonk is designed for performance and memory efficiency. Defining shapes and performing collision tests require no heap allocations and thus put no pressure on the garbage collector. If you reuse spatial hashes, Bonk will never cause garbage collection.
Bonk is available as a [NuGet package](https://www.nuget.org/packages/MoonTools.Core.Bonk/).
Bonk is licensed under the [LGPL-3](https://www.gnu.org/licenses/lgpl-3.0.en.html). In summary: feel free to include it in your closed-source game and modify it internally at will, but if you make changes that you intend to redistribute, you **must** freely publish your changes.
Bonk is licensed under the [LGPL-3](https://www.gnu.org/licenses/lgpl-3.0.en.html) license. In summary: feel free to include it in your closed-source game and modify it internally at will, but if you make changes that you intend to redistribute, you **must** freely publish your changes.

View File

@ -1,12 +1,12 @@
+++
title = "Broad Phase"
date = 2019-09-14T18:33:15-07:00
weight = 15
weight = 20
chapter = true
pre = "<b>3. </b>"
pre = "<b>4. </b>"
+++
### Chapter 3
### Chapter 4
# Broad Phase

View File

@ -0,0 +1,24 @@
---
title: "EPA2D"
date: 2019-09-14T22:54:35-07:00
weight: 20
---
If an overlap has occurred, often we wish to determine the minimum separating vector, which is the shortest possible vector by which one of the shapes could be moved in order for them to stop overlapping. For this purpose, Bonk implements the Expanding Polytope Algorithm.
To use this feature, you must pass the shapes and transforms as well as the simplex that is returned from the *GJK2D.TestCollision* method.
```cs
var squareA = new Rectangle(-1, -1, 1, 1);
var transformA = Transform2D.DefaultTransform;
var squareB = new Rectangle(-1, -1, 1, 1);
var transformB = new Transform2D(new Vector2(1.5f, 0));
var (result, simplex) = GJK2D.TestCollision(squareA, transformA, squareB, transformB);
if (result)
{
var intersection = EPA2D.Intersect(squareA, transformA, squareB, transformB, simplex);
}
```

View File

@ -0,0 +1,20 @@
---
title: "GJK2D"
date: 2019-09-14T22:47:55-07:00
weight: 10
---
Bonk uses the Gilbert-Johnson-Keerthi, or GJK, algorithm to perform narrow phase collision detection.
To accurately check a collision, you must pass two sets of shapes and transforms. Remember that a transform operates on the vertices of a shape: it moves, rotates, and scales them in 2D space.
```cs
var circleA = new Circle(2);
var transformA = new Transform2D(new Vector2(-1, -1));
var circleB = new Circle(2);
var transformB = new Transform2D(new Vector2(1, 1));
var (result, simplex) = GJK2D.TestCollision(circleA, transformA, circleB, transformB);
```
Note that this method returns two variables. The first is whether an overlap has occurred. The second is the simplex. You can think of the simplex as a key that helps you find the minimum separating vector. Note that if there was no collision, the value of the simplex is irrelevant.

View File

@ -0,0 +1,13 @@
+++
title = "Narrow Phase"
date = 2019-09-14T22:10:17-07:00
weight = 40
chapter = true
pre = "<b>5. </b>"
+++
### Chapter 5
# Narrow Phase
Slow and precise.

View File

@ -0,0 +1,7 @@
---
title: "Overview"
date: 2019-09-14T22:11:27-07:00
weight: 5
---
In the broad phase, we were looking for speed. Now, once we have obtained a list of potential collisions, we want to know if these shapes are actually colliding or not. We are looking for accuracy.

View File

@ -1,12 +1,12 @@
+++
title = "Shapes"
date = 2019-09-14T19:08:45-07:00
weight = 10
weight = 15
chapter = true
pre = "<b>2. </b>"
pre = "<b>3. </b>"
+++
### Chapter 2
### Chapter 3
# Shapes

View File

@ -4,14 +4,14 @@ date: 2019-09-14T19:19:03-07:00
weight: 5
---
In Bonk, an **IShape2D** is defined by two methods.
In Bonk, an *IShape2D* is defined by two methods.
1) A *support function*, which is a method that returns the farthest possible point in the shape in a given **Vector2** direction. The shape vertices are transformed by a **Transform2D**.
1) A *support function*, which is a method that returns the farthest possible point in the shape in a given *Vector2* direction. The shape vertices are transformed by a *Transform2D*.
2) A method for creating an axis-aligned bounding box from the shape that takes a **Transform2D** to transform the shape vertices.
2) A method for creating an axis-aligned bounding box from the shape that takes a *Transform2D* to transform the shape vertices.
Bonk provides you with tested implementations of four common 2D shapes, but you can implement these methods to create custom **IShape2D** structs.
Bonk provides you with tested implementations of four common 2D shapes, but you can implement these methods to create custom *IShape2D* structs.
Note that shapes themselves do not contain absolute position data. You will use a **Transform2D** to manipulate the shape's position, rotation, and scale in 2D space.
Note that shapes themselves do not contain absolute position data. You will use a *Transform2D* to manipulate the shape's position, rotation, and scale in 2D space.
A final restriction to note: Defining concave shapes is an error. The collision solving algorithm that Bonk uses does not support concave shapes. If you wish to define concave shapes, define them as a composition of multiple convex shapes.

View File

@ -0,0 +1,31 @@
---
title: "Position2D"
date: 2019-09-14T23:17:46-07:00
weight: 10
---
In 2D games, when it comes to rendering we are generally dealing with pixels. This presents us with certain challenges that don't exist in 3D.
For example - what is half of a pixel? The pixel is the smallest possible unit in 2D rendering, so fractions of pixels don't really exist.
This can present us with problems when it comes to game physics. If your character is at coordinates (2.5, 3.5), where do you draw the character? Is their sprite rounded up, or down? It might look strange if the rounding does not line up with your physics system.
To avoid these problems, I created the *Position2D* struct. *Position2D* maintains an integer X and Y position, as well as X and Y fractional carryover. Whenever numbers with fractional values are added to the position, the fractional values are added to the carryover. Once the carryover value reaches 1, it is added to the coordinate. This means that we can treat positions as integer values without sacrificing precision.
This means that in our physics calculations, we will never have discrepancies between internal game position logic and rendering.
```cs
var one = new Position2D(1.3f, 3.5f);
var two = new Position2D(4.8f, 0.9f);
var result = one + two; // result.X is 6, result.Y is 4
```
When we subtract *Position2D* vectors, this creates a *Vector2* representing motion, and the carryover values are used.
```cs
var one = new Position2D(1.3f, 3.5f);
var two = new Position2D(4.8f, 0.9f);
var result = one - two; // result.X is -3.5f, result.Y is 2.6f
```

View File

@ -0,0 +1,13 @@
---
title: "Transform2D"
date: 2019-09-14T23:30:26-07:00
weight: 20
---
To create a *Transform2D*, you must provide a *Position2D* or a *Vector2* for translation (*Vector2* is converted to *Position2D* internally), a float representing rotation in degrees, and a *Vector2* representing scale.
```cs
var transform = new Transform2D(new Position2D(4, 1), 5f, new Vector2(3, 1));
```
The following section will describe how to use Shapes in conjunction with Transforms to perform collision detection.

View File

@ -0,0 +1,13 @@
+++
title = "Transform"
date = 2019-09-14T23:05:37-07:00
weight = 10
chapter = true
pre = "<b>2. </b>"
+++
### Chapter 2
# Transform
Mathematical operations in disguise.

View File

@ -0,0 +1,14 @@
---
title: "Overview"
date: 2019-09-14T23:08:31-07:00
weight: 5
---
Bonk uses the *Transform2D* struct which is taken from the MoonTools.Core.Structs package. By extension it also uses the *Position2D* struct taken from the same package.
A *Transform2D* is basically a way to store information about an object's location, rotation, and scale in 2-dimensional space. Transforms use matrix math to perform all of these operations at once, making them very fast.
For memory performance purposes, shapes are implemented as structs, meaning they are value types. This means that it is much easier to deal with collision using a transform applied to a shape, rather than containing a transform within the shape.
It is also often the case that you will have a shape attached to a game object that is offset from the object by a certain amount. A *Transform2D* can represent this information as well, since *Tranform2Ds* can be composed together. As you can see, a *Transform2D* provides a generic structure for dealing with many scenarios involving 2D space.