I have been struggling for several hours, but just cant find a way to achieve what I want.
On the process of building a puzzle game (kinda like Rush Hour), I need to move draggable object within a “grid” and also prevent any object to pass throught another one.
Here is a prototype of the scene and blocs on excel, so you can guess how it looks like.
All the different shape and color blocs have the exact same behaviour. They can be dragged, go from an area of the grid to another one, with no possible passing through other blocs. They cant even touch each other, since there will be an offset between every object.
For the drag part, I am using the extension Snap To Grid, which works fine (but maybe not compatible with all the specs …) :
But I cant figure out how to deal with the collision part.
There will be (like on the screenshot), 14 blocs, walls around the game area and wall inside the game area. Walls cant be moved/dragged, but they should act the same way as blos for the collision part.
Any ideas ? Been stuck on this for too much time, I hope you could help me.
My approach to this would be to add 2 object variables (say named oldX and oldY) to the blocks, and assign them the x and y positions of the block.
Next, create a group (say named notPassable) with all the blocks and walls as members
Then create an event with the condition block being dragged. Under this create a 2 subevents:
the first event with an inverted condition that checks if the block collides with notPassable (effectively checking there is no collision), and an action that sets oldX and oldY to the current position of the block
the second event with the action that sets the block position to oldX and oldY
The reason for doing it this way is that if you don’t invert the first subevent and it’s block on block collision, I suspect it may update the position of both blocks.
My other suggestion would be to add a physics behavior to the objects, deactivate it at the start of the scene, and then activate it (while creating a mouse joint) when your mouse is over them and is clicking on them.
Then your walls can have physics collisions and should act as you expect (I think)
You can do it in the editor, but it’d be more accurate to do it in the events, using the "At the beginning of the scene " condition and setting the values in a foreach block sub-event.
Doing it this way means you can change the scene around without having to re-enter the co-ordinates.
No, it doesn’t (my code was a mock-up). If the object is a member of both groups, it doesn’t detect a collision.
I think this should be raised as a bug.
In the meantime, a work-around is to create a duplicate of each block object (remember to remove any behaviours), and use it like a hidden hitbox (I’ve used the name of the draggable objects, and added the suffix “HB”). I linked this hitbox object with the draggable object via an object variable on both objects named “Id”.
Here’s how I did it (and I checked it does work ) :
Yes. The issue there is that the dragged block is being placed at the mouse position. And once the mouse clears the obstacle, the dragged block will appear to jump through the obstacle.
You may want to do a check that the mouse is still over the dragged block. If it’s not, then either stop dragging (not sure if this is possible), or move the mouse back to the block (or mouse’s previous position).
Don’t do a whole new system. Implement your own drag type behaviour - it’s pretty easy to do.
Here’s a very similar behaviour from one of my abandoned game that only uses the Physics behaviour and a mouse joint. All you’d need to do is check the fixed rotation in the bloc’s physics behaviour, set gravity to 0, and you’ve got what you’re after :