0

I have HTML 5 circle drag and drop example,

http://jsfiddle.net/eGjak/503/

i want to follow things

  1. prevent drag circles outside of the canvas

  2. hide lines over the circles

  3. prevent drag over another circle

i play with some codes but no luck there. can anyone please help me, by logic or some helpful resource

1
  • Hello, do you have a working example of your requirements? I'm really interested in how you implemented the collision prevention. I know how to detect collision but not how to prevent it. Commented Feb 25, 2014 at 15:05

1 Answer 1

3
  1. This is an easy one. It's simply checking x doesn't leave the left or right side of the screen and y doesn't leave the top or bottom of the screen

    if (x>0 || x<(canvas.width - circle.width) && y>0 || y<(canvas.height - circle.height)) {

    ...update... 

    }

  2. Here you need to do a line/circle collision check. See here.

  3. For this you need circle/circle collision detection. The below will return true if collided otherwise false:

    this.isIntersecting = function(c1center, c1radius, c2center, c2radius) { var dX = Math.pow(c1center.x - c2center.x, 2); var dY = Math.pow(c1center.y - c2center.y, 2); var r2 = Math.pow(c1radius.radius() + c2radius.radius(), 2); return (dX + dY <= r2); } 

c1center and c2center are object with x, y properties (eg: c1center = {x:0, y:0 })

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.