GML Need help with soft collision script

S

Searous

Guest
First off, my collisions work fine. I just want to fix what happens when two instances collide. Also, I'm working with GMS 2.

What I want to do is write a script that pushes two instances away from each other, using the weight value of each object to determine how much it is affected by other instances; weight is the amplitude for the pushing effect. This scrip will execute on both colliding instances, and apply the pushing effect only to the executing object. The effect should scale the closer two instances are to each other, modifying the rate at which the amplitude increases basted on the size of the instances sprite. (while I would like this to be based on masks, simply using a sphere around the instances origin)

I've attempted an implementation of this effect myself, and it has several problems. The most noticible, is that instances seem to sltick to one another rather than being pushed away. This might be a simple fix though.

I've played with this on and off for most of the time I've been working on my game. It's pretty much stayed the same, and it's always had the problem I mention.

Here is the script:
Code:
/// @description Soft entity collisions
var a, distance;
a = point_direction(x, y, other.x, other.y) - 90;
distance = point_distance(x, y, other.x, other.y);

x += sin(degtorad(a)) * (other.weight * (((other.sprite_width / 2) - distance) / (other.sprite_width / 2)));
y += cos(degtorad(a)) * (other.weight * (((other.sprite_width / 2) - distance) / (other.sprite_width / 2)));
On a side note, I would also like a hard collision version which uses masks, and prevents an instance from passing through another instance. My code is setup in such a way to allow different collision conditions based on any factor I want to implement.

Thank you for reading.
 

Neptune

Member
The first thing that comes to mind is:
As soon as the instances touch -- determine a target "force / speed" in the opposite direction -- maybe (current_speed - weight) as the magnitude or whatever physics you want?
And then slowly increment your x and y values toward the new target x and y values (found from the opposite direction(or whatever angle, depending on collision angles), the new magnitude and your trig functions).

Hope this makes sense... I think it will give a squishy/soft collision. A hard version of this, would just be instantly snapping to the new magnitude.
 
S

Searous

Guest
The first thing that comes to mind is:
As soon as the instances touch -- determine a target "force / speed" in the opposite direction -- maybe (current_speed - weight) as the magnitude or whatever physics you want?
And then slowly increment your x and y values toward the new target x and y values (found from the opposite direction(or whatever angle, depending on collision angles), the new magnitude and your trig functions).

Hope this makes sense... I think it will give a squishy/soft collision. A hard version of this, would just be instantly snapping to the new magnitude.
The only problem I have with that is: I'm not sure how to find a point on the edge of a circle. This seems like a much simpler way to go. I think I spotted the issue with my code though. I swear, I"m an idiot sometimes.
 

Neptune

Member
Ah ok, well if it helps, for circles: Given an angle, the (x,y) coordinates are as follows:
Code:
x_coord = radius*dcos(degrees);
y_coord = -radius*dsin(degrees);
So, changing the 'radius' value, changes the size of the circle (magnitude of the (x,y) vector).
Good-luck!
 
Last edited:
Top