Weird physics when setting up tween for movement

Hey everyone, new here and to gdevelop as well. I’m currently working on a mobile slide puzzle game to help promote my art and since I’m more of an artist than a programmer I decided to go with gdevelop to create it. The problem I’m having is with the movement. I just want to basically have slide movement on each piece where it can slide into the empty space when swiped in that direction. I’ve tried several different things and have installed quite a few extensions in order to try to achieve this effect but still having issues smh.

Right now I’m just experimenting with the tween and physics 2.0 features. I setup conditions that set the x and y coordinates to the objects x and y coordinates on touch, set the object from static to dynamic, when you start to drag the object it creates 4 objects at each of the 4 way axises at a distance of 450 pixels (the dimensions of the square pieces) and if you drag the object 30 pixels or more in any one direction it tweens the object to one of those 4 object it’s closest to and then destroys them. It also creates a object at the x and y origin spot and if you release the drag on the object less than 30 pixels from the origin it moves the object back to that origin location. All of my pieces origins have been placed in the center of each object and they are laid out on a 3 column 4 row grid of 450 by 450 squares.

Everything works fine when I have the controls enabled for the first piece but when I setup the same type of controls for the second piece and then try to drag the piece down the second piece I setup goes flying off the screen? The piece moves just as expected and doesn’t cause the second piece to fly off if I have the controls disabled for that piece? I think it has something to do with the collision area of the directional piece effecting the 2nd piece when it is created? I’ve tried the whole filter “enable mask” but still gives me issues. I’m not exactly sure what to do as seeing how this has been the only way I’ve discovered so far to get my piece to move correctly smh. I even tried having the piece tween to a location based on the y coordinate + 450 pixels but still doesn’t work smh.

I’m sure there’s probably a simpler solution than what I’m trying but if so I haven’t found it yet. I’ve included a screen shot of part of the control code and a link to the game with the second piece’s controls turned on to show you what it’s doing. Oh, the only piece that works is the piece right above the empty space btw. Any help woul be greatly appreciated!

What you can do is give the pieces the “Draggable Object” behavior and create barriers.

Well I did that at first but I had pieces sticking if they weren’t completely in the spot being dragged. Then I tried applying a force in the direction being dragged but still the pieces didn’t always wind up exactly in the area they needed to be at. Finally I tried tweening the piece using the create object method and that got the piece perfectly in the spot it needed to be at. I thought I had finally figured out a good method until I added the events and actions to the second piece and started having that weird issue smh. But thanks for the suggestion! :blush::vulcan_salute:

Of course! I don’t have a lot of experience in that area, but what I would recommend is directly messaging Keith_1357. He is currently helping me with a problem and he’s really talented.

Please don’t direct message me. I pop in quite frequently and help when I can. Otherwise, there are a lot of other more experienced users here. I’m pretty good with the language but I’m still learning concepts.

Oh, btw, here’s a screenshot of the current puzzle I setup and have been experimenting with, just to give you an idea of what I’m trying to do. Eventually I want to setup 19 liece puzzle and possibly even 29.

I don’t really have the time right now. I wouldn’t mix physics with tweens though.
I made a slider puzzle last year. I didn’t use dragging. I checked if a tile was clicked and then checked if it was moveable and then used a tween to move it.

This was my method
if the tile is clicked then check if it is movable by using a ray cast (or point is inside) in the 4 directions around the tile.
if it’s moveable then tween it in that direction

I’d share the project but it’s more of a completed project and a bit of a mess. It would take a lot of explaining plus I’d rather help people than give them all of the answers. If I get the chance. I can make a slimmed down version of the mechanism.

Here’s the basics without any checks like tile is being moved. I’d use a boolean for that.

The scene has 2 objects. The tiles are tiled sprites so the image can be positioned inside it instead of separate images. If you use separate images that’s fine just put them into a group named tile. The border is made from an object named frame. It can be hidden at the beginning.

The tiles have the tween behavior.

There’s a group called TileGroup that has the tile object and the frame object. If the image is split then add all of the tile objects and the frame object to this group and create a 2ns group called tile with the tile objects.

The grid is set to 32x32. So, the tiles are all 32x32.

There are 3 scene number variables X, Y, Angle. They’re set to the default value of 0 but the default value isn’t important.

The text is a bit small. I did it on my phone and I wanted to put it in 1 picture.

The events are fairly straightforward.
When a tile is clicked set angle to - 1 and x, y to the center of the tile. The x, y is used to check for other tiles or the frame.

It uses not point is inside using x, y plus or minus the tile size of 32 on each side. This will check for the center position of any tile or frame.

If there isn’t a tile or frame then set the angle to the direction 0, 90, 180 or 270. Since there’s only 1 open space each tile can only have 1 valid move.

If the angle isn’t still - 1 then repick the clicked tile based on its center x, y values and use the tween behavior to the move the tiles in that direction by the grid and image size which is 32.

That’s the basics. We check if a tile is clicked and check if there’s not a tile or frame object on one of the sides.

Try me. Click a black tike.

A quick search came up with this older thread on making a slider puzzle game.

That’s great! Thank you for sharing all of this with me and the example you provided is pretty much what I’m going for! I haven’t tried it yet myself but I can definitely follow the logic of it and seems like it would do the trick for me! I’m sick at the moment but will try it out here in the next day or so and post what my results are and/or if I have any problems with it.

On a side note, I had planned on setting up set puzzle patterns that would load based upon variables set by events in the scene, this is for a timed game category idea I had for leaderboards. Also I will need to have different images load on the tiles in that pattern, also based on triggered variables. Is it possible to do that with this solution?

Either way, thank you again for this option!

You can use external layouts. Or create it through events. If you have the images in a group then you can use the group name and it will behave the same no matter which image is used.

I used a tiled sprite because you can place instances of the same object in a grid and set the image offset to show the corresponding image part based on the column times width and row times height.

I wonder why did you use “invert the logical result of these conditions” instead of inverting the condition itself?
will it work if condition “x ; y is inside ___” is inverted?

That part is a little tricky with certain conditions.
NOT inverts the result while inverting conditions inverts the condition. (in most cases)
Conditions return true or false and select the matching objects.

If there were 10 objects and the point was inside 1
point is inside true picks the object
inverted point is indside true, picks 9
NOT inside false doesn’t trigger (there is 1 point inside. it’s true but not makes it false)

If there were 10 objects and the point was inside 0
point is inside false does not trigger
inverted point is indside true, picks 10
NOT inside true triggers action doesn’t pick anything
(there is 0 points inside. So, it’s false but not makes it true)

3 Likes

thank you for explanation its confusing but I think I get it

1 Like

It took me awhile to figure out, mostly because I’ve been sick, but I took your example and ran with it and finally have my base mechanics figured out.

I setup a object the same size as my other pieces and placed it in the blank area. I then set it up to where when someone touched or clicked a puzzle piece it would create 4 50 pixel wide and tall boxes at the center of each side and turn on collision detection. If the empty object was colliding with one of those boxes it would save the x and y axis of the puzzle piece, tween to the location of the empty space and then teleport the empty space object to the x and y coordinates that were saved. I decided to do it this way so I could setup effects and other things for the individual pieces later if I wanted. Like in the example below I setup a glow effect and raised the z order on any pieces touching the empty space object. Although, I had to go into the collision editor and sheer off the corners to keep it from effecting diagonal pieces adjacent to it smh lol. I’m still new to the whole effects stuff but I think it’ll be fun experimenting with lol. Oh, and here’s a couple of screenshots I stitched together to show the events and actions I used. I basically setup all of them into a group and copy pasted them for each piece and just changed the object and variable on each one to match. Time consuming but not too bad imop.

Anyways, I really appreciate you taking the time to explain your method of doing this and showing me an example of it too! That was really helpful and inspiring in trying to figure out a method that would work for my overall game idea! :grin::metal::vulcan_salute::pray:

1 Like

You’re re welcome. Nice game and art.

1 Like

check out my method… it sooo simple ,just 3 events and 1 tween behavior :upside_down_face:
2024-03-09 23-25-55

I completely redisigned my concept. This is simplified plus adds the abilty to move an entire row or column. Thanks to Modo for the inspiration.

1 Like