-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PointInside to Shapes - Polygon and Ellipse (#48)
- Loading branch information
1 parent
90584d2
commit 256ce7d
Showing
10 changed files
with
302 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Shapes | ||
|
||
Shapes are fundamental to the game engine, used in rendering sprites, determining collisions, and more. All shapes implement the `IShape` interface, which provides some useful functions: | ||
|
||
## IShape | ||
|
||
```typescript | ||
Transform(transform: Transform): IShape; | ||
FarthestPointInDirection(direction: Vector): Vector; | ||
PointInside(point: Vector): boolean; | ||
``` | ||
|
||
- `Transform` takes a transform and applies it to shape; translating, rotating and scaling a shape. | ||
- `FarthestPointInDirection` returns the point that is farthest in the direction provided. Used in the GJK algorithm for collision detection. | ||
- `PointInside` determines if a point provided is within the shape or not. | ||
|
||
See the [API reference for more details](../../reference/interfaces/ishape). | ||
|
||
## Polygon | ||
|
||
The `Polygon` is a series of arbitrary points that make up a shape. It can be declared by passing an array of points to the constructor. For example: | ||
```typescript | ||
const triangle = new Polygon([ | ||
new Vector(-1, -1), | ||
new Vector(1, -1), | ||
new Vector(0, 1) | ||
]); | ||
``` | ||
See the [API reference for more details](../../reference/classes/polygon). | ||
|
||
## Ellipse | ||
|
||
The `Ellipse` is an elliptical representation of a shape, for example an oval or a circle. It is represented by a center position, dimensions, and a rotation. For example: | ||
```typescript | ||
const oval = new Ellipse(new Vector(4, 2), Math.PI / 4, new Vector(2, 2)); | ||
``` | ||
See the [API reference for more details](../../reference/classes/ellipse). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
Copyright 2020 JamJar Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import Vector from "../../geometry/vector"; | ||
import Ellipse from "./ellipse"; | ||
import Transform from "../transform/transform"; | ||
|
||
describe("Ellise - FarthestPointInDirection", () => { | ||
type TestTuple = [string, Vector, Ellipse, Vector]; | ||
test.each<TestTuple>([ | ||
[ | ||
"Circle, most upwards point", | ||
new Vector(0, 1), | ||
new Ellipse(new Vector(1, 1), 0, new Vector(0, 0)), | ||
new Vector(0, 1) | ||
], | ||
[ | ||
"Circle, top right point", | ||
new Vector(0.7071067811865476, 0.7071067811865475), | ||
new Ellipse(new Vector(1, 1), 0, new Vector(0, 0)), | ||
new Vector(1, 1) | ||
], | ||
[ | ||
"Oval, right point", | ||
new Vector(4, 0), | ||
new Ellipse(new Vector(4, 2), 0, new Vector(0, 0)), | ||
new Vector(1, 0) | ||
], | ||
[ | ||
"Oval, rotated 45 degrees, right point", | ||
new Vector(1.4142135623730954, 0), | ||
new Ellipse(new Vector(4, 2), Math.PI / 4, new Vector(0, 0)), | ||
new Vector(1, 0) | ||
], | ||
])("%p", (description: string, expected: Vector, ellipse: Ellipse, direction: Vector) => { | ||
const result = ellipse.FarthestPointInDirection(direction); | ||
expect(result.x).toBeCloseTo(expected.x, 5); | ||
expect(result.y).toBeCloseTo(expected.y, 5); | ||
}); | ||
}); | ||
|
||
describe("Ellipse - Transform", () => { | ||
type TestTuple = [string, Ellipse, Ellipse, Transform]; | ||
test.each<TestTuple>([ | ||
[ | ||
"Scale circle by 5", | ||
new Ellipse(new Vector(10, 10), 0, new Vector(0, 0)), | ||
new Ellipse(new Vector(2, 2), 0, new Vector(0, 0)), | ||
new Transform(new Vector(0, 0), new Vector(5, 5)) | ||
], | ||
[ | ||
"Translate circle up 3, left 2", | ||
new Ellipse(new Vector(2, 2), 0, new Vector(3, -2)), | ||
new Ellipse(new Vector(2, 2), 0, new Vector(0, 0)), | ||
new Transform(new Vector(3, -2), new Vector(1, 1)) | ||
], | ||
[ | ||
"Rotate oval by 45 degrees", | ||
new Ellipse(new Vector(2, 4), Math.PI / 4, new Vector(0, 0)), | ||
new Ellipse(new Vector(2, 4), 0, new Vector(0, 0)), | ||
new Transform(new Vector(0, 0), new Vector(1, 1), Math.PI / 4) | ||
], | ||
[ | ||
"Scale, Translate and Rotate oval by (2,3), (-3,10), and 45 degrees", | ||
new Ellipse(new Vector(4, 12), Math.PI / 4, new Vector(8.31370849898476, 7.171572875253811)), | ||
new Ellipse(new Vector(2, 4), 0, new Vector(5, 2)), | ||
new Transform(new Vector(-3, 10), new Vector(2, 3), Math.PI / 4) | ||
], | ||
])("%p", (description: string, expected: Ellipse, ellipse: Ellipse, transform: Transform) => { | ||
expect(ellipse.Transform(transform)).toEqual(expected) | ||
}); | ||
}); | ||
|
||
describe("Ellipse - PointInside", () => { | ||
type TestTuple = [string, boolean, Ellipse, Vector]; | ||
test.each<TestTuple>([ | ||
[ | ||
"Circle around origin, point inside", | ||
true, | ||
new Ellipse(new Vector(1, 1), 0, new Vector(0, 0)), | ||
new Vector(0.2,0.2) | ||
], | ||
[ | ||
"Circle around origin, point outside", | ||
false, | ||
new Ellipse(new Vector(1, 1), 0, new Vector(0, 0)), | ||
new Vector(1.2,0.2) | ||
], | ||
[ | ||
"Circle around arbitrary point, point inside", | ||
true, | ||
new Ellipse(new Vector(1, 1), 0, new Vector(3, 2)), | ||
new Vector(2.9,2.3) | ||
], | ||
[ | ||
"Circle around arbitrary point, point outside", | ||
false, | ||
new Ellipse(new Vector(1, 1), 0, new Vector(5, 2)), | ||
new Vector(3,5) | ||
], | ||
])("%p", (description: string, expected: boolean, ellipse: Ellipse, point: Vector) => { | ||
expect(ellipse.PointInside(point)).toEqual(expected) | ||
}); | ||
}); | ||
|
||
|
||
describe("Polygon - Circle", () => { | ||
type TestTuple = [string, Ellipse, Ellipse]; | ||
test.each<TestTuple>([ | ||
[ | ||
"2r circle around origin", | ||
new Ellipse(new Vector(2, 2), 0, new Vector(0, 0)), | ||
Ellipse.Circle(2, new Vector(0, 0)) | ||
], | ||
])("%p", (description: string, expected: Ellipse, ellipse: Ellipse) => { | ||
expect(ellipse).toEqual(expected) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.