Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Ball to Ball collision handling

Ball to Ball collision handling

Scheduled Pinned Locked Moved JavaScript
questionjavascripthtmlgame-devperformance
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SRJ92
    wrote on last edited by
    #1

    hi, I have looked all over and can't find something that suits my needs... so here goes... I am creating a snooker game in HTML 5 canvas and JavaScript and have now came to the part of resolving the ball to ball collisions. So far I have this, which is great, it detects the cue ball hitting another ball:

    function isColliding(ball){
    var distance = Math.sqrt(((this.posX - ball.posX)*(this.posX - ball.posX)) + ((this.posY - ball.posY) * (this.posY - ball.posY)));

    if (distance < this.radius + ball.radius){
    //balls have collided now work out where they collided

    var collisionPointX = ((this.posX * ball.radius) + (ball.posX * this.radius)) / (this.radius + ball.radius);

    var collisionPointY = ((this.posY * ball.radius) + (ball.posY * this.radius)) / (this.radius + ball.radius);

    ball.speed = this.speed;

    this.directionX = Math.cos(?);

    this.directionY = Math.sin(?);

    ball.directionX = Math.cos(?);

    ball.directionY = Math.sin(?);

    }

    }

    However I need to know how do I work out the Math.cos and Math.sin for the balls to move in a direction that is expected (using elastic collision), when they collide? Thanks for help in advance.. Steve

    K S 2 Replies Last reply
    0
    • S SRJ92

      hi, I have looked all over and can't find something that suits my needs... so here goes... I am creating a snooker game in HTML 5 canvas and JavaScript and have now came to the part of resolving the ball to ball collisions. So far I have this, which is great, it detects the cue ball hitting another ball:

      function isColliding(ball){
      var distance = Math.sqrt(((this.posX - ball.posX)*(this.posX - ball.posX)) + ((this.posY - ball.posY) * (this.posY - ball.posY)));

      if (distance < this.radius + ball.radius){
      //balls have collided now work out where they collided

      var collisionPointX = ((this.posX * ball.radius) + (ball.posX * this.radius)) / (this.radius + ball.radius);

      var collisionPointY = ((this.posY * ball.radius) + (ball.posY * this.radius)) / (this.radius + ball.radius);

      ball.speed = this.speed;

      this.directionX = Math.cos(?);

      this.directionY = Math.sin(?);

      ball.directionX = Math.cos(?);

      ball.directionY = Math.sin(?);

      }

      }

      However I need to know how do I work out the Math.cos and Math.sin for the balls to move in a direction that is expected (using elastic collision), when they collide? Thanks for help in advance.. Steve

      K Offline
      K Offline
      Kenneth Haugland
      wrote on last edited by
      #2

      I think you should have a look at the three top articles in the search below: http://www.codeproject.com/search.aspx?q=snooker&x=0&y=0&sbo=kw[^] In any case you should use vector calculus to calculate the angles as well as the elsatic collition. The Angles would be dependent on the incoming vector and the deviation should be from this line of reference. I know that the code is written in C# but I think you should get the understanding of the physics.

      D 1 Reply Last reply
      0
      • K Kenneth Haugland

        I think you should have a look at the three top articles in the search below: http://www.codeproject.com/search.aspx?q=snooker&x=0&y=0&sbo=kw[^] In any case you should use vector calculus to calculate the angles as well as the elsatic collition. The Angles would be dependent on the incoming vector and the deviation should be from this line of reference. I know that the code is written in C# but I think you should get the understanding of the physics.

        D Offline
        D Offline
        dusty_dex
        wrote on last edited by
        #3

        There are physics engines already implemented in JS Physics Engine comparison :)

        "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

        K 1 Reply Last reply
        0
        • D dusty_dex

          There are physics engines already implemented in JS Physics Engine comparison :)

          "It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan That's what machines are for. Got a problem? Sleep on it.

          K Offline
          K Offline
          Kenneth Haugland
          wrote on last edited by
          #4

          Did not know that. :)

          1 Reply Last reply
          0
          • S SRJ92

            hi, I have looked all over and can't find something that suits my needs... so here goes... I am creating a snooker game in HTML 5 canvas and JavaScript and have now came to the part of resolving the ball to ball collisions. So far I have this, which is great, it detects the cue ball hitting another ball:

            function isColliding(ball){
            var distance = Math.sqrt(((this.posX - ball.posX)*(this.posX - ball.posX)) + ((this.posY - ball.posY) * (this.posY - ball.posY)));

            if (distance < this.radius + ball.radius){
            //balls have collided now work out where they collided

            var collisionPointX = ((this.posX * ball.radius) + (ball.posX * this.radius)) / (this.radius + ball.radius);

            var collisionPointY = ((this.posY * ball.radius) + (ball.posY * this.radius)) / (this.radius + ball.radius);

            ball.speed = this.speed;

            this.directionX = Math.cos(?);

            this.directionY = Math.sin(?);

            ball.directionX = Math.cos(?);

            ball.directionY = Math.sin(?);

            }

            }

            However I need to know how do I work out the Math.cos and Math.sin for the balls to move in a direction that is expected (using elastic collision), when they collide? Thanks for help in advance.. Steve

            S Offline
            S Offline
            SRJ92
            wrote on last edited by
            #5

            Thanks :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups