Collision with a canvas ring, how to work, where circles should move, once encountered?

I am going to create a game in html canvas. This is an air hockey game, and I got quite a lot, though that. The game has three circles: the disk that hit, and two controllers (used to hit the disk / circle).

I have a disk that bounces off walls and has the function to detect when a disk collides with a controller. The bit I'm struggling with is when two circles collide, the controller should remain stationary, and the disk should move in the right direction. I read a bunch of article but still can not understand.

This is where Codepen links my progress. You can see that the puck bounces off the controller, but not in the right direction. You will also see that the puck comes from the controller through which it passes. http://codepen.io/allanpope/pen/a01ddb29cbdecef58197c2e829993284?editors=001

I think that after this I am an elastic collision, but I’m not sure how it works. I found this article but could not get it to work.

http://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769

Heres is my collision detection feature. Turn to the drive yourself, and the controller [i] is the controller that the drive lands on.

this.discCollision = function() { for (var i = 0; i < controllers.length; i++) { // Minus the x pos of one disc from the x pos of the other disc var distanceX = self.x - controllers[i].x, // Minus the y pos of one disc from the y pos of the other disc distanceY = self.y - controllers[i].y, // Multiply each of the distances by itself // Squareroot that number, which gives you the distance between the two disc's distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY), // Added the two disc radius together addedRadius = self.radius + controllers[i].radius; // Check to see if the distance between the two circles is smaller than the added radius // If it is then we know the circles are overlapping if (distance <= addedRadius) { var newVelocityX = (self.velocityX * (self.mass - controllers[i].mass) + (2 * controllers[i].mass * controllers[i].velocityX)) / (self.mass + controllers[i].mass); var newVelocityY = (self.velocityY * (self.mass - controllers[i].mass) + (2 * controllers[i].mass * controllers[i].velocityX)) / (self.mass + controllers[i].mass); self.velocityX = newVelocityX; self.velocityY = newVelocityY; self.x = self.x + newVelocityX; self.y = self.y + newVelocityY; } } } 

Update

Deconstructed a demonstration of a collision with a circle and tried to implement his collision formula. This below works for hitting the puck / disc forward and down, but for some reason will not hit the back or up.

 this.discCollision = function() { for (var i = 0; i < controllers.length; i++) { // Minus the x pos of one disc from the x pos of the other disc var distanceX = self.x - controllers[i].x, // Minus the y pos of one disc from the y pos of the other disc distanceY = self.y - controllers[i].y, // Multiply each of the distances by itself // Squareroot that number, which gives you the distance between the two disc's distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY), // Added the two disc radius together addedRadius = self.radius + controllers[i].radius; // Check to see if the distance between the two circles is smaller than the added radius // If it is then we know the circles are overlapping if (distance < addedRadius) { var normalX = distanceX / distance, normalY = distanceY / distance, midpointX = (controllers[i].x + self.x) / 2, midpointY = (controllers[i].y + self.y) / 2, delta = ((controllers[i].velocityX - self.velocityX) * normalX) + ((controllers[i].velocityY - self.velocityY) * normalY), deltaX = delta*normalX, deltaY = delta*normalY; // Rebound puck self.x = midpointX + normalX * self.radius; self.y = midpointY + normalY * self.radius; self.velocityX += deltaX; self.velocityY += deltaY; // Accelerate once hit self.accelerationX = 3; self.accelerationY = 3; } } } 
+5
source share
2 answers

The main problem with my code was that the user controller was attached to the mouse. When a collision occurs, the function will work continuously because the circles are still touching due to the position of the mouse. I changed my code, so the controller is controlled by the user keyboard.

I also asked for help on reddit and got some help in my collision code. Some good resources where they are related. ( http://www.reddit.com/r/javascript/comments/3cjivi/having_a_go_at_building_an_air_hockey_game_stuck/ )

0
source

I am not very versed in these types of math problems, but it looks like you need to rotate your vectors around sinusoidal and cosine angles. I will point you to a working example and the source code that runs it. I did not get this example.

I recently solved the collision detection problem with the circle of this problem, but one solution I came across includes code to create new vector directions. Ira Greenburg hosts her original source at processing.org . Ira also refers to Keith Peter's decision in Animation ActionScript: Make Things Move!

I copied Ira code to Javascript processing mode and then moved it to Github Pages so you can see it before you try.

0
source

All Articles