Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //change masses,speed and framerate
- var digits = 2;
- var s=-2;
- var extraframes = 1;
- counter=0;
- class Block {
- constructor(x, s, m, v) {
- this.pos = x;
- this.mass = m;
- this.size = s;
- this.vel = v;
- }
- show() {
- fill(0, 0, 150)
- square(this.pos, height - this.size, this.size);
- textAlign(RIGHT)
- fill(0)
- text(counter / pow(10, digits - 1), width, 100)
- }
- move() {
- this.pos = this.pos + this.vel;
- }
- bounce() {
- if (this.pos < 0) {
- counter++
- this.vel *= -1;
- }
- }
- collide(other) {
- if (this.pos + this.size > other.pos) {
- counter++;
- this.temp = this.vel;
- this.sum1 = (this.mass - other.mass) / (this.mass + other.mass) * this.vel
- this.sum2 = (2 * other.mass) / (this.mass + other.mass) * other.vel
- this.vel = this.sum1 + this.sum2
- this.sum1 = (2 * this.mass) / (this.mass + other.mass) * this.temp
- this.sum2 = (other.mass - this.mass) / (this.mass + other.mass) * other.vel
- other.vel = this.sum1 + this.sum2
- }
- }
- finished(that){
- if(this.vel>that.vel && this.vel>0){
- noLoop()
- print("finsihed!")
- }
- }
- }
- function setup() {
- frameRate(60);
- createCanvas(600, 300);
- //x,s,m,v
- b1 = new Block(width / 2, 100, pow(100, (digits - 1)), s/extraframes);
- b2 = new Block(width / 5, 33, 1, 0);
- }
- function draw() {
- background(33, 40, 40);
- for (let j = 0; j < extraframes; j++) {
- b1.move();
- b2.move();
- b1.bounce();
- b2.bounce();
- b2.collide(b1);
- //b1.finished(b2);
- }
- b1.show();
- b2.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment