feat: collision filtering

This commit is contained in:
cha0s 2024-07-26 19:28:28 -05:00
parent 08dfe8ac29
commit ebf62613ef
3 changed files with 43 additions and 8 deletions

View File

@ -188,8 +188,18 @@ export default class Collider extends Component {
}
for (const i in aabbs) {
const aabb = aabbs[i];
const body = this.bodies[i];
for (const j in otherAabbs) {
const otherAabb = otherAabbs[j];
const otherBody = other.bodies[j];
if (body.group === otherBody.group && body.group < 0) {
continue;
}
if (body.group !== otherBody.group || 0 === body.group) {
if (0 === (otherBody.mask & body.bits) || 0 === (body.mask & otherBody.bits)) {
continue;
}
}
// todo accuracy
if (intersects(aabb, otherAabb)) {
intersections.push({
@ -244,6 +254,14 @@ export default class Collider extends Component {
}
}
async load(instance) {
for (const i in instance.bodies) {
instance.bodies[i] = {
...this.constructor.schema.constructor.defaultValue(
this.constructor.schema.specification.concrete.properties.bodies.concrete.subtype,
),
...instance.bodies[i],
};
}
instance.updateAabbs();
// heavy handed...
if ('undefined' !== typeof window) {
@ -270,15 +288,14 @@ export default class Collider extends Component {
subtype: {
type: 'object',
properties: {
bits: {defaultValue: 0x00000001, type: 'uint32'},
impassable: {type: 'uint8'},
group: {type: 'int32'},
mask: {defaultValue: 0xFFFFFFFF, type: 'uint32'},
points: {
type: 'array',
subtype: vector2d('int16'),
},
tags: {
type: 'array',
subtype: {type: 'string'},
},
unstoppable: {type: 'uint8'},
},
},

View File

@ -2,11 +2,12 @@ import {expect, test} from 'vitest';
import Schema from './schema.js';
test('defaults values', () => {
const compare = (specification, value) => {
expect(new Schema(specification).defaultValue())
.to.deep.equal(value);
};
test('defaults values', () => {
[
'uint8',
'int8',
@ -54,6 +55,22 @@ test('defaults values', () => {
);
});
test.todo('defaults nested values', () => {
compare(
{
type: 'array',
subtype: {
type: 'object',
properties: {
foo: {defaultValue: 'bar', type: 'string'},
},
},
},
[{}],
[{foo: 'bar'}],
);
});
test('validates a schema', () => {
[
'uint8',

View File

@ -14,6 +14,7 @@ for (let i = 0; i < N; ++i) {
Collider: {
bodies: [
{
group: -1,
points: [
{x: -2.5, y: -2.5},
{x: 14, y: -2.5},