Skip to content

Commit

Permalink
Merge pull request #59 from pmndrs/cylinder-type
Browse files Browse the repository at this point in the history
Set Cylynder.type correctly when creating a Cylinder
  • Loading branch information
marcofugaro committed Dec 24, 2020
2 parents d6ff48b + 1c70e10 commit 1372dc9
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 44 deletions.
52 changes: 46 additions & 6 deletions dist/cannon-es.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3326,7 +3326,7 @@ class Body extends EventTarget {
}

this.allowSleep = typeof options.allowSleep !== 'undefined' ? options.allowSleep : true;
this.sleepState = 0;
this.sleepState = Body.AWAKE;
this.sleepSpeedLimit = typeof options.sleepSpeedLimit !== 'undefined' ? options.sleepSpeedLimit : 0.1;
this.sleepTimeLimit = typeof options.sleepTimeLimit !== 'undefined' ? options.sleepTimeLimit : 1;
this.timeLastSleepy = 0;
Expand Down Expand Up @@ -3393,7 +3393,7 @@ class Body extends EventTarget {

wakeUp() {
const prevState = this.sleepState;
this.sleepState = 0;
this.sleepState = Body.AWAKE;
this.wakeUpAfterNarrowphase = false;

if (prevState === Body.SLEEPING) {
Expand Down Expand Up @@ -5042,6 +5042,7 @@ const Ray_intersectSphere_normal = new Vec3();
Ray.prototype[Shape.types.SPHERE] = Ray.prototype._intersectSphere;
const intersectConvex_normal = new Vec3();
const intersectConvex_vector = new Vec3();
Ray.prototype[Shape.types.CYLINDER] = Ray.prototype._intersectConvex;
Ray.prototype[Shape.types.CONVEXPOLYHEDRON] = Ray.prototype._intersectConvex;
const intersectTrimesh_normal = new Vec3();
const intersectTrimesh_localDirection = new Vec3();
Expand Down Expand Up @@ -7758,7 +7759,15 @@ const SPHSystem_update_u = new Vec3();
*/

class Cylinder extends ConvexPolyhedron {
constructor(radiusTop, radiusBottom, height, numSegments) {
constructor(radiusTop = 1, radiusBottom = 1, height = 1, numSegments = 8) {
if (radiusTop < 0) {
throw new Error('The cylinder radiusTop cannot be negative.');
}

if (radiusBottom < 0) {
throw new Error('The cylinder radiusBottom cannot be negative.');
}

const N = numSegments;
const vertices = [];
const axes = [];
Expand Down Expand Up @@ -7812,6 +7821,11 @@ class Cylinder extends ConvexPolyhedron {
faces,
axes
});
this.type = Shape.types.CYLINDER;
this.radiusTop = radiusTop;
this.radiusBottom = radiusBottom;
this.height = height;
this.numSegments = numSegments;
}

}
Expand Down Expand Up @@ -9711,6 +9725,10 @@ class Vec3Pool extends Pool {

}

// Naming rule: based of the order in SHAPE_TYPES,
// the first part of the method is formed by the
// shape type that comes before, in the second part
// there is the shape type that comes after in the SHAPE_TYPES list
const COLLISION_TYPES = {
sphereSphere: Shape.types.SPHERE,
spherePlane: Shape.types.SPHERE | Shape.types.PLANE,
Expand All @@ -9728,6 +9746,13 @@ const COLLISION_TYPES = {
planeParticle: Shape.types.PLANE | Shape.types.PARTICLE,
boxParticle: Shape.types.BOX | Shape.types.PARTICLE,
convexParticle: Shape.types.PARTICLE | Shape.types.CONVEXPOLYHEDRON,
cylinderCylinder: Shape.types.CYLINDER,
sphereCylinder: Shape.types.SPHERE | Shape.types.CYLINDER,
planeCylinder: Shape.types.PLANE | Shape.types.CYLINDER,
boxCylinder: Shape.types.BOX | Shape.types.CYLINDER,
convexCylinder: Shape.types.CONVEXPOLYHEDRON | Shape.types.CYLINDER,
heightfieldCylinder: Shape.types.HEIGHTFIELD | Shape.types.CYLINDER,
particleCylinder: Shape.types.PARTICLE | Shape.types.CYLINDER,
sphereTrimesh: Shape.types.SPHERE | Shape.types.TRIMESH,
planeTrimesh: Shape.types.PLANE | Shape.types.TRIMESH
};
Expand Down Expand Up @@ -10895,6 +10920,14 @@ class Narrowphase {
}
}

heightfieldCylinder(hfShape, convexShape, hfPos, convexPos, hfQuat, convexQuat, hfBody, convexBody, rsi, rsj, justTest) {
return this.convexHeightfield(convexShape, hfShape, convexPos, hfPos, convexQuat, hfQuat, convexBody, hfBody, rsi, rsj, justTest);
}

particleCylinder(si, sj, xi, xj, qi, qj, bi, bj, rsi, rsj, justTest) {
return this.convexParticle(sj, si, xj, xi, qj, qi, bj, bi, rsi, rsj, justTest);
}

sphereTrimesh(sphereShape, trimeshShape, spherePos, trimeshPos, sphereQuat, trimeshQuat, sphereBody, trimeshBody, rsi, rsj, justTest) {
const edgeVertexA = sphereTrimesh_edgeVertexA;
const edgeVertexB = sphereTrimesh_edgeVertexB;
Expand Down Expand Up @@ -11247,7 +11280,14 @@ const particlePlane_relpos = new Vec3();
const particlePlane_projected = new Vec3();
Narrowphase.prototype[COLLISION_TYPES.planeParticle] = Narrowphase.prototype.planeParticle;
const particleSphere_normal = new Vec3();
Narrowphase.prototype[COLLISION_TYPES.sphereParticle] = Narrowphase.prototype.sphereParticle; // WIP
Narrowphase.prototype[COLLISION_TYPES.sphereParticle] = Narrowphase.prototype.sphereParticle;
Narrowphase.prototype[COLLISION_TYPES.cylinderCylinder] = Narrowphase.prototype.convexConvex;
Narrowphase.prototype[COLLISION_TYPES.sphereCylinder] = Narrowphase.prototype.sphereConvex;
Narrowphase.prototype[COLLISION_TYPES.planeCylinder] = Narrowphase.prototype.planeConvex;
Narrowphase.prototype[COLLISION_TYPES.boxCylinder] = Narrowphase.prototype.boxConvex;
Narrowphase.prototype[COLLISION_TYPES.convexCylinder] = Narrowphase.prototype.convexConvex;
Narrowphase.prototype[COLLISION_TYPES.heightfieldCylinder] = Narrowphase.prototype.heightfieldCylinder;
Narrowphase.prototype[COLLISION_TYPES.particleCylinder] = Narrowphase.prototype.particleCylinder; // WIP

const cqj = new Quaternion();
const convexParticle_local = new Vec3();
Expand Down Expand Up @@ -11820,9 +11860,9 @@ class World extends EventTarget {
this.accumulator -= dt;
substeps++;

if (performance.now() - t0 > dt * 2 * 1000) {
if (performance.now() - t0 > dt * 1000) {
// The framerate is not interactive anymore.
// We are at half of the target framerate.
// We are below the target framerate.
// Better bail out.
break;
}
Expand Down
52 changes: 37 additions & 15 deletions dist/cannon-es.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ declare module "shapes/Shape" {
import type { Body } from "objects/Body";
import type { Material } from "material/Material";
export const SHAPE_TYPES: {
SPHERE: 1;
PLANE: 2;
BOX: 4;
COMPOUND: 8;
CONVEXPOLYHEDRON: 16;
HEIGHTFIELD: 32;
PARTICLE: 64;
CYLINDER: 128;
TRIMESH: 256;
readonly SPHERE: 1;
readonly PLANE: 2;
readonly BOX: 4;
readonly COMPOUND: 8;
readonly CONVEXPOLYHEDRON: 16;
readonly HEIGHTFIELD: 32;
readonly PARTICLE: 64;
readonly CYLINDER: 128;
readonly TRIMESH: 256;
};
export type ShapeType = typeof SHAPE_TYPES[keyof typeof SHAPE_TYPES];
export type ShapeOptions = {
Expand Down Expand Up @@ -568,6 +568,16 @@ declare module "shapes/Particle" {
calculateWorldAABB(pos: Vec3, quat: Quaternion, min: Vec3, max: Vec3): void;
}
}
declare module "shapes/Cylinder" {
import { ConvexPolyhedron } from "shapes/ConvexPolyhedron";
export class Cylinder extends ConvexPolyhedron {
radiusTop: number;
radiusBottom: number;
height: number;
numSegments: number;
constructor(radiusTop?: number, radiusBottom?: number, height?: number, numSegments?: number);
}
}
declare module "material/ContactMaterial" {
import type { Material } from "material/Material";
export type ContactMaterialOptions = {
Expand Down Expand Up @@ -606,6 +616,7 @@ declare module "world/Narrowphase" {
import type { Plane } from "shapes/Plane";
import type { Trimesh } from "shapes/Trimesh";
import type { Heightfield } from "shapes/Heightfield";
import { Cylinder } from "shapes/Cylinder";
import type { ContactMaterial } from "material/ContactMaterial";
import type { World } from "world/World";
export const COLLISION_TYPES: {
Expand All @@ -625,6 +636,13 @@ declare module "world/Narrowphase" {
planeParticle: 66;
boxParticle: 68;
convexParticle: 80;
cylinderCylinder: 128;
sphereCylinder: 129;
planeCylinder: 130;
boxCylinder: 132;
convexCylinder: 144;
heightfieldCylinder: 160;
particleCylinder: 192;
sphereTrimesh: 257;
planeTrimesh: 258;
};
Expand Down Expand Up @@ -654,6 +672,13 @@ declare module "world/Narrowphase" {
[COLLISION_TYPES.planeParticle]: typeof Narrowphase.prototype.planeParticle;
[COLLISION_TYPES.boxParticle]: typeof Narrowphase.prototype.boxParticle;
[COLLISION_TYPES.convexParticle]: typeof Narrowphase.prototype.convexParticle;
[COLLISION_TYPES.cylinderCylinder]: typeof Narrowphase.prototype.convexConvex;
[COLLISION_TYPES.sphereCylinder]: typeof Narrowphase.prototype.sphereConvex;
[COLLISION_TYPES.planeCylinder]: typeof Narrowphase.prototype.planeConvex;
[COLLISION_TYPES.boxCylinder]: typeof Narrowphase.prototype.boxConvex;
[COLLISION_TYPES.convexCylinder]: typeof Narrowphase.prototype.convexConvex;
[COLLISION_TYPES.heightfieldCylinder]: typeof Narrowphase.prototype.heightfieldCylinder;
[COLLISION_TYPES.particleCylinder]: typeof Narrowphase.prototype.particleCylinder;
[COLLISION_TYPES.sphereTrimesh]: typeof Narrowphase.prototype.sphereTrimesh;
[COLLISION_TYPES.planeTrimesh]: typeof Narrowphase.prototype.planeTrimesh;
constructor(world: World);
Expand All @@ -677,6 +702,8 @@ declare module "world/Narrowphase" {
planeParticle(sj: Plane, si: Particle, xj: Vec3, xi: Vec3, qj: Quaternion, qi: Quaternion, bj: Body, bi: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
boxParticle(si: Box, sj: Particle, xi: Vec3, xj: Vec3, qi: Quaternion, qj: Quaternion, bi: Body, bj: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
convexParticle(sj: ConvexPolyhedron, si: Particle, xj: Vec3, xi: Vec3, qj: Quaternion, qi: Quaternion, bj: Body, bi: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
heightfieldCylinder(hfShape: Heightfield, convexShape: Cylinder, hfPos: Vec3, convexPos: Vec3, hfQuat: Quaternion, convexQuat: Quaternion, hfBody: Body, convexBody: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
particleCylinder(si: Particle, sj: Cylinder, xi: Vec3, xj: Vec3, qi: Quaternion, qj: Quaternion, bi: Body, bj: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
sphereTrimesh(sphereShape: Sphere, trimeshShape: Trimesh, spherePos: Vec3, trimeshPos: Vec3, sphereQuat: Quaternion, trimeshQuat: Quaternion, sphereBody: Body, trimeshBody: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
planeTrimesh(planeShape: Plane, trimeshShape: Trimesh, planePos: Vec3, trimeshPos: Vec3, planeQuat: Quaternion, trimeshQuat: Quaternion, planeBody: Body, trimeshBody: Body, rsi?: Shape | null, rsj?: Shape | null, justTest?: boolean): true | void;
}
Expand Down Expand Up @@ -883,6 +910,7 @@ declare module "collision/Ray" {
[Shape.types.SPHERE]: typeof Ray.prototype._intersectSphere;
[Shape.types.PLANE]: typeof Ray.prototype._intersectPlane;
[Shape.types.BOX]: typeof Ray.prototype._intersectBox;
[Shape.types.CYLINDER]: typeof Ray.prototype._intersectConvex;
[Shape.types.CONVEXPOLYHEDRON]: typeof Ray.prototype._intersectConvex;
[Shape.types.HEIGHTFIELD]: typeof Ray.prototype._intersectHeightfield;
[Shape.types.TRIMESH]: typeof Ray.prototype._intersectTrimesh;
Expand Down Expand Up @@ -1478,12 +1506,6 @@ declare module "objects/SPHSystem" {
nablaw(r: number): number;
}
}
declare module "shapes/Cylinder" {
import { ConvexPolyhedron } from "shapes/ConvexPolyhedron";
export class Cylinder extends ConvexPolyhedron {
constructor(radiusTop: number, radiusBottom: number, height: number, numSegments: number);
}
}
declare module "solver/SplitSolver" {
import { Solver } from "solver/Solver";
import { Body } from "objects/Body";
Expand Down
52 changes: 46 additions & 6 deletions dist/cannon-es.js
Original file line number Diff line number Diff line change
Expand Up @@ -3322,7 +3322,7 @@ class Body extends EventTarget {
}

this.allowSleep = typeof options.allowSleep !== 'undefined' ? options.allowSleep : true;
this.sleepState = 0;
this.sleepState = Body.AWAKE;
this.sleepSpeedLimit = typeof options.sleepSpeedLimit !== 'undefined' ? options.sleepSpeedLimit : 0.1;
this.sleepTimeLimit = typeof options.sleepTimeLimit !== 'undefined' ? options.sleepTimeLimit : 1;
this.timeLastSleepy = 0;
Expand Down Expand Up @@ -3389,7 +3389,7 @@ class Body extends EventTarget {

wakeUp() {
const prevState = this.sleepState;
this.sleepState = 0;
this.sleepState = Body.AWAKE;
this.wakeUpAfterNarrowphase = false;

if (prevState === Body.SLEEPING) {
Expand Down Expand Up @@ -5038,6 +5038,7 @@ const Ray_intersectSphere_normal = new Vec3();
Ray.prototype[Shape.types.SPHERE] = Ray.prototype._intersectSphere;
const intersectConvex_normal = new Vec3();
const intersectConvex_vector = new Vec3();
Ray.prototype[Shape.types.CYLINDER] = Ray.prototype._intersectConvex;
Ray.prototype[Shape.types.CONVEXPOLYHEDRON] = Ray.prototype._intersectConvex;
const intersectTrimesh_normal = new Vec3();
const intersectTrimesh_localDirection = new Vec3();
Expand Down Expand Up @@ -7754,7 +7755,15 @@ const SPHSystem_update_u = new Vec3();
*/

class Cylinder extends ConvexPolyhedron {
constructor(radiusTop, radiusBottom, height, numSegments) {
constructor(radiusTop = 1, radiusBottom = 1, height = 1, numSegments = 8) {
if (radiusTop < 0) {
throw new Error('The cylinder radiusTop cannot be negative.');
}

if (radiusBottom < 0) {
throw new Error('The cylinder radiusBottom cannot be negative.');
}

const N = numSegments;
const vertices = [];
const axes = [];
Expand Down Expand Up @@ -7808,6 +7817,11 @@ class Cylinder extends ConvexPolyhedron {
faces,
axes
});
this.type = Shape.types.CYLINDER;
this.radiusTop = radiusTop;
this.radiusBottom = radiusBottom;
this.height = height;
this.numSegments = numSegments;
}

}
Expand Down Expand Up @@ -9707,6 +9721,10 @@ class Vec3Pool extends Pool {

}

// Naming rule: based of the order in SHAPE_TYPES,
// the first part of the method is formed by the
// shape type that comes before, in the second part
// there is the shape type that comes after in the SHAPE_TYPES list
const COLLISION_TYPES = {
sphereSphere: Shape.types.SPHERE,
spherePlane: Shape.types.SPHERE | Shape.types.PLANE,
Expand All @@ -9724,6 +9742,13 @@ const COLLISION_TYPES = {
planeParticle: Shape.types.PLANE | Shape.types.PARTICLE,
boxParticle: Shape.types.BOX | Shape.types.PARTICLE,
convexParticle: Shape.types.PARTICLE | Shape.types.CONVEXPOLYHEDRON,
cylinderCylinder: Shape.types.CYLINDER,
sphereCylinder: Shape.types.SPHERE | Shape.types.CYLINDER,
planeCylinder: Shape.types.PLANE | Shape.types.CYLINDER,
boxCylinder: Shape.types.BOX | Shape.types.CYLINDER,
convexCylinder: Shape.types.CONVEXPOLYHEDRON | Shape.types.CYLINDER,
heightfieldCylinder: Shape.types.HEIGHTFIELD | Shape.types.CYLINDER,
particleCylinder: Shape.types.PARTICLE | Shape.types.CYLINDER,
sphereTrimesh: Shape.types.SPHERE | Shape.types.TRIMESH,
planeTrimesh: Shape.types.PLANE | Shape.types.TRIMESH
};
Expand Down Expand Up @@ -10891,6 +10916,14 @@ class Narrowphase {
}
}

heightfieldCylinder(hfShape, convexShape, hfPos, convexPos, hfQuat, convexQuat, hfBody, convexBody, rsi, rsj, justTest) {
return this.convexHeightfield(convexShape, hfShape, convexPos, hfPos, convexQuat, hfQuat, convexBody, hfBody, rsi, rsj, justTest);
}

particleCylinder(si, sj, xi, xj, qi, qj, bi, bj, rsi, rsj, justTest) {
return this.convexParticle(sj, si, xj, xi, qj, qi, bj, bi, rsi, rsj, justTest);
}

sphereTrimesh(sphereShape, trimeshShape, spherePos, trimeshPos, sphereQuat, trimeshQuat, sphereBody, trimeshBody, rsi, rsj, justTest) {
const edgeVertexA = sphereTrimesh_edgeVertexA;
const edgeVertexB = sphereTrimesh_edgeVertexB;
Expand Down Expand Up @@ -11243,7 +11276,14 @@ const particlePlane_relpos = new Vec3();
const particlePlane_projected = new Vec3();
Narrowphase.prototype[COLLISION_TYPES.planeParticle] = Narrowphase.prototype.planeParticle;
const particleSphere_normal = new Vec3();
Narrowphase.prototype[COLLISION_TYPES.sphereParticle] = Narrowphase.prototype.sphereParticle; // WIP
Narrowphase.prototype[COLLISION_TYPES.sphereParticle] = Narrowphase.prototype.sphereParticle;
Narrowphase.prototype[COLLISION_TYPES.cylinderCylinder] = Narrowphase.prototype.convexConvex;
Narrowphase.prototype[COLLISION_TYPES.sphereCylinder] = Narrowphase.prototype.sphereConvex;
Narrowphase.prototype[COLLISION_TYPES.planeCylinder] = Narrowphase.prototype.planeConvex;
Narrowphase.prototype[COLLISION_TYPES.boxCylinder] = Narrowphase.prototype.boxConvex;
Narrowphase.prototype[COLLISION_TYPES.convexCylinder] = Narrowphase.prototype.convexConvex;
Narrowphase.prototype[COLLISION_TYPES.heightfieldCylinder] = Narrowphase.prototype.heightfieldCylinder;
Narrowphase.prototype[COLLISION_TYPES.particleCylinder] = Narrowphase.prototype.particleCylinder; // WIP

const cqj = new Quaternion();
const convexParticle_local = new Vec3();
Expand Down Expand Up @@ -11816,9 +11856,9 @@ class World extends EventTarget {
this.accumulator -= dt;
substeps++;

if (performance.now() - t0 > dt * 2 * 1000) {
if (performance.now() - t0 > dt * 1000) {
// The framerate is not interactive anymore.
// We are at half of the target framerate.
// We are below the target framerate.
// Better bail out.
break;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/convex.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
world.addBody(tetraBody)
demo.addVisual(tetraBody)

// ConvexPolyhedron cylinder shape
// The Cylinder is a ConvexPolyhedron under the hood
const height = 2
const radius = 0.5
const detail = 20
Expand Down
4 changes: 1 addition & 3 deletions examples/shapes.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@

// Cylinder shape 2
const cylinderShape2 = new CANNON.Cylinder(size, size, size * 2, 10)
const quaternion = new CANNON.Quaternion()
quaternion.setFromEuler(Math.PI / 2, Math.PI / 2, 0)
cylinderShape2.transformAllPoints(new CANNON.Vec3(), quaternion)
const cylinderBody2 = new CANNON.Body({ mass })
cylinderBody2.addShape(cylinderShape2)
cylinderBody2.position.set(size * 2, size * 4 + 1, size * 2)
cylinderBody2.quaternion.setFromEuler(Math.PI / 2, Math.PI / 2, 0)
world.addBody(cylinderBody2)
demo.addVisual(cylinderBody2)

Expand Down
Loading

0 comments on commit 1372dc9

Please sign in to comment.