How do you prevent dragged objects from going through walls?

What I’m trying to do:
I’m creating a game for mobile. It is a touch controlled game, of course, and you are supposed to drag the blue square into the house, down a few halls, and into a room with a green pentagon. I got the dragging part to work. But I cant get the blue square to collide with the walls while dragging. It keeps going through or over the walls.

So here’s the Question:
How do I get the blue square to collide with the walls of the house, WITHOUT it going through them WHILE dragging it?

I have done something similar before this way:

Create 4 “sensors”. Rectangular objects, 2 with the width (for top and bottom) of the draggable object and 2 with the heigth (right and left). Set its origin coordinates centered in the interior border (according to its position relative to the draggable object) and set 4 extra coordinates (points in GDevelop) centered in the borders of the draggable object.

In the events, force the sensors to always be positioned at its point in the draggable object, so they will be always aligned to it. You can also set the opacity of the sensors to 0 at the scene start, so they will be invisible.

Something like this, more or less

Now you just set the dragging events to occur in a direction only if the sensor of such direction is NOT colliding with a wall.

Note: sometimes a high dragging speed will override the collision effect of the sensor. There are two ways to avoid that s#!t: 1.Make your draggable object a “follower”: instead of force your object to be at the cursor/touch position, make it follow that position at certain speed. This also adds a funny effect for the draggable object, like it is alive and reacts to your finger movement 2. Make the sensors wider into the draggable object. This will increase the chance to hit a coliision at high speed. In fact, you can apply this two measures at the same time and be happy forever and ever until the end of the time-space fabric, matter and sense of existence :slight_smile:

Hope that helps.

Thank you for your quick reply! I was able to do everything you said, except the last part where you said, “set the dragging events to occur in a direction only if the sensor.”

How do I do that? I looked but I’m not seeing the event to set drag direction in only one direction.

I wasn’t using the draggable behavior…

When I was doing something similar in a game, I was doing all the dragging functions “by hand” in the events. That means I was setting the draggable object position to the cursor position in an event with the condition the mouse button is held (and release the object when not). When this conditions were true I was adding (or substracting, depending on the cartesian direction I wanted my object to move) some pixels to the current X and Y coordinates of the draggable object (that’s the “follower effect”). So, I was also able to put “when this sensor is NOT colliding with walls” to the conditions of movement. Example: if the left sensor is colliding then the game cannot substract from the value of the X coordinate of the draggable object, or if the bottom sensor is colliding then the game cannot sum to the value of the Y coordinate of the object, etc.

(I try to explain it as better as I can, english isn’t my native language)

This is an example, is a bit buggy, but you get the idea:

—Drag and collide example—

Thank you very much I will download and take a look at it. :slight_smile:

Thank you for this. It’s not perfect but it’s exactly what I needed.