• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Mouse-drag object goes through wall

P

Pylon

Guest
I have an object (uses physics) that I drag around with the mouse.

Create event code looks like this:
depth = -1000;
grab = false;
xx = 0;
yy = 0;

Step event code looks like this:
if(grab==true)
{
xo=phy_position_x+xx;
yo=phy_position_y+yy;
phy_speed_x = mouse_x-xo;
phy_speed_y = mouse_y-yo;
}else{ phy_speed_x = 0;
phy_speed_y = 0;}

Left pressed event code looks like this:
grab = true;
xx=mouse_x-phy_position_x;
yy=mouse_y-phy_position_y;

Global left released event code is this:
grab = false;

I have another object which I use as floor/wall, which has a collision event with my mouse-drag object.
But the object I drag with the mouse still goes through the floor/wall. Any ideas on how to prevent that? Thanks.

All objects and the room use physics.
 

2Dcube

Member
You are setting the position of the object to your mouse directly, so that will happen even if your mouse is over another physics object.

If you want to use physics the way they were intended, never set phy_position_x/_y yourself.
The physics system is about using forces on the fixtures, which in turn changes x,y position, rotation etc.

So what you could try is have the object move towards the mouse with forces. This will be more like the magnet item in Zelda: Breath of the Wild. It's not as direct as simply dragging, more like there is an invisible elastic string between the mouse and your object.

For example
Code:
if (grab == true)
{
  var force,dir,ximp,yimp;
  force = 10;
  dir = point_direction(x,y,mouse_x,mouse_y);
  ximp = lengthdir_x(force,dir);
  yimp = lengthdir_y(force,dir);
  physics_apply_impulse(x,y,ximp,yimp);
}
 
B

Bayesian

Guest
This will be more like the magnet item in Zelda: Breath of the Wild
This is probably not what he wants his mouse object to do.

You should take phy_speed_x/y and set it to the difference between your mouse_x/y and phy_position_x/y, so that it moves that total distance in one step
 
Last edited by a moderator:
  • Like
Reactions: Ben
P

Pylon

Guest
You are setting the position of the object to your mouse directly, so that will happen even if your mouse is over another physics object.

If you want to use physics the way they were intended, never set phy_position_x/_y yourself.
The physics system is about using forces on the fixtures, which in turn changes x,y position, rotation etc.

So what you could try is have the object move towards the mouse with forces. This will be more like the magnet item in Zelda: Breath of the Wild. It's not as direct as simply dragging, more like there is an invisible elastic string between the mouse and your object.

For example
Code:
if (grab == true)
{
  var force,dir,ximp,yimp;
  force = 10;
  dir = point_direction(x,y,mouse_x,mouse_y);
  ximp = lengthdir_x(force,dir);
  yimp = lengthdir_y(force,dir);
  physics_apply_impulse(x,y,ximp,yimp);
}
That's the thing. I'm not changing the object position based on the mouse x/y. I'm applying phy_speed_x/y to it to move it to the mouse position.
Your code doesn't allow me to move the object at all for some reason.
Thank you anyway, much appreciate it.
 
Top