Skip to content

Commit

Permalink
added intersect result
Browse files Browse the repository at this point in the history
  • Loading branch information
hamaluik committed Feb 15, 2020
1 parent 56f7971 commit 2e9a4b5
Show file tree
Hide file tree
Showing 30 changed files with 199 additions and 60 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.0] - 2020-02-15
### Added
- Added `TestResult` type which is returned from `test()` and can be checked directly
for collisions or passed to `intersect()` to further calculate an intersection
- Added `IntersectResult` type which is returned from `intersect()` and can be used
to calculate collision normals, separation distances, etc
- Added `Headbutt.testAndIntersect()` function which is a shortcut for testing and
then calculating the intersection if a collision is found

### Changed
- Changed `Headbutt` interface from interacting with an object with internal state
to static pure functions (i.e. call `Headbutt.test()` instead of `new Headbutt().test()`)

## [0.5.0] - 2020-02-15
### Added
- Added transformation support to all provided shape classes
Expand All @@ -28,7 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.3.0] - 2017-07-01
- Initial release

[Unreleased]: https://github.com/hamaluik/headbutt/compare/v0.5.0...HEAD
[Unreleased]: https://github.com/hamaluik/headbutt/compare/v0.6.0...HEAD
[0.6.0]: https://github.com/hamaluik/headbutt/compare/v0.4.0...v0.6.0
[0.5.0]: https://github.com/hamaluik/headbutt/compare/v0.4.0...v0.5.0
[0.4.0]: https://github.com/hamaluik/headbutt/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/hamaluik/headbutt/releases/tag/v0.3.0
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,31 @@ Create either 2D shapes or 3D shapes by implementing the appropriate interface:
* `Box`
* `Polyhedron`

Then, instantiate an instance of the appropriate `Headbutt`:
Apply generic transformations to shapes:

```haxe
var hb2 = new headbutt.twod.Headbutt();
var hb3 = new headbutt.threed.Headbutt();
var box = new Box(new Vec3(0.5, 0.5, 0.5));
box.transform = GLM.transform(
new Vec3(5, -1, 3), // translation
Quat.fromEuler(Math.PI / 4, 0, Math.PI / 3, new Quat()), // rotation
new Vec3(2, 1, 4) // scale
);
```

Next, check shapes for intersections:
Then, check shapes for intersections:

```haxe
if(hb2.test(shapeA, shapeB)) { /*...*/ }
if(hb3.test(objectA, objectB)) { /*...*/ }
if(headbutt.twod.Headbutt.test(shapeA, shapeB)) { /*...*/ }
if(headbutt.threed.Headbutt.test(objectA, objectB)) { /*...*/ }
```

Alternatively, calculate intersections (**note:** intersection calculations haven't been implemented in 3D yet!):

```haxe
var penetration: Null<Vec2> = hb2.intersect(shapeA, shapeB);
var testResult = headbutt.twod.Headbutt.test(shapeA, shapeB);
var intersection = headbutt.twod.Headbutt.intersect(testResult);
// or:
var intersection = headbutt.twod.Headbutt.testAndIntersect(shapeA, shapeB);
```

## API
Expand Down
17 changes: 8 additions & 9 deletions bench/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class Main {
}

public static function main(): Void {
var hb = new Headbutt();
println('| Test | Intersect | Headbutt (μs/iter) |');
println('|:-----|:---------:|---------:|-------:|');

Expand All @@ -77,13 +76,13 @@ class Main {

var line_line_int_hb = new Benchmark();
line_line_int_hb.run(function() {
hb.test(lineA, lineB);
Headbutt.test(lineA, lineB);
});
println('| line/line | ✔ | ${line_line_int_hb.toString()} |');

var line_line_noint_hb = new Benchmark();
line_line_noint_hb.run(function() {
hb.test(lineA, lineC);
Headbutt.test(lineA, lineC);
});
println('| line/line | ✗ | ${line_line_noint_hb.toString()} |');

Expand All @@ -93,31 +92,31 @@ class Main {

var circ_circ_int_hb = new Benchmark();
circ_circ_int_hb.run(function() {
hb.test(circleA, circleB);
Headbutt.test(circleA, circleB);
});
println('| circ/circ | ✔ | ${circ_circ_int_hb.toString()} |');

var circ_circ_noint_hb = new Benchmark();
circ_circ_noint_hb.run(function() {
hb.test(circleA, circleC);
Headbutt.test(circleA, circleC);
});
println('| circ/circ | ✗ | ${circ_circ_noint_hb.toString()} |');

var pentA = new headbutt.twod.shapes.Polygon([new Vec2(0, 1), new Vec2(1, 0.5), new Vec2(1, -1), new Vec2(-1, -1), new Vec2(-1, 0.5)]);
var pentB = new headbutt.twod.shapes.Polygon([new Vec2(0, 1), new Vec2(1, 0.5), new Vec2(1, -1), new Vec2(-1, -1), new Vec2(-1, 0.5)]);
pentB.set_trs(new Vec2(0.5, 0), 0, new Vec2(1, 1));
pentB.setTransform(new Vec2(0.5, 0), 0, new Vec2(1, 1));
var pentC = new headbutt.twod.shapes.Polygon([new Vec2(0, 1), new Vec2(1, 0.5), new Vec2(1, -1), new Vec2(-1, -1), new Vec2(-1, 0.5)]);
pentC.set_trs(new Vec2(5, 0), 0, new Vec2(1, 1));
pentC.setTransform(new Vec2(5, 0), 0, new Vec2(1, 1));

var pent_pent_int_hb = new Benchmark();
pent_pent_int_hb.run(function() {
hb.test(pentA, pentB);
Headbutt.test(pentA, pentB);
});
println('| pent/pent | ✔ | ${pent_pent_int_hb.toString()} |');

var pent_pent_noint_hb = new Benchmark();
pent_pent_noint_hb.run(function() {
hb.test(pentA, pentC);
Headbutt.test(pentA, pentC);
});
println('| pent/pent | ✗ | ${pent_pent_noint_hb.toString()} |');
}
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang="en"><!-- use theme color or fallback --><!--use textcolor from settings, otherwise create a contrasting color to theme color--><head><meta charset="utf-8"/><link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet"/><link href="./bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet"/><link href="./bootstrap/css/bootstrap-select.min.css" rel="stylesheet"/><link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,700italic,400italic" rel="stylesheet" type="text/css"/><link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,600,600italic,400" rel="stylesheet" type="text/css"/><link href="https://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css" rel="stylesheet" type="text/css"/><script src="./jquery-1.9.1.min.js"></script><script src="./bootstrap/js/bootstrap.min.js"></script><script src="./bootstrap/js/bootstrap-select.min.js"></script><link href="./styles.css" rel="stylesheet"/><link href="./extra-styles.css" rel="stylesheet"/><link href="./haxe-nav.css" rel="stylesheet"/><script>var dox = {rootPath: "./",platforms: ["dox"]};</script><script src="./nav.js"></script><script src="./index.js"></script><link rel="icon" href="./favicon.ico" type="image/x-icon"/><title>File not found - Headbutt</title></head><body><style>.navbar .brand {display: inline-block;float: none;text-shadow: 0 0 0 transparent;</style><nav class="nav"><div class="navbar"><div class="navbar-inner" style="background:#FAFAFA; border-bottom:1px solid rgba(0,0,0,.09)"><div class="container"><a class="brand" style="color:#000000" href="./">Headbutt</a></div></div></div></nav><div class="container main-content"><div class="row-fluid"><div class="span3"><div class="well sidebar-nav"><form class="form-search" id="searchForm"><div class="input-prepend input-block-level"><span class="add-on"><i class="icon-search"></i></span><input id="search" type="text" placeholder="Filter" autocomplete="off"/></div></form></div><div class="well sidebar-nav"><div id="nav"></div></div></div><div class="span9"><script type="text/javascript">$(document).ready(errorSearch);</script><h1><small>404</small> Page not found</h1><p>Page not found, sorry.</p></div></div></div><footer class="section site-footer" style="background:#FAFAFA"><div class="container"><div class="copyright"><p style="color:#000000">This documentation is generated for version 0.5.0</p><p style="color:#000000">&copy; 2020 &nbsp;</p></div></div></footer><script src=".//highlighter.js"></script><link href="./highlighter.css" rel="stylesheet"/></body></html>
<!DOCTYPE html><html lang="en"><!-- use theme color or fallback --><!--use textcolor from settings, otherwise create a contrasting color to theme color--><head><meta charset="utf-8"/><link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet"/><link href="./bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet"/><link href="./bootstrap/css/bootstrap-select.min.css" rel="stylesheet"/><link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,700italic,400italic" rel="stylesheet" type="text/css"/><link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,600,600italic,400" rel="stylesheet" type="text/css"/><link href="https://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css" rel="stylesheet" type="text/css"/><script src="./jquery-1.9.1.min.js"></script><script src="./bootstrap/js/bootstrap.min.js"></script><script src="./bootstrap/js/bootstrap-select.min.js"></script><link href="./styles.css" rel="stylesheet"/><link href="./extra-styles.css" rel="stylesheet"/><link href="./haxe-nav.css" rel="stylesheet"/><script>var dox = {rootPath: "./",platforms: ["dox"]};</script><script src="./nav.js"></script><script src="./index.js"></script><link rel="icon" href="./favicon.ico" type="image/x-icon"/><title>File not found - Headbutt</title></head><body><style>.navbar .brand {display: inline-block;float: none;text-shadow: 0 0 0 transparent;</style><nav class="nav"><div class="navbar"><div class="navbar-inner" style="background:#FAFAFA; border-bottom:1px solid rgba(0,0,0,.09)"><div class="container"><a class="brand" style="color:#000000" href="./">Headbutt</a></div></div></div></nav><div class="container main-content"><div class="row-fluid"><div class="span3"><div class="well sidebar-nav"><form class="form-search" id="searchForm"><div class="input-prepend input-block-level"><span class="add-on"><i class="icon-search"></i></span><input id="search" type="text" placeholder="Filter" autocomplete="off"/></div></form></div><div class="well sidebar-nav"><div id="nav"></div></div></div><div class="span9"><script type="text/javascript">$(document).ready(errorSearch);</script><h1><small>404</small> Page not found</h1><p>Page not found, sorry.</p></div></div></div><footer class="section site-footer" style="background:#FAFAFA"><div class="container"><div class="copyright"><p style="color:#000000">This documentation is generated for version 0.6.0</p><p style="color:#000000">&copy; 2020 &nbsp;</p></div></div></footer><script src=".//highlighter.js"></script><link href="./highlighter.css" rel="stylesheet"/></body></html>
5 changes: 2 additions & 3 deletions docs/headbutt/threed/Headbutt.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
display: inline-block;
float: none;
text-shadow: 0 0 0 transparent;
</style><nav class="nav"><div class="navbar"><div class="navbar-inner" style="background:#FAFAFA; border-bottom:1px solid rgba(0,0,0,.09)"><div class="container"><a class="brand" style="color:#000000" href="../../">Headbutt</a></div></div></div></nav><div class="container main-content"><div class="row-fluid"><div class="span3"><div class="well sidebar-nav"><form class="form-search" id="searchForm"><div class="input-prepend input-block-level"><span class="add-on"><i class="icon-search"></i></span><input id="search" type="text" placeholder="Filter" autocomplete="off"/></div></form></div><div class="well sidebar-nav"><div id="nav"></div></div></div><div class="span9"><div class="page-header"><span class="viewsource"><a href="https://github.com/hamaluik/headbutt/tree/master/src/headbutt/threed/Headbutt.hx" class="btn btn-medium"><i class="fa fa-eye"></i> View source</a></span><h1><small>class</small> Headbutt</h1><h4><small>package <a href="../../headbutt/threed/index.html">headbutt.threed</a></small></h4> <hr/></div><div class="body"><div class="doc doc-main"><div class="indent"><p></p></div></div><h3 class="section">Constructor</h3><div class="fields"><div class="field "><a name="new"></a><h3 class="anchor"><code><a href="#new"><span class="identifier">new</span></a> ()</code></h3><div class="doc"><p>Create a new Headbutt instance. Headbutt needs to be instantiated because
internally it stores state. This may change in the future.</p></div></div></div><h3 class="section">Variables</h3><div class="fields"><div class="field "><a name="maxIterations"></a><h3 class="anchor"><code><span class="label label-meta label-meta-value" title="@:value metadata">@:value(20)</span><a href="../../headbutt/threed/Headbutt.html#maxIterations"><span class="identifier">maxIterations</span></a>:<span class="type">Int</span><span> = 20</span></code></h3><div class="doc"><p>The maximum number of simplex evolution iterations before we accept the
</style><nav class="nav"><div class="navbar"><div class="navbar-inner" style="background:#FAFAFA; border-bottom:1px solid rgba(0,0,0,.09)"><div class="container"><a class="brand" style="color:#000000" href="../../">Headbutt</a></div></div></div></nav><div class="container main-content"><div class="row-fluid"><div class="span3"><div class="well sidebar-nav"><form class="form-search" id="searchForm"><div class="input-prepend input-block-level"><span class="add-on"><i class="icon-search"></i></span><input id="search" type="text" placeholder="Filter" autocomplete="off"/></div></form></div><div class="well sidebar-nav"><div id="nav"></div></div></div><div class="span9"><div class="page-header"><span class="viewsource"><a href="https://github.com/hamaluik/headbutt/tree/master/src/headbutt/threed/Headbutt.hx" class="btn btn-medium"><i class="fa fa-eye"></i> View source</a></span><h1><small>class</small> Headbutt</h1><h4><small>package <a href="../../headbutt/threed/index.html">headbutt.threed</a></small></h4> <hr/></div><div class="body"><div class="doc doc-main"><div class="indent"><p></p></div></div><h3 class="section">Static variables</h3><div class="fields"><div class="field "><a name="MAX_ITERATIONS"></a><h3 class="anchor"><code><span class="label label-meta label-meta-value" title="@:value metadata">@:value(20)</span><span class="label label-static">static</span><a href="../../headbutt/threed/Headbutt.html#MAX_ITERATIONS"><span class="identifier">MAX_ITERATIONS</span></a>:<span class="type">Int</span><span> = 20</span></code></h3><div class="doc"><p>The maximum number of simplex evolution iterations before we accept the
given simplex. For non-curvy shapes, this can be low. Curvy shapes potentially
require higher numbers, but this can introduce significant slow-downs at
the gain of not much accuracy.</p></div></div></div><h3 class="section">Methods</h3><div class="fields"><div class="field "><a name="test"></a><h3 class="anchor"><code><a href="#test"><span class="identifier">test</span></a> (<span style="white-space:nowrap">shapeA:<a class="type" title="" href="../../headbutt/threed/Shape.html">Shape</a>,</span> <span style="white-space:nowrap">shapeB:<a class="type" title="" href="../../headbutt/threed/Shape.html">Shape</a></span>):<span class="type">Bool</span></code></h3><div class="doc"><p>Given two convex shapes, test whether they overlap or not</p><p class="javadoc">Parameters:</p><table class="table table-bordered params"><tr><th style="width:25%;"><code>shapeA</code></th><td></td></tr><tr><th style="width:25%;"><code>shapeB</code></th><td></td></tr></table><p class="javadoc">Returns:</p><div class="indent inline-content"><p>Bool</p></div></div></div></div></div></div></div></div><footer class="section site-footer" style="background:#FAFAFA"><div class="container"><div class="copyright"><p style="color:#000000">This documentation is generated for version 0.5.0</p><p style="color:#000000">&copy; 2020 &nbsp;</p></div></div></footer><script src="../..//highlighter.js"></script><link href="../../highlighter.css" rel="stylesheet"/></body></html>
the gain of not much accuracy.</p></div></div></div><h3 class="section">Static methods</h3><div class="fields"><div class="field "><a name="test"></a><h3 class="anchor"><code><span class="label label-static">static</span><a href="#test"><span class="identifier">test</span></a> (<span style="white-space:nowrap">shapeA:<a class="type" title="" href="../../headbutt/threed/Shape.html">Shape</a>,</span> <span style="white-space:nowrap">shapeB:<a class="type" title="" href="../../headbutt/threed/Shape.html">Shape</a></span>):<span class="type">Bool</span></code></h3><div class="doc"><p>Given two convex shapes, test whether they overlap or not</p><p class="javadoc">Parameters:</p><table class="table table-bordered params"><tr><th style="width:25%;"><code>shapeA</code></th><td></td></tr><tr><th style="width:25%;"><code>shapeB</code></th><td></td></tr></table><p class="javadoc">Returns:</p><div class="indent inline-content"><p>Bool</p></div></div></div></div></div></div></div></div><footer class="section site-footer" style="background:#FAFAFA"><div class="container"><div class="copyright"><p style="color:#000000">This documentation is generated for version 0.6.0</p><p style="color:#000000">&copy; 2020 &nbsp;</p></div></div></footer><script src="../..//highlighter.js"></script><link href="../../highlighter.css" rel="stylesheet"/></body></html>
Loading

0 comments on commit 2e9a4b5

Please sign in to comment.