Skip to content

Commit a67d18c

Browse files
committed
Fix hit delta calculations
1 parent 991eecb commit a67d18c

File tree

8 files changed

+36
-36
lines changed

8 files changed

+36
-36
lines changed

docs/bundle.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ <h2 id="axis-aligned-bounding-boxes">Axis-Aligned Bounding Boxes</h2>
234234
<span class="hljs-keyword">public</span> pos: Point;
235235
<span class="hljs-keyword">public</span> half: Point;
236236

237-
<span class="hljs-keyword">constructor</span>(<span class="hljs-params">pos, half</span>) {
237+
<span class="hljs-keyword">constructor</span>(<span class="hljs-params">pos: Point, half: Point</span>) {
238238
<span class="hljs-keyword">this</span>.pos = pos;
239239
<span class="hljs-keyword">this</span>.half = half;
240240
}</pre></div>
@@ -418,10 +418,10 @@ <h3 id="aabb-vs-segment">AABB vs Segment</h3>
418418
hit.normal.x = <span class="hljs-number">0</span>;
419419
hit.normal.y = -signY;
420420
}
421-
hit.delta.x = hit.time * delta.x;
422-
hit.delta.y = hit.time * delta.y;
423-
hit.pos.x = pos.x + hit.delta.x;
424-
hit.pos.y = pos.y + hit.delta.y;
421+
hit.delta.x = (<span class="hljs-number">1.0</span> - hit.time) * -delta.x;
422+
hit.delta.y = (<span class="hljs-number">1.0</span> - hit.time) * -delta.y;
423+
hit.pos.x = pos.x + delta.x * hit.time;
424+
hit.pos.y = pos.y + delta.y * hit.time;
425425
<span class="hljs-keyword">return</span> hit;
426426
}</pre></div>
427427

lib/intersect.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/intersect.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/intersect.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class AABB {
8080
public pos: Point;
8181
public half: Point;
8282

83-
constructor(pos, half) {
83+
constructor(pos: Point, half: Point) {
8484
this.pos = pos;
8585
this.half = half;
8686
}
@@ -144,10 +144,10 @@ export class AABB {
144144
hit.normal.x = 0;
145145
hit.normal.y = -signY;
146146
}
147-
hit.delta.x = hit.time * delta.x;
148-
hit.delta.y = hit.time * delta.y;
149-
hit.pos.x = pos.x + hit.delta.x;
150-
hit.pos.y = pos.y + hit.delta.y;
147+
hit.delta.x = (1.0 - hit.time) * -delta.x;
148+
hit.delta.y = (1.0 - hit.time) * -delta.y;
149+
hit.pos.x = pos.x + delta.x * hit.time;
150+
hit.pos.y = pos.y + delta.y * hit.time;
151151
return hit;
152152
}
153153

src/intersect.ts.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ each axis).
209209
public pos: Point;
210210
public half: Point;
211211

212-
constructor(pos, half) {
212+
constructor(pos: Point, half: Point) {
213213
this.pos = pos;
214214
this.half = half;
215215
}
@@ -388,10 +388,10 @@ the very starting of the line, so just set the hit time to zero.
388388
hit.normal.x = 0;
389389
hit.normal.y = -signY;
390390
}
391-
hit.delta.x = hit.time * delta.x;
392-
hit.delta.y = hit.time * delta.y;
393-
hit.pos.x = pos.x + hit.delta.x;
394-
hit.pos.y = pos.y + hit.delta.y;
391+
hit.delta.x = (1.0 - hit.time) * -delta.x;
392+
hit.delta.y = (1.0 - hit.time) * -delta.y;
393+
hit.pos.x = pos.x + delta.x * hit.time;
394+
hit.pos.y = pos.y + delta.y * hit.time;
395395
return hit;
396396
}
397397

test/aabb.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from 'assert';
22
import {AABB, EPSILON, Hit, Point, Sweep} from '../src/intersect';
33

4-
function almostEqual(actual, expected, message) {
4+
function almostEqual(actual, expected, message: string = undefined) {
55
if (Math.abs(actual - expected) > 1e-8) {
66
assert.equal(actual, expected, message);
77
}
@@ -85,8 +85,8 @@ describe('AABB', () => {
8585
almostEqual(hit.time, time);
8686
almostEqual(hit.pos.x, point.x + delta.x * time);
8787
almostEqual(hit.pos.y, point.y + delta.y * time);
88-
almostEqual(hit.delta.x, delta.x * time);
89-
almostEqual(hit.delta.y, delta.y * time);
88+
almostEqual(hit.delta.x, (1.0 - time) * -delta.x);
89+
almostEqual(hit.delta.y, (1.0 - time) * -delta.y);
9090
almostEqual(hit.normal.x, -1);
9191
almostEqual(hit.normal.y, 0);
9292
});
@@ -99,8 +99,8 @@ describe('AABB', () => {
9999
almostEqual(hit.time, 0);
100100
almostEqual(hit.pos.x, -4);
101101
almostEqual(hit.pos.y, 4);
102-
almostEqual(hit.delta.x, 0);
103-
almostEqual(hit.delta.y, 0);
102+
almostEqual(hit.delta.x, -delta.x);
103+
almostEqual(hit.delta.y, -delta.y);
104104
almostEqual(hit.normal.x, -1);
105105
almostEqual(hit.normal.y, 0);
106106
});
@@ -116,8 +116,8 @@ describe('AABB', () => {
116116
almostEqual(hit.time, time);
117117
almostEqual(hit.pos.x, point.x + delta.x * time);
118118
almostEqual(hit.pos.y, point.y + delta.y * time);
119-
almostEqual(hit.delta.x, delta.x * time);
120-
almostEqual(hit.delta.y, delta.y * time);
119+
almostEqual(hit.delta.x, (1.0 - time) * -delta.x);
120+
almostEqual(hit.delta.y, (1.0 - time) * -delta.y);
121121
almostEqual(hit.normal.x, -1);
122122
almostEqual(hit.normal.y, 0);
123123
});
@@ -296,8 +296,8 @@ describe('AABB', () => {
296296
almostEqual(sweep.hit.time, time);
297297
almostEqual(sweep.hit.pos.x, aabb2.pos.x + delta.x * time + direction.x * aabb2.half.x);
298298
almostEqual(sweep.hit.pos.y, aabb2.pos.y + delta.y * time + direction.y * aabb2.half.y);
299-
almostEqual(sweep.hit.delta.x, delta.x * time);
300-
almostEqual(sweep.hit.delta.y, delta.y * time);
299+
almostEqual(sweep.hit.delta.x, (1.0 - time) * -delta.x);
300+
almostEqual(sweep.hit.delta.y, (1.0 - time) * -delta.y);
301301
});
302302

303303
test('should set sweep.hit.normal to normals of box 1', () => {
@@ -317,8 +317,8 @@ describe('AABB', () => {
317317
almostEqual(sweep.pos.x, 0);
318318
almostEqual(sweep.pos.y, -4);
319319
almostEqual(sweep.hit.time, 0);
320-
almostEqual(sweep.hit.delta.x, 0);
321-
almostEqual(sweep.hit.delta.y, 0);
320+
almostEqual(sweep.hit.delta.x, -delta.x);
321+
almostEqual(sweep.hit.delta.y, -delta.y);
322322
});
323323
});
324324
});

0 commit comments

Comments
 (0)