Giter VIP home page Giter VIP logo

ammojs-typed's Introduction

Ammo.js Typed

This project provides the Ammo.js modules with typescript definitions.

Installation

Use npm or yarn to install this version of ammojs from npm

$ npm install ammojs-typed

or from github

$ npm install github:giniedp/ammojs-typed

Usage

Ammo as window global

Configure your tsconfig.json to lookup the ambient types

  "typeRoots": ["node_modules/ammojs-typed/ammo/ambient"]

Then at some point require ammo.js (depends on your build chain)

require('ammojs-typed')

or reference the script

<script src="./ammo.js">

And use the global Ammo object

Ammo().then(() => {
  new Ammo.btVector3(1, 2, 3)
})

Ammo as es6 module import

You probably need to set the following compilerOptions in tsconfig.json

  "allowSyntheticDefaultImports": true,
  "esModuleInterop": true

Then import ammo like this

import Ammo from 'ammojs-typed'

This works but be cautious here. The default import gives you the bootstrap function. After bootstrapping the api is not available through the Ammo symbol by default.

Ammo().then(api => {
  const v1 = new api.btVector3(1, 2, 3)
  const v2 = new Ammo.btVector3(1, 2, 3) // <-- runtime error here
})

You can work around that by booting like this

Ammo(Ammo).then(() => {
  const v2 = new Ammo.btVector3(1, 2, 3) // <-- works
})

Ammo as dynamic import

Enable same compilerOptions as above

import('./ammo.js')                        // use dynamic import
  .then((Module) => Module.default())      // bootstrap ammo.js
  .then((ammo) => {
    const v1 = new ammo.btVector3(1, 2, 3) // use ammo here
  })

Since typescript 3.8 you can use type only imports. So with dynamic imports you can safely import ammo.js types, without including them in you bundle like this

import type Ammo from './ammo.js'

import('./ammo.js')                        // use dynamic import
  .then((Module) => Module.default())      // bootstrap ammo.js
  .then((ammo) => {
    let v1: Ammo.btVector3 = null
    // ...
    v1 = v
  })

Generate .d.ts files

Clone this repository and install node dependencies

git clone [email protected]:giniedp/ammojs-typed.git
cd ammojs-typed
npm install

Place the ammo.idl and ammo.js into the ./ammo folder. To download the latest version from the ammo.js repository run

$ npm run download

Make your adjustments to the IDL file if needed (see below) and run

$ npm run generate

This will parse the ./ammo/ammo.idl and generate a ./ammo/ammo.d.ts as well as ./ammo/ambient/ammo.d.ts

Manual IDL adjustments

The btVector4 implements the shape of btVector3 which causes a signature mismatch of the setValue method which typescript complains about. Add the following to the btVector4

+void setValue(float x, float y, float z);

The btDbvtBroadphase should derive from btBroadphaseInterface

-interface btDbvtBroadphase {
+interface btDbvtBroadphase: btBroadphaseInterface {

References

ammojs-typed's People

Contributors

giniedp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.