Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. function Disc(parent, position, rad) {
  2. this.parent = parent; // a PlinkoBoard object
  3. this.position = position;
  4. this.r = rad;
  5. this.velocity = createVector(0, 0);
  6. this.mass = 1;
  7. this.acceleration = createVector(0, 0);
  8.  
  9. this.update = function () {
  10. //this.applyForce(GRAVITY);
  11. this.acceleration.y = 1;
  12. this.velocity.add(this.acceleration);
  13. this.position.add(this.velocity);
  14. this.acceleration.mult(0);
  15. this.checkCollisions();
  16. }
  17. this.hoverUpdate = function(){
  18. this.applyForce(createVector(.1,0));
  19. this.velocity.add(this.acceleration);
  20. this.position.add(this.velocity);
  21. this.acceleration.mult(0);
  22. this.checkCollisions();
  23. }
  24.  
  25. //Show the disc
  26. this.show = function () {
  27. fill(color(220, 10, 10));
  28. stroke(color(255, 0, 0));
  29. ellipse(this.position.x, this.position.y, this.r * 2, this.r * 2);
  30. noStroke();
  31. }
  32.  
  33. this.applyForce = function (force) {
  34. var f = p5.Vector.div(force, this.mass);
  35. this.acceleration.add(f);
  36. };
  37.  
  38. this.checkCollisions = function () {
  39. // if hits right wall
  40. if (this.position.x > (this.parent.position.x+this.parent.size_width-(this.r))) {
  41. this.position.x = (this.parent.position.x+this.parent.size_width-(this.r));
  42. this.velocity.x *= -1;
  43. } else if (this.position.x < this.parent.position.x+(this.r)) { // left wall
  44. this.velocity.x *= -1;
  45. this.position.x = this.parent.position.x+(this.r);
  46. }
  47.  
  48. // floor
  49. if (this.position.y > this.parent.floor_y) {
  50. this.velocity.y *= -0.5;
  51. this.position.y = this.parent.floor_y;
  52. }
  53.  
  54. for(var index = 0; index < this.parent.pegs.length; index++){
  55. var this_peg = this.parent.pegs[index];
  56. if(dist(this.position.x,this.position.y,this_peg.position.x,this_peg.position.y) < (this.r + this_peg.r-1)){
  57. var collisionPointX = ((this.position.x * this_peg.r) + (this_peg.position.x * this.r)) / (this.r + this_peg.r);
  58. var collisionPointY = ((this.position.y * this_peg.r) + (this_peg.position.y * this.r)) / (this.r + this_peg.r);
  59. var normal_vect = createVector(collisionPointX-this_peg.position.x,collisionPointY-this_peg.position.y).normalize();
  60. console.log(collisionPointX,collisionPointY,normal_vect);
  61. //this.velocity.x = (this.velocity.x * (this.mass - this_peg.mass)) / (this.mass + this_peg.mass);
  62. //this.velocity.y = (this.velocity.y * (this.mass - this_peg.mass)) / (this.mass + this_peg.mass);
  63. this.velocity = p5.Vector.add(p5.Vector.mult(p5.Vector.mult(normal_vect,p5.Vector.dot(this.velocity,normal_vect)),-2),this.velocity);//( -2*(V dot N)*N + V )
  64. this.velocity.mult(.5);
  65. this.position.x += this.velocity.x*1.2;
  66. this.position.y += this.velocity.y*1.2;
  67. }
  68. }
  69.  
  70. };
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement