Coder Social home page Coder Social logo

three-body's Introduction

three-body

Simple gravity simulation using javascript to plot the motion of three objects on the web browser canvas.

Description

This is a very simplistic simulation that will calculate the motion of three objects in space based on the attractive force of gravity between the objects. This is analogous to the sun, the earth, and the moon, three objects in motion.

Motivation

Some friends were discussing a science fiction book involving orbital calculations of a system of three objects. The concept seemed like an entertaining way to spend an evening of coding, just for fun.

Physics calculations

The calculation involves advancing the simulation in small steps of time (dt). At each time interval, a new position, velocity vector and acceleration vector are calculated based on the positions of all three objects. A JavaScript setInterval() timer fires 100 timer events per second. A for loop is used to calculate 100 time intervals per timer event. The calculation time interval (dt) is configured to 10 seconds. Combined, this provides a real time motion display with approximately 100000:1 time acceleration. At the end of each for loop, a single pixel is drawn on the canvas for each of three objects.

To simplify the calculation, the position of each object has been constrained into a two dimensional X, Y space with the Z coordinate set to zero. This is similar to our solar system with the orbits of the sun and planets being roughly in planar.

How to run

The simulation includes a simple HTML file that loads a JavaScript module to draw X,Y coordinate points in the browser using the canvas API. The best way to view the simulation is using VSCode and a web browser. This was written using the Chrome web browser.

  • Clone repository
  • Open repository is VSCode.
  • Select the index.html file and open with the "Live Server" VSCode extension.
  • Buttons at the bottom of the web page will select various preset values.

The simulation may be paused by clicking anywhere on the browser canvas element. Each time the simulation is paused, the coordinates of all 3 objects are printed in the browser console. A timer will auto-pause the simulation after 60 seconds to avoid unnecessary CPU usage.

three-body's People

Contributors

cotarr 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.