How to use @cocos/cannon - 10 common examples

To help you get started, we’ve selected a few @cocos/cannon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cocos-creator / engine / cocos / physics / cannon / cannon-world.ts View on Github external
//  addContactMaterial (contactMaterial: ContactMaterial) {
    //     this._cannonWorld.addContactMaterial(contactMaterial._getImpl());
    // }

    addConstraint (constraint: CannonConstraint) {
        this._world.addConstraint(constraint.impl);
    }

    removeConstraint (constraint: CannonConstraint) {
        this._world.removeConstraint(constraint.impl);
    }
}

const from = new CANNON.Vec3();
const to = new CANNON.Vec3();
function setupFromAndTo (worldRay: ray, distance: number) {
    Vec3.copy(from, worldRay.o);
    worldRay.computeHit(to, distance);
}

const raycastOpt: CANNON.IRaycastOptions = {
    'checkCollisionResponse': false,
    'collisionFilterGroup': -1,
    'collisionFilterMask': -1,
    'skipBackFaces': false
}
github cocos-creator / engine / cocos / physics / cannon / cannon-world.ts View on Github external
}

    //  addContactMaterial (contactMaterial: ContactMaterial) {
    //     this._cannonWorld.addContactMaterial(contactMaterial._getImpl());
    // }

    addConstraint (constraint: CannonConstraint) {
        this._world.addConstraint(constraint.impl);
    }

    removeConstraint (constraint: CannonConstraint) {
        this._world.removeConstraint(constraint.impl);
    }
}

const from = new CANNON.Vec3();
const to = new CANNON.Vec3();
function setupFromAndTo (worldRay: ray, distance: number) {
    Vec3.copy(from, worldRay.o);
    worldRay.computeHit(to, distance);
}

const raycastOpt: CANNON.IRaycastOptions = {
    'checkCollisionResponse': false,
    'collisionFilterGroup': -1,
    'collisionFilterMask': -1,
    'skipBackFaces': false
}
github cocos-creator / engine / cocos / physics / cannon / cannon-body.ts View on Github external
}

const CollisionEventObject = {
    type: 'onCollisionEnter' as ICollisionEventType,
    selfCollider: null,
    otherCollider: null,
    // selfRigidBody: null,
    // otherRigidBody: null,
    contacts: [] as any,
};

const contactsPool = [] as any;

const _tCannonV1 = new CANNON.Vec3();

const _tCannonV2 = new CANNON.Vec3();
github cocos-creator / engine / cocos / physics / cannon / cannon-body.ts View on Github external
}

}

const CollisionEventObject = {
    type: 'onCollisionEnter' as ICollisionEventType,
    selfCollider: null,
    otherCollider: null,
    // selfRigidBody: null,
    // otherRigidBody: null,
    contacts: [] as any,
};

const contactsPool = [] as any;

const _tCannonV1 = new CANNON.Vec3();

const _tCannonV2 = new CANNON.Vec3();
github cocos-creator / engine / cocos / physics / cannon / cannon-body.ts View on Github external
constructor (options?: ICreateBodyOptions) {
        options = options || {};
        this._name = options.name || '';
        this._body = new CANNON.Body({
            type: ERigidBodyType.DYNAMIC,
        });
        setWrap(this._body, this);

        this._body.sleepSpeedLimit = 0.1; // Body will feel sleepy if speed<1 (speed == norm of velocity)
        this._body.sleepTimeLimit = 1; // Body falls asleep after 1s of sleepiness

        this._onCollidedListener = this._onCollided.bind(this);
    }
github cocos-creator / engine / cocos / physics / cannon / cannon-world.ts View on Github external
set allowSleep (v: boolean) {
        this._world.allowSleep = v;
    }

    set gravity (gravity: Vec3) {
        Vec3.copy(this._world.gravity, gravity);
    }

    // get defaultContactMaterial () {
    //     return this._defaultContactMaterial;
    // }

    readonly bodies: CannonSharedBody[] = [];

    private _world: CANNON.World;
    private _raycastResult = new CANNON.RaycastResult();

    constructor () {
        this._world = new CANNON.World();
        this._world.broadphase = new CANNON.NaiveBroadphase();
    }

    step (deltaTime: number, timeSinceLastCalled?: number, maxSubStep?: number) {
        // sync scene to physics
        for (let i = 0; i < this.bodies.length; i++) {
            this.bodies[i].syncSceneToPhysics();
        }

        this._world.step(deltaTime, timeSinceLastCalled, maxSubStep);

        // sync physics to scene
        for (let i = 0; i < this.bodies.length; i++) {
github cocos-creator / engine / cocos / physics / cannon / constraint / cannon-point-to-point-constraint.ts View on Github external
constructor (first: CannonSharedBody, firstPivot: Vec3, second: CannonSharedBody, secondPivot: Vec3, options?: any) {
        super(new CANNON.PointToPointConstraint(
            first.body,
            Vec3.copy(new CANNON.Vec3(), firstPivot),
            second.body,
            Vec3.copy(new CANNON.Vec3(), secondPivot),
            options === undefined ? undefined : options.maxForce));
    }
}
github cocos-creator / engine / cocos / physics / cannon / constraint / cannon-distance-constraint.ts View on Github external
constructor (first: any, second: any, distance?: number, options?: IDistanceConstraintOptions) {
        super(new CANNON.DistanceConstraint(
            first.impl,
            second.impl,
            distance!,
            options === undefined ? undefined : options.maxForce));
    }
}
github cocos-creator / engine / cocos / physics / cannon / constraint / cannon-lock-constraint.ts View on Github external
constructor (first: CannonSharedBody, second: CannonSharedBody, distance?: number, options?: IDistanceConstraintOptions) {
        super(new CANNON.DistanceConstraint(
            first.body,
            second.body,
            distance!,
            options === undefined ? undefined : options.maxForce));
    }
}
github cocos-creator / engine / cocos / physics / cannon / shapes / cannon-box-shape.ts View on Github external
constructor (size: Vec3) {
        super();
        Vec3.multiplyScalar(this.halfExtent, size, 0.5);
        this._shape = new CANNON.Box(this.halfExtent.clone());
    }