Fix collision detection issues
This commit is contained in:
parent
3c5d87a98b
commit
6d6284f717
6 changed files with 40 additions and 39 deletions
|
|
@ -23,6 +23,7 @@ export const settings = {
|
||||||
shaderMacros: {
|
shaderMacros: {
|
||||||
distanceScale: 64,
|
distanceScale: 64,
|
||||||
distanceOffset: 0.15,
|
distanceOffset: 0.15,
|
||||||
|
//edgeSmoothing: 0,
|
||||||
edgeSmoothing: 10,
|
edgeSmoothing: 10,
|
||||||
},
|
},
|
||||||
shaderCombinations: {
|
shaderCombinations: {
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,10 @@ void main() {
|
||||||
for (int i = 0; i < CIRCLE_LIGHT_COUNT; i++) {
|
for (int i = 0; i < CIRCLE_LIGHT_COUNT; i++) {
|
||||||
float lightCenterDistance = distance(circleLights[i].center, worldCoordinates);
|
float lightCenterDistance = distance(circleLights[i].center, worldCoordinates);
|
||||||
|
|
||||||
|
/*if (lightCenterDistance < circleLights[i].radius) {
|
||||||
|
lighting = vec3(1.0, 1.0, 0.0);
|
||||||
|
}*/
|
||||||
|
|
||||||
vec3 lightColorAtPosition = circleLights[i].value / pow(
|
vec3 lightColorAtPosition = circleLights[i].value / pow(
|
||||||
lightCenterDistance / LIGHT_DROP + 1.0, 2.0
|
lightCenterDistance / LIGHT_DROP + 1.0, 2.0
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ export class Camera extends GameObject {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.boundingBox = new BoundingBox(null);
|
this.boundingBox = new BoundingBox(null);
|
||||||
physics.addDynamicBoundingBox(this.boundingBox);
|
//physics.addDynamicBoundingBox(this.boundingBox);
|
||||||
|
|
||||||
this.addCommandExecutor(BeforeRenderCommand, this.draw.bind(this));
|
this.addCommandExecutor(BeforeRenderCommand, this.draw.bind(this));
|
||||||
this.addCommandExecutor(MoveToCommand, this.moveTo.bind(this));
|
this.addCommandExecutor(MoveToCommand, this.moveTo.bind(this));
|
||||||
|
|
|
||||||
|
|
@ -43,17 +43,13 @@ export class Character extends GameObject {
|
||||||
const nextPrimitive = this.primitive.clone();
|
const nextPrimitive = this.primitive.clone();
|
||||||
nextPrimitive.center = value;
|
nextPrimitive.center = value;
|
||||||
|
|
||||||
console.log(this.physics
|
|
||||||
.findIntersecting(nextPrimitive.boundingBox)
|
|
||||||
.filter(b => b.value)
|
|
||||||
.map((b) => b.value.distance(nextPrimitive.center) + 2 * nextPrimitive.radius)
|
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.physics
|
this.physics
|
||||||
.findIntersecting(nextPrimitive.boundingBox)
|
.findIntersecting(nextPrimitive.boundingBox)
|
||||||
.filter(b => b.value)
|
.filter((b) => b.value)
|
||||||
.map((b) => b.value.distance(nextPrimitive.center) + 2 * nextPrimitive.radius)
|
.map(
|
||||||
|
(b) => b.value.distance(nextPrimitive.center) + nextPrimitive.radius
|
||||||
|
)
|
||||||
.find((d) => d < 0) !== undefined
|
.find((d) => d < 0) !== undefined
|
||||||
) {
|
) {
|
||||||
this.setPosition(value);
|
this.setPosition(value);
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,9 @@ class Node {
|
||||||
export class BoundingBoxTree {
|
export class BoundingBoxTree {
|
||||||
root?: Node;
|
root?: Node;
|
||||||
|
|
||||||
constructor(boxes?: Array<ImmutableBoundingBox>) {
|
constructor(boxes: Array<ImmutableBoundingBox> = []) {
|
||||||
if (boxes) {
|
|
||||||
this.build(boxes);
|
this.build(boxes);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public build(boxes: Array<ImmutableBoundingBox>) {
|
public build(boxes: Array<ImmutableBoundingBox>) {
|
||||||
this.root = this.buildRecursive(boxes, 0, null);
|
this.root = this.buildRecursive(boxes, 0, null);
|
||||||
|
|
@ -83,7 +81,7 @@ export class BoundingBoxTree {
|
||||||
box: ImmutableBoundingBox
|
box: ImmutableBoundingBox
|
||||||
): Array<ImmutableBoundingBox> {
|
): Array<ImmutableBoundingBox> {
|
||||||
const maybeResults = this.findMaybeIntersecting(box, this.root, 0);
|
const maybeResults = this.findMaybeIntersecting(box, this.root, 0);
|
||||||
const results = maybeResults.filter(box.intersects.bind(box));
|
const results = maybeResults.filter((b) => b.intersects(box));
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,17 +94,22 @@ export class BoundingBoxTree {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const comparisons: Array<(
|
if (depth % 4 == 0 && box.xMax < node.rectangle.xMin) {
|
||||||
a: ImmutableBoundingBox,
|
return this.findMaybeIntersecting(box, node.left, depth + 1);
|
||||||
b: ImmutableBoundingBox
|
}
|
||||||
) => boolean> = [
|
|
||||||
(a, b) => a.xMin < b.xMax,
|
if (depth % 4 == 1 && box.xMin > node.rectangle.xMax) {
|
||||||
(a, b) => a.xMax > b.xMin,
|
return this.findMaybeIntersecting(box, node.right, depth + 1);
|
||||||
(a, b) => a.yMin < b.yMax,
|
}
|
||||||
(a, b) => a.xMax > b.xMin,
|
|
||||||
];
|
if (depth % 4 == 2 && box.yMax < node.rectangle.yMin) {
|
||||||
|
return this.findMaybeIntersecting(box, node.left, depth + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth % 4 == 3 && box.yMin > node.rectangle.yMax) {
|
||||||
|
return this.findMaybeIntersecting(box, node.right, depth + 1);
|
||||||
|
}
|
||||||
|
|
||||||
if (comparisons[depth % 4](node.rectangle, box)) {
|
|
||||||
return [
|
return [
|
||||||
node.rectangle,
|
node.rectangle,
|
||||||
...this.findMaybeIntersecting(box, node.left, depth + 1),
|
...this.findMaybeIntersecting(box, node.left, depth + 1),
|
||||||
|
|
@ -114,9 +117,6 @@ export class BoundingBoxTree {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.findMaybeIntersecting(box, node.left, depth + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private findParent(
|
private findParent(
|
||||||
box: ImmutableBoundingBox,
|
box: ImmutableBoundingBox,
|
||||||
node: Node,
|
node: Node,
|
||||||
|
|
@ -124,7 +124,7 @@ export class BoundingBoxTree {
|
||||||
parent: Node
|
parent: Node
|
||||||
): [Node, number] {
|
): [Node, number] {
|
||||||
if (node === null) {
|
if (node === null) {
|
||||||
return [parent, depth];
|
return [parent, depth - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
const dimension = depth % 4;
|
const dimension = depth % 4;
|
||||||
|
|
|
||||||
|
|
@ -4,23 +4,23 @@ import { ImmutableBoundingBox } from './containers/immutable-bounding-box';
|
||||||
import { BoundingBoxBase } from './containers/bounding-box-base';
|
import { BoundingBoxBase } from './containers/bounding-box-base';
|
||||||
|
|
||||||
export class Physics {
|
export class Physics {
|
||||||
private staticBoundingBoxesWaitList = [];
|
|
||||||
private isTreeInitialized = false;
|
private isTreeInitialized = false;
|
||||||
|
private staticBoundingBoxesWaitList = [];
|
||||||
private staticBoundingBoxes = new BoundingBoxTree();
|
private staticBoundingBoxes = new BoundingBoxTree();
|
||||||
|
|
||||||
private dynamicBoundingBoxes = new BoundingBoxList();
|
private dynamicBoundingBoxes = new BoundingBoxList();
|
||||||
|
|
||||||
public addStaticBoundingBox(boundingBox: ImmutableBoundingBox) {
|
public addStaticBoundingBox(boundingBox: ImmutableBoundingBox) {
|
||||||
|
if (!this.isTreeInitialized) {
|
||||||
this.staticBoundingBoxesWaitList.push(boundingBox);
|
this.staticBoundingBoxesWaitList.push(boundingBox);
|
||||||
|
} else {
|
||||||
|
this.staticBoundingBoxes.insert(boundingBox);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public addDynamicBoundingBox(boundingBox: BoundingBoxBase) {
|
public addDynamicBoundingBox(boundingBox: BoundingBoxBase) {
|
||||||
if (this.isTreeInitialized) {
|
|
||||||
this.staticBoundingBoxes.insert(boundingBox);
|
|
||||||
} else {
|
|
||||||
this.dynamicBoundingBoxes.insert(boundingBox);
|
this.dynamicBoundingBoxes.insert(boundingBox);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public findIntersecting(box: BoundingBoxBase): Array<BoundingBoxBase> {
|
public findIntersecting(box: BoundingBoxBase): Array<BoundingBoxBase> {
|
||||||
return [
|
return [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue