Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated features #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Deprecate material name & all other deprecations
  • Loading branch information
bjornstar committed Dec 27, 2021
commit cb4e833ee83cd9bc15ac4b73ac9d943d13fa0e16
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
<div class="caption">
<a href="examples/${name}" class="name">${readableName}</a>
<a
href="https://github.com/react-spring/cannon-es/blob/master/examples/${name}.html"
href="https://github.com/pmndrs/cannon-es/blob/master/examples/${name}.html"
target="_blank"
class="github"
title="View source code on GitHub"
Expand Down Expand Up @@ -204,7 +204,7 @@
<h1 class="title">
cannon-es
<a
href="https://github.com/react-spring/cannon-es"
href="https://github.com/pmndrs/cannon-es"
target="_blank"
class="github"
title="View source code for cannon-es on GitHub">
Expand Down
3 changes: 1 addition & 2 deletions src/cannon-es.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './collision/ObjectCollisionMatrix'
export * from './collision/AABB'
export * from './collision/ArrayCollisionMatrix'
export * from './collision/Broadphase'
export * from './collision/CollisionMatrix'
export * from './collision/GridBroadphase'
export * from './collision/NaiveBroadphase'
export * from './collision/Ray'
Expand Down
3 changes: 1 addition & 2 deletions src/collision/AABB.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AABB } from './AABB'
import { Ray } from './Ray'
import { Vec3 } from '../math/Vec3'
import { Quaternion } from '../math/Quaternion'
import { Transform } from '../math/Transform'
import { Vec3 } from '../math/Vec3'

describe('AABB', () => {
test('construct', () => {
Expand Down
55 changes: 21 additions & 34 deletions src/collision/Broadphase.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
import { Body } from '../objects/Body'
import { Vec3 } from '../math/Vec3'
import { Quaternion } from '../math/Quaternion'

import type { AABB } from '../collision/AABB'
import type { World } from '../world/World'

/**
* Base class for broadphase implementations
* @author schteppe
*/
export class Broadphase {
export abstract class Broadphase {
/**
* The world to search for collisions in.
*/
world: World | null
world: World | null = null
/**
* If set to true, the broadphase uses bounding boxes for intersection tests, else it uses bounding spheres.
*/
useBoundingBoxes: boolean
useBoundingBoxes = false
/**
* Set to true if the objects in the world moved.
*/
dirty: boolean

constructor() {
this.world = null
this.useBoundingBoxes = false
this.dirty = true
}
dirty = true

/**
* Get the collision pairs from the world
* @param world The world to search in
* @param p1 Empty array to be filled with body objects
* @param p2 Empty array to be filled with body objects
*/
collisionPairs(world: World, p1: Body[], p2: Body[]): void {
throw new Error('collisionPairs not implemented for this BroadPhase class!')
}
abstract collisionPairs(world: World, p1: Body[], p2: Body[]): void

/**
* Check if a body pair needs to be intersection tested at all.
Expand Down Expand Up @@ -128,23 +120,22 @@ export class Broadphase {
const id1 = p1[i].id
const id2 = p2[i].id
const key = id1 < id2 ? `${id1},${id2}` : `${id2},${id1}`
t[key] = i
t.indexes[key] = i
t.keys.push(key)
}

for (let i = 0; i !== t.keys.length; i++) {
const key = t.keys.pop()
const pairIndex = t[key]
const key = t.keys[i]
const pairIndex = t.indexes[key]
pairs1.push(p1[pairIndex])
pairs2.push(p2[pairIndex])
delete t[key]
delete t.indexes[key]
}

t.keys.length = 0
}

/**
* To be implemented by subcasses
*/
setWorld(world: World): void {}
abstract setWorld(world: World): void

/**
* Check if the bounding spheres of two bodies overlap.
Expand All @@ -158,23 +149,19 @@ export class Broadphase {
}

/**
* Returns all the bodies within the AABB.
* Returns all the bodies within an AABB.
* @param result An array to store resulting bodies in.
*/
aabbQuery(world: World, aabb: AABB, result: Body[]): Body[] {
console.warn('.aabbQuery is not implemented in this Broadphase subclass.')
return []
}
abstract aabbQuery(world: World, aabb: AABB, result: Body[]): Body[]
}

// Temp objects
const Broadphase_collisionPairs_r = new Vec3()

const Broadphase_collisionPairs_normal = new Vec3()
const Broadphase_collisionPairs_quat = new Quaternion()
const Broadphase_collisionPairs_relpos = new Vec3()

const Broadphase_makePairsUnique_temp: Record<string, any> = { keys: [] }
type TempPairs = {
indexes: Record<string, number>
keys: string[]
}
const Broadphase_makePairsUnique_temp: TempPairs = { indexes: {}, keys: [] }
const Broadphase_makePairsUnique_p1: Body[] = []
const Broadphase_makePairsUnique_p2: Body[] = []

const bsc_dist = new Vec3()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Body } from '../objects/Body'
import type { Body } from '../../objects/Body'

/**
* Collision "matrix".
Expand All @@ -8,11 +8,7 @@ export class ArrayCollisionMatrix {
/**
* The matrix storage.
*/
matrix: number[]

constructor() {
this.matrix = []
}
matrix: number[] = []

/**
* Get an element
Expand Down
47 changes: 47 additions & 0 deletions src/collision/CollisionMatrix/ObjectCollisionMatrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Body } from '../../objects/Body'

/**
* Records what objects are colliding with each other
*/
export class ObjectCollisionMatrix {
/**
* The matrix storage.
*/
matrix: Record<string, boolean> = {}

get(bi: Body, bj: Body): boolean {
const { id: i } = bi
const { id: j } = bj

const key = i < j ? `${i}-${j}` : `${j}-${i}`

return key in this.matrix
}

set(bi: Body, bj: Body, value: boolean): void {
const { id: i } = bi
const { id: j } = bj

const key = i < j ? `${i}-${j}` : `${j}-${i}`

if (value) {
this.matrix[key] = true
} else {
delete this.matrix[key]
}
}

/**
* Empty the matrix
*/
reset(): void {
this.matrix = {}
}

/**
* Set max number of objects
*/
setNumObjects(): void {
/**/
}
}
7 changes: 7 additions & 0 deletions src/collision/CollisionMatrix/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ArrayCollisionMatrix } from './ArrayCollisionMatrix'
import { ObjectCollisionMatrix } from './ObjectCollisionMatrix'

export type CollisionMatrix = ArrayCollisionMatrix | ObjectCollisionMatrix
export type CollisionMatrixClass = typeof ArrayCollisionMatrix | typeof ObjectCollisionMatrix

export { ArrayCollisionMatrix, ObjectCollisionMatrix }
36 changes: 13 additions & 23 deletions src/collision/GridBroadphase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AABB } from './AABB'
import { Broadphase } from '../collision/Broadphase'
import { Vec3 } from '../math/Vec3'
import { Shape } from '../shapes/Shape'

import type { Body } from '../objects/Body'
import type { Sphere } from '../shapes/Sphere'
import type { Plane } from '../shapes/Plane'
Expand Down Expand Up @@ -77,8 +79,6 @@ export class GridBroadphase extends Broadphase {
* Get all the collision pairs in the physics world
*/
collisionPairs(world: World, pairs1: Body[], pairs2: Body[]): void {
const N = world.numObjects()
const bodies = world.bodies
const max = this.aabbMax
const min = this.aabbMin
const nx = this.nx
Expand All @@ -104,12 +104,8 @@ export class GridBroadphase extends Broadphase {

const binRadius = Math.sqrt(binsizeX * binsizeX + binsizeY * binsizeY + binsizeZ * binsizeZ) * 0.5

const types = Shape.types
const SPHERE = types.SPHERE
const PLANE = types.PLANE
const BOX = types.BOX
const COMPOUND = types.COMPOUND
const CONVEXPOLYHEDRON = types.CONVEXPOLYHEDRON
const SPHERE = Shape.types.SPHERE
const PLANE = Shape.types.PLANE
const bins = this.bins
const binLengths = this.binLengths
const Nbins = this.bins.length
Expand Down Expand Up @@ -178,8 +174,8 @@ export class GridBroadphase extends Broadphase {
}

// Put all bodies into the bins
for (let i = 0; i !== N; i++) {
const bi = bodies[i]
for (let i = 0; i !== world.bodies.length; i++) {
const bi = world.bodies[i]
const si = bi.shapes[0]

switch (si.type) {
Expand Down Expand Up @@ -264,21 +260,15 @@ export class GridBroadphase extends Broadphase {
}
}

// for (let zi = 0, zoff=0; zi < nz; zi++, zoff+= zstep) {
// console.log("layer "+zi);
// for (let yi = 0, yoff=0; yi < ny; yi++, yoff += ystep) {
// const row = '';
// for (let xi = 0, xoff=0; xi < nx; xi++, xoff += xstep) {
// const idx = xoff + yoff + zoff;
// row += ' ' + binLengths[idx];
// }
// console.log(row);
// }
// }

this.makePairsUnique(pairs1, pairs2)
}

aabbQuery(world: World, aabb: AABB, result: Body[] = []): Body[] {
return result
}
setWorld() {
/**/
}
}

const GridBroadphase_collisionPairs_d = new Vec3()
const GridBroadphase_collisionPairs_binPos = new Vec3()
11 changes: 11 additions & 0 deletions src/collision/NaiveBroadphase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NaiveBroadphase } from './NaiveBroadphase'

describe('NaiveBroadphase', () => {
test('construct', () => {
const nb = new NaiveBroadphase()
expect(nb).toBeDefined()
expect(nb.dirty).toBe(true)
expect(nb.useBoundingBoxes).toBe(false)
expect(nb.world).toBe(null)
})
})
12 changes: 5 additions & 7 deletions src/collision/NaiveBroadphase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Broadphase } from '../collision/Broadphase'

import type { AABB } from '../collision/AABB'
import type { Body } from '../objects/Body'
import type { World } from '../world/World'
Expand All @@ -9,13 +10,6 @@ import type { World } from '../world/World'
* The naive broadphase looks at all possible pairs without restriction, therefore it has complexity N^2 _(which is bad)_
*/
export class NaiveBroadphase extends Broadphase {
/**
* @todo Remove useless constructor
*/
constructor() {
super()
}

/**
* Get all the collision pairs in the physics world
*/
Expand Down Expand Up @@ -60,4 +54,8 @@ export class NaiveBroadphase extends Broadphase {

return result
}

setWorld() {
/**/
}
}
Loading