Jump to content

help me with circle collision


none-9999
 Share

Recommended Posts

For collision between two moving circles, you can use this function:

 

https://gist.github.com/kittykatattack/befdddc4985a9a43bf68

 

If you have an array called `circles` full of circle objects, you can test them for collisions inside a game loop like this:

//Test for a collision inside a game loop:for (var i = 0; i < circles.length; i++) {  //The first circle to use in the collision check   var c1 = circles[i];  for (var j = i + 1; j < circles.length; j++) {    //The second circle to use in the collision check     var c2 = circles[j];    //Check for a collision and bounce the circles apart if    //they collide.     movingCircleCollision(c1, c2);  }}
Link to comment
Share on other sites

 

For collision between two moving circles, you can use this function:

 

https://gist.github.com/kittykatattack/befdddc4985a9a43bf68

 

If you have an array called `circles` full of circle objects, you can test them for collisions inside a game loop like this:

//Test for a collision inside a game loop:for (var i = 0; i < circles.length; i++) {  //The first circle to use in the collision check   var c1 = circles[i];  for (var j = i + 1; j < circles.length; j++) {    //The second circle to use in the collision check     var c2 = circles[j];    //Check for a collision and bounce the circles apart if    //they collide.     movingCircleCollision(c1, c2);  }}

ok,  very thanks, i will test 

Link to comment
Share on other sites

That code has a little problem that may or may not influence in your game (beside that if your are using "use strict" it will fail).

 

It's that the order in which balls are checked for collision matters.

For example if you have a line of balls:

 

0       1 2 3 45 6 7 8

O->   OOOOOOOO

 

If you check them in the same order that they are hitted, they will all move in one frame.

(1 is hit so move to 2, 2 is hit so move to 3.......).

 

But if it's checked in in the opposite each on will start to move one frame at a time.

(8 was not hit, 7 was not hit....... 1 is hit so move to 2 ~ next frame: 8 was not hit.... 2 was hit so move to 3... ~ 7 frames later: 8 was hit so move)

 

It also means that you can't collide with more that 1 ball per frame (because it will move you out of position).

 

This could really make no difference at all in your game or it could make it feel a bit random, not always reacting at the same speed.

 

Good luck with your game.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...