Programming a Puzzle to detect collision of other bricks, grid area etc.

Hi all,

Please can you suggest the best way for me to handle the coding of this puzzle.
(See attached image)

Following the Getting Started tutorials, (specifically the code about how to drag the ship), I have successfully been able to code all the bricks and the key, to move/be dragged up and down or left and right on their respective paths.

Next I want to…

  1. Prevent the bricks from exiting the grid area.
  2. Prevent the bricks and the key from passing through each other.
  3. When the player has fully slid the key out of the bottom of the grid area, I want the game to recognise that this event has occurred so I can add the key to the players inventory and mark the puzzle as completed.

Would all 3 things be a physics need?
I’ve not had any reason to use the physics engine before, so totally green to its capabilities.

Or is it a case of coding x/y coordinates and/or heights/widths etc??
Is it a maths thing?
 

If you could be so kind as to point me in the right direction, I am sure I can figure it out, its just knowing what I need to research or best practices in these situations which is all new to me.

Thanks in advance
Angela

I think physics is the easiest solution.  Let the physics engine do the heavy lifting.

Hi Sporkfin,

Thanks for sending me down the physics path.

I have created static walls, ceiling and ground.

My key and (only coded two) bricks are set as dynamic (will code others once I’ve got things working)

This is so that the key and bricks can collide with each other and the walls, ceiling, ground etc.

However, dynamic setting means that they can rotate, which I do not want them to do at all.

I want them to be set to their horizontal/vertical paths only.

Any ideas on how to code this?

Thanks again, this is completely new to me. 

Thanks

Angela

 

I figured…
 

    itemKey.isFixedRotation = true – locks against (prevents) rotation

and applied this setting after adding object to the physics body, so no rotation occurs, however, the object can still move left and right

If you go the physics route, you’ll probably also need to disable gravity and set which direction you can drag the objects (not actually a physics thing). Also, if I remember correctly, you’ll also need to work with dynamic bodies for all those blocks (since iirc, only dynamic bodies can collide with other bodies).

I’d personally go for a logic based system and not physics, i.e. I’d write a function that checks which way each block can move and how far, and have it update whenever a block is moved. This method might be a bit more difficult though.

Still, I think both approaches should be fine, but each have their difficulties and potential issues. With physics, for instance, you need to prevent the player from being able to push blocks with other blocks or bump the key into locations it shouldn’t go, etc. I personally don’t favour physics for these kind of precision puzzles. Still, you should do what you feel works best for you.

You can use logic checks with a touch joint before setting the object’s new position.  object.setTarget( )

Hiya,

Thanks for replying, so using the physics route I have already disabled gravity and set the key and bricks to dynamic. I also learnt you can stope the bricks/key rotating, but they can still move in all directions. The key just pushes the bricks down. So the game knows the bricks have a body but I’ve been unable to restrict their axises.

I was trying to sleep last night and all I could think about was this damn puzzle!!LOL
So I kind of resided to the fact that I might have to go logic based and do the math.
 
But I am not sure on how to tackle it from this approach either.

THanks

Ok, so before I scrap the physics approach Im going to research the touch joints. THanks

You can also play around with the anchor points