Giter VIP home page Giter VIP logo

akihiko47 / verlet-physics-engine Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 43 KB

Very simple verlet Physics Engine. Realization of particles, collisions between particles, springs, solid joints and anchors. It is possible to implement: soft bodies, rigid bodies, ropes, cloth, etc.. Drawing with canvas.

Home Page: https://akihiko47.github.io/Verlet-Physics-Engine/

JavaScript 81.57% HTML 18.43%
cloth-simulation js particles physics-engine rigid-bodies soft-bodies verlet-integration

verlet-physics-engine's Introduction

⚛️ JS physics engine ⚛️

logo

A simple physics engine based on verlet integration. Realization of particles, collisions between particles, springs, solid joints and anchors. It is possible to implement: soft bodies, rigid bodies, ropes, etc. You can see examples here.

Files

There are two files:

  1. classes.js This file implements the particle, spring, solid joints, and mouse classes.
  2. utils.js This file contains help functions such as: particle rendering, position updates, spring state updates, mouse position updates.

You need to add these 2 files to your project and you will be able to use all the functionality you need. An example of adding can be seen in the first example.

Classes

  1. Particle(x, y, radius=0, color="#ffffff", anchored=false)
  2. Spring(particle1, particle2, strength, damping, width=0, color="#ffffff")
  3. Joint(particle1, particle2, width=0, color="#ffffff")
  4. Mouse(canvas, x=0, y=0, pickRadius=50)

With these classes it is possible to implement most of the necessary things such as: soft bodies, rigid bodies, ropes, etc. An important concept is that instances of these classes of your program should be added to the following lists:

let particles = [];  // main array of particles
let springs = [];  // main array of springs
let joints = [];  // main array of rigid joints

After that, they can then be updated with the function update(particles, springs, joints, dt); Please check out the examples for more information.

Mouse class

You can create an instance of the mouse class using this line let mouse = new Mouse(canvas, 0, 0); After that, the mouse position on the canvas will be automatically updated, get it using these lines:

let m_x = mouse.x;
let m_y = mouse.y;
let m_down = mouse.down;

Main loop

Watch a simple example of the program and main loop in the first example. This implementation is not mandatory. Feel free to add your own implementation variants.

Usage

Just copy the example you need and add the necessary functions to add the bodies you want. Feel free to change the code of classes and functions for the task you need. These examples should serve as the main learning ground.

Contributions

Contributions are welcomed! 👋

verlet-physics-engine's People

Contributors

akihiko47 avatar

Stargazers

 avatar

Watchers

 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.