Giter Club home page Giter Club logo

bouncing-ball's Introduction

Bouncing Balls

Simple bouncing balls simulation using plain JavaScript. Drawing in HTML Canvas, also plain CSS is used. (without third-part frameworks/code)

Try it here

Description

  • With a mouse click on the canvas a new ball is created, aim with holding the mouse down and moving it (drag the mouse further from the start point for greater speed), shoot the new ball with releasing the mouse button. (also touch screens/devices are supported, the same rules are used for touch events)
  • The canvas is updated (redrawn) 60 times in 1 second. (60 fps)
  • The canvas is responsive, with help from CSS media queries.
  • Because of that, the whole physics engine (all maths and logics inside) works with local coordinates/units. The local width is always 100 local units, and the height is always 66.6667 local units. (because the canvas ratio is 3:2)
  • The simulation is not a 100% real-world simulation, because there are many more factors for moving/colliding in the real world like the ball spinning, the softness of balls, the type of walls, even the weather, and sound waves have influence in the real world.
  • More description of the physics you can find inside the code, for example, when the balls collide these formulas are used, link.
  • Known issue: In "vertical" space/direction when the bottom is full with balls (when there is no space for a new ball) adding a new ball will make all balls go crazy (jumping randomly). This is because the balls will always collide and won't lose energy from colliding (at this moment I'm not sure how to solve this).

Repo structure

  • images - several gifs from the simulations
  • test - unit tests, Mocha framework is used for unit testing.This is how to run these tests using Mocha:
    • install NodeJS
    • install mocha in this project npm install mocha
    • run the tests from this project npm test
  • src - the source code of the application
    • index.html - a simple HTML page, JS and CSS files are imported and the choices and canvas are defined here
    • css/styles.css - used to define media queries (for responsiveness), and other very simple CSS rules
    • js/choice.js - used to initialize and stop the simulation (using the values from the choices)
    • js/bouncing-balls.js - handles the screen interaction (mouse and touch events) and draws in the canvas (some kind of mini game engine)
    • js/balls.js - balls collision and movement logics/physics
    • js/vector2d.js - 2 dimensional vector class, all vector related things are located here

License

This project is licensed under the MIT - see the LICENSE file for details

bouncing-ball's People

Contributors

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