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

View File

@ -2,11 +2,12 @@ import {expect, test} from 'vitest';
import Schema from './schema.js'; import Schema from './schema.js';
test('defaults values', () => {
const compare = (specification, value) => { const compare = (specification, value) => {
expect(new Schema(specification).defaultValue()) expect(new Schema(specification).defaultValue())
.to.deep.equal(value); .to.deep.equal(value);
}; };
test('defaults values', () => {
[ [
'uint8', 'uint8',
'int8', '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', () => { test('validates a schema', () => {
[ [
'uint8', 'uint8',

View File

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