Giter Site home page Giter Site logo

babylonjs-jolt-physics-plugin's Introduction

This plugin uses Jolt Physics JS (https://github.com/jrouwe/JoltPhysics.js) The plugin currently only uses the BabylonJS V1 Physics protocol. All code is maintained to the best of my ability, but has no guarantees.

Demos:

View: https://phoenixillusion.github.io/babylonjs-jolt-physics-plugin/

(Details in README under respective folders)

The basic-examples folder ports many of the JoltPhysicsJS Examples. This folder is used to test feature parity with base Jolt Physics JS library.

The trenchbroom-examples folder demonstrate load *.MAP files created using TrenchBroom (https://github.com/TrenchBroom/TrenchBroom) . These produce mainly brush-based levels.

The gltf-extras folder is a personal project, serializing Jolt physics constraint/configuration data into the extras parameter of a GLTF file. It is paired with a Unity extension that uses com.unity.cloud.gltfast to markup GameObjects with jolt-specific collision and constraint data, as well as places markup, configuration, and other data in the UI widgets and editor panels.

Basic Usage:

For most behavior, the JoltJS Physics Plugin is a drop-in replacement for basic Physics functionality, requiring only scene.enablePhysics be used to enable it.

    import { JoltJSPlugin } from  '@phoenixillusion/babylonjs-jolt-plugin';

    const engine = new Engine(this.canvas, true);
    const scene = new Scene(engine);
    scene.enablePhysics(new Vector3(0, -9.8, 0), await JoltJSPlugin.loadPlugin())
  
    const camera = new FlyCamera('camera1', new Vector3(0, 15, 30), scene);
    camera.setTarget(new Vector3(0, 10, 0));
    camera.attachControl(true);

    const light = new DirectionalLight('light', new Vector3(-1, -3, 0), scene);
    light.position.set(10, 10, 5);
    light.intensity = 0.7;

    const ground = MeshBuilder.CreateGround('ground', { width: 100, height: 100 });
    new PhysicsImpostor(ground, PhysicsImpostor.BoxImpostor, {
      mass: 0,
      friction: 0,
      restitution: 0.5
    })
    const sphere = MeshBuilder.CreateSphere('sphere', { diameter: 4, segments: 32 });
    sphere.position.set(0, 10, 0);
    new PhysicsImpostor(sphere, PhysicsImpostor.SphereImpostor, {
      mass: 5,
      friction: 0,
      restitution: 0.7
    });
    engine.runRenderLoop(() => {
        scene.render();
    });

Additional Features

See FEATURES.md

Jolt Version and Configuration (advanced)

The existing build is configured to load a fixed version of Jolt's wasm-compat library using https://www.unpkg.com/browse/jolt-physics/ . In cases where you wish to use another wasm binary, or where upgrading to a newer version of Jolt is not a breaking change, you can configure and override the Jolt import using the '@phoenixillusion/babylonjs-jolt-plugin/import' setJoltModule interface

    import { JoltJSPlugin } from  '@phoenixillusion/babylonjs-jolt-plugin';
    import initJolt from '<custom jolt build or version>'
    import setJoltModule from  '@phoenixillusion/babylonjs-jolt-plugin/import';

    setJoltModule(initJolt);
    const engine = new Engine(this.canvas, true);
    const scene = new Scene(engine);
    scene.enablePhysics(new Vector3(0, -9.8, 0), await JoltJSPlugin.loadPlugin())

Although most Jolt internals are abstracted away from the plugin, using the BabylonJS physics interface and custom utilities, the raw Jolt module remains accessible if needed. The shared instantiation of the Jolt module Jolt will not be available until after the first instance of a plugin has loaded. After this, you may access any internal Jolt features if needed.

import Jolt  from '@phoenixillusion/babylonjs-jolt-plugin/import';

babylonjs-jolt-physics-plugin's People

Contributors

phoenixillusion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

babylonjs-jolt-physics-plugin's Issues

Demo: character virtual - sensor enabled action buttons

Demo:

  • character virtual
    • gui reset button, respawn and reset level
    • on screen "action" button
    • change when near door (sensor region), action button opens door
      • motor with min/max angle
    • console/ computer
      • rotate bridge along your axis to bridge gap
    • button near draw-bridge
      • lower draw bridge to bridge secondary gap
    • pickup item
      • token/ key sensor that hides/disappear model on intersect with player
      • enable door if holding key

API/Demo: save state

API: Expose save/load engine state

Demo: Deterministic mode

Setup multiple items in scene
Provide in-screen data of exact x-y-z and time for specified items

Keep recording of system at every X frames

Allow enabling pre-determined actions on items at given timestamps

Allow reloading to specific sections and inserting given action

Run to timestamp Z and confirm systems bodies are at pre-calculated x-y-z locations

Demo: Memory/Cleanup

Create widget tracking memory usage

  • current/maximum memory
  • track at labels and times

Create master demo

  • link to all other scenes
  • track memory before during and after running each scene
  • confirm full cleanup for each demo

Demo: Vehicle - extra examples

Use exposed new api for following features:

  • tachometer- current rpm with min/max for auto gear
  • speed
  • gear - reverse, neutral, 1st,2nd,3rd
  • boost/nitro - runtime modify to higher speed

Extra Demos

  • truck with trailer

    • add extra vehicle in neutral behind vehicle, connect with point constraint
  • go kart - small vehicle with small track. Support quick turn around on wheels. Pickup items and use accelerator track-segments to get boost.

Demo: net-code/rollback/finite state machine with 2 physics systems

Dependent on save/load state API

Demo:
Two scenes with 2 plugins

Ensure both load same system

Ensure both have a rolling ring-buffer of save states

Assume one Body per player

Have a recording of player 1 and player 2 actions for given time window

Start demo

Each scene will play its own recording
Each scene will send its own instructions to the other scene with a random time offset in the past, greater than zero but less than the ring-buffer size

At the end of the demo, once the last instruction is received by both scenes, confirm both demos are both matching a fixed pre-determined outcome and each others own internal state

API/Demo - gravity influence

Expose per-body gravity influence

Demo:

  • press button to switch gravity from Y axis to X axis

Demo:

  • vehicle:
    • when in sensor volume, shift gravity on vehicle
    • oriented camera "up" counter to gravity
    • drive from ground on to wall and back
  • character virtual : wall walk
    • mirror same logic as above.
    • does not use api, since character virtual does not use system gravity by default
  • character virtual : planet/galaxy
    • adopt above logic but extend to mini worlds with jump/ launch between them
    • disable mid-air movement when in launch between planets

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.