circle - line collision and bump

Hello .
I am currently developing some basic physics for an android game , but I’ve been for days trying to do a decent collision for a moving circle and a static line .

For the circle I have its center (a point cx,cy) the radius and it velocity vx and vy .
For the line I have the points Pa and Pb that describe the line .

I can already detect when a collision occurs .

But what I’m suffering to find is the final velocity vx and vy after the collision .

Any hints ?

Couldn’t you get the perpendicular at the point of collision then calculate a reflection vector over that?

well the idea of what shoul be done I also have :slight_smile:

the problem is how to make this really work


collided(Circle circle, Line line)
{
    //Get the vector for the line and compute a normalized inverse.
    Vector2 lineVector = new Vector2(line.x2 - line.x1, line.y2 - line.y1);
    lineVector.normalize();
    float temp = lineVector.x;
    lineVector.x = -lineVector.y;
    lineVector.y = temp;

    //Find the measure of the angle between the circle and the inverse.
    float angle = circle.velocity.normalized().dot(lineVector);

    //Reflect the circle's velocity.
    float circleMag = circle.velocity.magnitude();
    circle.velocity.x += Math.cos(-2*angle) * circleMag;
    circle.velocity.y += Math.sin(-2*angle) * circleMag;
}

I’m pretty sure that’s not right at all, but I can’t really think about it right now. It should give you the idea, though.

The normal calculation is correct. As for the rest: v’ = v - (1 + e) (v . N) N where e is the coefficient of restitution.

I don’t like to pass through angles :stuck_out_tongue:

For a bump with a line (not a segment, if it is a segment you have to do special case for the end) :


collided(Circle circle, Line line)
{
    //Get the tangente of the line
    Vector2 lineVector = new Vector2(line.x2 - line.x1, line.y2 - line.y1);
    lineVector.normalize();

    //Project velocity on tangent and normal

    float vTangent = circle.velocity.x*lineVector.x+circle.velocity.y*lineVector.y;
    float vNormal = circle.velocity.y*circle.velocity.x- circle.velocity.x*lineVector.y;

    // Restitution coef

    vNormal = e * vNormal;

    //Calculate the new velocity by flipping the normal velocity
    
    circle.velocity.x = vTangent*lineVector.x + vNormal * lineVector.y;
    circle.velocity.y = vTangent*lineVector.y -  vNormal * lineVector.x;
}

I’m not 100% sure for the signs. But you avoid cos/sin.

I’m pretty sure that it is wrong. The dot product give the cos(angle) not the angle.

Some helper function I had laying around:

	public static float bounceAgainst(float normal, float heading) {
		return 2*normal - (heading + (float)Math.PI);
	}

I think I use it in a bunch of places, so it should work. Heading is just the heading (direction) of the colliding object. Normal is a normal of the line bouncing against. Returns the new heading after bounce.

public class LLine2D extends Line2D.Float{
	.
	.
	.
	public float getDX() {
		return x2-x1;
	}
	public float getDY() {
		return y2-y1;
	}
	
	public float getDirection() {
		return (float)Math.atan2(getDY(), getDX());
	}
}

Add half a pie (PI/2) to the getDirection() of the line to get the normal.

Whatever works best depends on if you have angle and velocity or dx and dy for your movement.

Hope it helps

Ah, yup yup. I always forget that until my equation doesn’t work and I have to go back and remember why. Thanks. :slight_smile:

Thank you all for the help .

I ended up implementing something like Bonbon Chan described … but a question for you alll : what the hell are those Cricle and Line classes ?? At least on my JSE they’re not there , (or I havent searched in the right place) but you guys seem to know them thoroughly … so what’s up with them ??

Those shapes are in java.awt.geom, note the circle is a type of ellipse. They probably just mean the user defined classes like you have though.

Haha, yeah, I think I started that. I only used that as a reference point so you could see what was happening, then hook that into your code. It’s just easier than having like lineX1, lineX2, lineY1, lineY2 etc. etc. being the values. Assumedly you’ll have your own ways of implementing circle and line and they’ll have their own ways of being accessed and mutated.

I have just copied it :wink: Like it is writen, it is easy to understand.