Draggable and collision


#1

Hi guys !

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.

Thanks.


#2

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:

  1. 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
  2. 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.


#3

I think MrMen’s suggestion is a strong one.

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)


#4

Thanks a lot.

First time for me to add object variables. How can I do that please ?


#5

In the editor, click the 3 vertical dots beside the object name, and select Edit Object Variables

image

To access it in the events, use <ObjectName>.Variable(<VariableName>)


#6

Ok I have created them, but I do I assign them to the x and y position of the bloks please ?

Is this here or with an event ?


#7

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.


#8

I must be doing something wrong Oo


#9

Close. try something like:


#10

Still not working sorry. Is it on your side ?


#11

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 :smiley:) :

The objects:
image


The groups:


The events:


#12

Hi MrMen and again, thank you very much for everything.

I THINK (obviously not =p) I’ve done it exactly like you did, but it doesnt work …

No behaviour on the bloctestHB and draggable behaviour for bloctest.

Thanks a lot !


#13

Close, so close. You’ve inverted the dragged bloc collision with notPassable (the condition in the second to last event in your screenshot). :slightly_smiling_face:


#14

Damn me =p
Its working great now, thanks MrMen.

Now I would like to keep the kind of movement I had when I begin.
Snap To Grid.

Any chance it can be done with this collision system ?
I’ve try it, by switching or adding my snap to grid event in here :

And its of course, not so easy =p

blocs are moving the way they are supposed to. They are also correctly stopped by other blocs like this :

image

But when I drag them and pass from a bloc to another, they still get through it.

If you cant do it on your side, maybe I can send you a video, just ask me.

Thanks a lot.


#15

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).


#16

I cant find a solution to this unfortunaly.
Any other way to do this according to all the speficiation of the “puzzle” ?


#17

What’s wrong with the solution I gave you?


#18

I dont want the mouse to go back to the block. It will feel strange for players to have something not respondng like they think it would.

I’ll go for a whole other system I think. They will navigate through blocs with the arrow, selecte them and them move them with the arrow.


#19

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 :

DragWithMouse

The events for this are :

The second event in a bit more detail :

image


#20

Thanks a lot ! Very busy this last few days. I’ll definitely try it and keep you in touch.