Please help with circle -> rotated rect coll func

BlitzMax Forums/BlitzMax Programming/Please help with circle -> rotated rect coll func

Robert Cummings(Posted 2009) [#1]
Hello everyone,

Getting back into programming again after a haitus.

My latest project simply wants me to make a circle for the main player and collide it with a rotated rectangle anywhere in the screen with sliding collision.

So I would need to try and work out (in laymans terms, as I am just an artist):

1. define the circle
2. define the rectangle (ie width, height and angle)

3. move the circle against the rectangle but "slide along it" - perhaps the function would fill in a new x,y value for the sphere trying to pass through it?

4. move the circle through a rectangle but register a collision for "zones"

I don't see a situation where I need to collide rotated rectangle -> rotated rectangle, so this should be simple to work out in theory...

So if anyone has any super simple code or ideas I would be in your debt and be very grateful. I could probably do a game logo for you if your help is a function and very "lightweight" in terms of processing :)


ImaginaryHuman(Posted 2009) [#2]
To do this I think there is probably a function you can use maybe in the code archives or other threads, to tell if the circle and rectangle overlap. But I think if you want the ball to be sliding along the rectangle then you're starting to get into physics and would need to calculate some kind of physics interaction. Are you making a pinball game?


Robert Cummings(Posted 2009) [#3]
No just sliding collision of walls in a 2D environment.


Robert Cummings(Posted 2009) [#4]
I suppose super quick circle - line intersection would be just as good. I would then just draw out lines for walls.

For sliding collision I would use:

1. get the angle from circle position and intersection
2. position circle at intersection
3. move it back out by the angle * half radius

Would that work? I don't think it would be very fast though... I'm intent on about a thousand little circles to line intersections with sliding collision (they don't collide with each other though, just move around the world).

If this is the right way to go then I guess I need circle -> line intersection function.


Warpy(Posted 2009) [#5]
Have a look at this blog post I did for the bouncing part.

To make it work with a circle instead of a point, detect collisions when the distance between the centre of the circle and the line is less than the circle's radius - see this code archive entry, or any of the others.

Once you've detected a collision, you can find the point of intersection by solving the line-point distance equation, setting the distance to be equal to the radius.