Giter Club home page Giter Club logo

bocha's Introduction

Bocha

Javascript 2d elastic collision

To run? just clone on your pc, run the server (take a look below) and run index.html.

Each ball has

  • Mass
  • Velocity
  • Friction
  • Radius
  • Position on screen

See, there is a solution to handle ball inside to ball!

(wait for animation) Gif

Take a look at "Two-dimensional collision with two moving objects" on: https://en.wikipedia.org/wiki/Elastic_collision

This formula can be wrote as a pseudo code below. Formula

See this pseudo code that I adapted:

// Just move balls not collideds

if not collided {
    ball1.move()
    ball2.move()
    return
}

// Discovery new velocities

theta1 = atan2(ball1.dy, ball1.dx) // Angle
theta2 = atan2(ball2.dy, ball2.dx) // Angle
phi = atan2(ball2.y - ball1.y, ball2.x - ball1.x) // Contact angle
m1 = ball1.mass
m2 = ball2.mass
v1 = sqrt(ball1.dx * ball1.dx + ball1.dy * ball1.dy) // Velocity
v2 = sqrt(ball2.dx * ball2.dx + ball2.dy * ball2.dy) // Velocity

dx1F = (v1 * cos(theta1 - phi) * (m1-m2) + 2*m2*v2*cos(theta2 - phi)) / (m1+m2) * cos(phi) + v1*sin(theta1-phi) * cos(phi+PI/2)
dy1F = (v1 * cos(theta1 - phi) * (m1-m2) + 2*m2*v2*cos(theta2 - phi)) / (m1+m2) * sin(phi) + v1*sin(theta1-phi) * sin(phi+PI/2)
dx2F = (v2 * cos(theta2 - phi) * (m2-m1) + 2*m1*v1*cos(theta1 - phi)) / (m1+m2) * cos(phi) + v2*sin(theta2-phi) * cos(phi+PI/2)
dy2F = (v2 * cos(theta2 - phi) * (m2-m1) + 2*m1*v1*cos(theta1 - phi)) / (m1+m2) * sin(phi) + v2*sin(theta2-phi) * sin(phi+PI/2)

// Apply new valocities

ball1.dx = dx1F
ball1.dy = dy1F                
ball2.dx = dx2F                
ball2.dy = dy2F

// If they are still collideds, throw ball to out

for (i = 0; i < 999; i++) {
    ball1.move()
    ball2.move()
    if !isCollided(ball1, ball2) break
}

Server

You can start the server by running:

cd server
npm i # Just first time
node main

bocha's People

Contributors

ivansansao avatar

Watchers

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