Circular collision

G

Gabriel Fournier

Guest
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?
 

obscene

Member
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.
 
G

Gabriel Fournier

Guest
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.
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
 
G

Gabriel Fournier

Guest
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 ... ??
 

RujiK

Member
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;}
 

Yal

🐧 *penguin noises*
GMC Elder
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 ... ??
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).
 
G

Gabriel Fournier

Guest
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;}
WOW it's like magic in my eyes.

This is EXACTLY what I wanted.

Thank you very much
 
G

Gabriel Fournier

Guest
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).
Oh right now I understand but Yes @RujiK code works damn well thnaks for the feed back
 

Negastar

Member
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.
 

Yal

🐧 *penguin noises*
GMC Elder
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.
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.
 

Joe Ellis

Member
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
 
Top