# Circular collision

Discussion in 'Programming' started by Gabriel Fournier, Oct 14, 2016.

Tags:
1. ### Gabriel FournierMember

Joined:
Jul 5, 2016
Posts:
31
Hi everyone,

I'm trying to make a pixel perfect circular collision. I want to restricred a circular zone around a player so he cannot go out of this circular zone.

I know there is a collision_circle in game maker but I' m not sure it's good for me ..

Is there smeone who can help me with this?

2. ### obsceneMember

Joined:
Jun 21, 2016
Posts:
2,433
You could use that but .... actually what might be the easiest thing for you is something like this in your End Step event...

if distance_to_point(x,y) > some_number
{
x=xprevious
y=yprevious
}

Let me know if that doesn't suit your needs.

3. ### Gabriel FournierMember

Joined:
Jul 5, 2016
Posts:
31
I'll try this and come back to you.

In the end what I'm trying to do is like in Arc the lad twilight of spirit movement system in battle.

Their moving zone are delimeted by circle zone so I need to do circle collision around the player in du time

4. ### Gabriel FournierMember

Joined:
Jul 5, 2016
Posts:
31
I tweak a little bit what you gave me.

In the end step of the player i've put

if point_in_circle(x, y, circle.x, circle.y, 128){
x = xprevious
y = yprevious
}

It work well the only problem now is it not as fluid as I would like for the collision. I think it's because of the mask of my player probably ... ??

5. ### RujiKMember

Joined:
Jun 21, 2016
Posts:
167
I made a circle collision thing awhile ago:

WARNING! This is DANGEROUSLY silky smooth. Try it and be amazed.

Code:
```var distx = x - circle.x;
var disty = y - circle.y;

var max_distance = 70;
var current_angle = point_direction(0,0,distx,disty);

var max_distx = lengthdir_x(max_distance,current_angle);
var max_disty = lengthdir_y(max_distance,current_angle);

if abs(distx) > abs(max_distx) {x += max_distx - distx;}
if abs(disty) > abs(max_disty) {y += max_disty - disty;} ```

6. ### YalGMC MemerGMC Elder

Joined:
Jun 20, 2016
Posts:
3,739
Nah, it's because you stop movement completely if you'd move out of the circle, even in cases where you'd slide along the edges. @RujiK's code should do exactly what you want if you modify it a bit (change max_distance to 128).

RujiK likes this.
7. ### Gabriel FournierMember

Joined:
Jul 5, 2016
Posts:
31
WOW it's like magic in my eyes.

This is EXACTLY what I wanted.

Thank you very much

RujiK and SnotWaffle Studios like this.
8. ### Gabriel FournierMember

Joined:
Jul 5, 2016
Posts:
31
Oh right now I understand but Yes @RujiK code works damn well thnaks for the feed back

RujiK likes this.
9. ### NegastarMember

Joined:
Aug 26, 2016
Posts:
24
Looks like something I could use also. Can the script be used to check for collisions within a certain section of the circle? I'm thinking it's possible.

10. ### YalGMC MemerGMC Elder

Joined:
Jun 20, 2016
Posts:
3,739
Yeah, just check if the angle from the center of the circle to the object lies within the sector of the circle you want collisions for... if it's not, report that there is no collision. Easiest way to do this is to compute the center angle of the sector and use abs(angle_difference()) < half_sector_angular_width.

Negastar likes this.
11. ### Joe EllisMember

Joined:
Aug 30, 2016
Posts:
948
Here's an alternative that you might prefer cus it has less calculations:

Code:
```var d = point_distance(circle.x, circle.y, x, y);

if d > max_distance
{
d = max_distance / d
x = circle.x + ((x - circle.x) * d)
y = circle.y + ((y - circle.y) * d)
}```
The result is exactly the same as Rujik's one, I just tested both methods with 2 objects that start in the same place running each one, and they're both always in the exact same place.
If you wanna know what the maths is doing, just look up normalizing vectors, this kind of thing is done all the time with vector maths

RujiK likes this.