I need help here on figuring out the math for when a circle collides with a line between (x1, y1)-(x2, y2) when the line is at an angle. What would be the math formula to detect if the circle has crossed between (x1, y1)-(x2, y2) of the line.
Thankx
Printable View
I need help here on figuring out the math for when a circle collides with a line between (x1, y1)-(x2, y2) when the line is at an angle. What would be the math formula to detect if the circle has crossed between (x1, y1)-(x2, y2) of the line.
Thankx
The equation for the circle I'm using is:
y2+x2-1=0
That is a circle with radius 1 and centre (0,0)
the line I'm using is:
y=x+1
I'm using this because I know what the answer will be.
replace y for x+1 in the circle equation, giving:
x2+(x+1)2-1=0
x2+x2+2x+1-1=0
2x2+2x=0
x=0 or x=-1
then put the x values into either equation, the line equation is simpler, giving.
y=1 when x=0
and y=0 when x=0
so the collision points are:
(0,1) and (-1,0)
I'm not sure how to make a straight forward equation though (eg if you're using this for a game.)
Thankx for replying. I am really having a difficult time relating the line part though. I have a line and the starting point is (x1, y1) and the other end is (x2, y2). If the circle intersects somewhere between the two, where is that point. I can't relate your x to my x1 and x2. I understand the circle formula but not the line. Would you mind breaking it down a bit so that an algebra idiot like myself could get a better understanding of what you are saying. I really do appreciate your time and effort.
A line has no end points, so we need to modify the line equation to work within limits. Add constraints on both sides:
y-y1=(x-x1)(y2-y1)/(x2-x1)
&
min(x1,x2)<x<max(x1,x2)
and the circle:
x^2+y^2=r^2
solve y.
to change a line from (x1,y1) to (x2,y2)
to=
y=mx+c (stadard way of writing a straight line.) you have to do this:
(y-y1) / (x-x1) = m
where m is the gradient which is:
(y2-y1) / (x2-x1)
which is basically what kedaman said.
Here's how I would then continue. I'd do what I described in the first post, then if the x value is lower than x1 or higher than x2 (that is if x1<x2 otherwise do it vice versa), then don't bother finding a y value for that instersect as the line with those boundries doesn't actually go there.
did that clear things up. hehe I doubt it.
Ya'll have been great trying to help me with this and maybe I'm just slow. I take and insert a component which is a line onto my form inside of a picture box. The line goes from the bottom left corner (as the start point) and ends at the top right of the picture box (as the ending point). The line has the properties of x1 x2 y1 and y2. I then grab a shape component and add it to my picture box and make it a circle. I put the circle in the top left corner of the picture box. I then use a horizontal scroll bar to move the shape horizontally and a vertical scrollbar to move the circle vertically. I'm looking to code collision detection when the circle hits the line no matter where on the line that it touches.
I do very much thank you for your replys to my post, but I am really not understanding how I can relate your formulas to this. Algebra just wasn't and still isn't one of my strong points. Could you please break it down a bit further for me please. Thankx
sorry, but I can't figure out how to get your circle into the right format, someone else will have to help out here.
Acidic, I appriciate your effort. I really do.
OK, I've tried having a go, here what I've got.
Since you've always got a perfect circle, you don't have to worry about coefficients of x or y.
radius = (Circle.Width-Circle.Left)/2
r=radius2
l=Circle.Left+radius
h=Form.Height-Circle.Top+radius
then the equation of the circle is:
y2+(x-l)2-r+h=0
I haven't tested then thoroughly, it might be somewhere in the right direction though.
EDIT:
Ignore the above equation (I think).
I've made somthing in VB (presuming you use this). just place a circle in a form as well as this code for a button:
VB Code:
Dim radius As Integer Dim r As Double Dim h As Double Dim l As Double Private Sub Command1_Click() radius = qqq.Width / 2 r = radius ^ 2 l = qqq.Left + radius h = Form1.ScaleHeight - qqq.Top + radius Print radius Print r Print l Print h End Sub
I think it works as it is supposed to.
Acidic, you do your momma and daddy proud. That code really is something and I do mean it my friend. One question though; when the circle moves and finally touches the line, what would be the intersect point of the line itself, especially with the line being at an angle. It would be as if it were a triangle and the line is it's hypotenuse. How can I actually know what part of the line that the circle has made a collision with? I'm looking to create a dent in the line in my game. An example of what I'm looking to do in the game is create a dent into a wall as though a cannonball hits the wall. The wall to finally have an opening in it from the cannon balls hitting it. I know what to do if it were square with 90 degree angles but not the hypotenuse side of the triangle.
Once again I thank you for putting in the time to help me. I can only hope that one day I will be able to return the favor. If not to you then to a newby in the future.
I have to say I'm stuck now, I'm not even sure my above code is correct. I'd wait until tomorrow when the real Gurus wake up and answer yor question.
Your code did what I needed as far as the circle goes and I sincerely thank you for your time. Don't sell yourself short as far as real Gurus go. You should be very proud of your accomplishments and also with sharing your knoledge. Thank you my friend thank you.
Does anyone out there have any sugestions at to what I can use as a formula? HELLLLLLLLPPPPPPPPPPPPP!