Drag sprite only along lines (which are also sprites)

Hi everyone!

I am making a puzzle that is similar to this:
image

I want to make a maze where I have to drag the yellow circle over the lines with the goal to reach the end.

Knipselopzettest

Right now I am testing out how to do it. I have created sprites that are lines over which the yellow circle will be dragged. Right now I duplicated one sprite and rotated it for this test (line1 is vertical line2 is horizontal) but eventually there will be more sprites of different sizes that will be either horizontal or vertical lines.

I put in a condition that if the yellow circle is on one of the lines it will stay in the center depending on whether it is a horizontal line or a vertical line.

My problem is that I don’t know how to make sure that the yellow circle cannot be dragged outside of the lines. With the condition I have now it will only snap to the line if it is in collision with the line. My goal is that the yellow circle can only be dragged horizontally or vertically along the lines and not outside the lines.

I think that the main problem is that I will use multiple sprites/lines and therefore cannot make a condition that the yellow circle is limited to the bounding box of the line.

I have been trying to figure this out for a while and I need some outside perspective to steer me in the right direction.

So my question is: How do I make sure that the yellow circle can only be dragged along the lines and not outside of those lines?

I’ve tried to do something similar in the past. It never worked. I think I might have it fixed. It needs more testing.

Try me (the purple box can be dragged)
https://gd.games/keith_13579/follow-line-2024

Project (Click the Green [code] button and [download zip])
https://github.com/doug13579/Gdevelop-drag-objects-along-line-objects

I used 2 “line” objects for testing. It doesn’t seem to matter if the lines overlap or just touch. I used a grid so everything was consistent. I wouldn’t put a gap between the objects. Instead of dragging, it might be better to add force toward the cursor. I didn’t try that method.

This is the scene, I set the opacity for the orange lines for testing. You can see that some overlap while others don’t.

There are 3 main objects. A player with 4 points and the number variable CurrentID and the drag behavior. The Line objects have the variable ID.

Events:

Player Points

I used local variables but in the real world, I might use scene variables for the array instead. It’s a user preference.

The sides array has the 4 directions.

Thank you both for replying. this was really helpfull and insightfull.

First I did not know about object groups, so thank you for pointing that out.

Second I used the set up of @Keith_1357 . The only changes that I have is that I didn’t use a grid for the test. And I added a condition to the “clamp player to bounding box” of line part, so that the dragitem stays in the middle of the line. The only slight issue I have with that is that when you drag the dragitem at a intersection between lines, the dragitem start to jiggle/vibrate/twitch a little (I hope those words were clear :sweat_smile:)

In my approach, I guess you’d want to make sure the objects overlapped. That way they’d share the same area and switching the boundaries wouldn’t cause a jump.

In your method if there’s a gap between the objects then it going to be jumping back and forth between the bounding boxes. I would keep the objects next to each other.

The junction gave me problems. It took a while to fix. I wanted to use trigger once but that would get messed up if the object direction was changed. You could try a trigger once although it might need some sort of directional trigger once. IDK. I’m not at my PC. I believe I got rid of all the issues in my method.

With my method I think the objects should probably overlap. It might also help to move the directional points closer to the center. A single pixel might be enough. I like to leave a little room for variance. You can try reducing the points,