Object collision puzzle

Started by Steve84, Sun 29/04/2007 21:56:08

Previous topic - Next topic

Steve84

Hi,

I was wondering if anybody could give me some advice regarding object collisions.

Basically I have some puzzles which include dragging puzzle pieces around to get them into the correct places. I have the dragging part working but I can't seem to figure out how to get it so that I can't drag an puzzle piece through another one. I've tried using the 'isobjectcolliding' function but this only works once an object has already overlapped another one.

Check this website out as this is similar to the puzzle I'm trying to make and will demonstrate what I mean...

http://www.puzzlebeast.com/slidingblock/sliding_irritating.html

(Word of warning though....the puzzles on that website are annoyingly addictive!)

Thanks in advance for any help you can give me...

SupSuper

I'd store the original object position before the drag, and when 'isobjectcolliding' hits, put it back on the original position.
Programmer looking for work

CodeJunkie

If you're doing nice grid-based movements I'd suggest using a 2d array.  Everytime the object is dragged and let go, find the new mouse position relative to the old mouse position to find out which direction it was dragged.  Then check those new positions in the matrix to see if it is empty, update and redraw.  For example:

{ {0, 0, 2, 2}
   {1, 1, 1, 2}
   {1, 0, 2, 2}
   {1, 0, 0, 0} }

0 = empty, 1 = shape 1, 2 = shape 2

If you try to move shape 1 to the right, first check that the array is big enough each time and if so check the item to the right of each 1.  If any of the checks return something other than 0 or 1 then change some variable to mean that it is impossible (a boolean is suitable).  Otherwise, set all the 1's to 0's and all the 0's in the new position to 1's.

Steve84

Thanks for the help...

Quote from: SupSuper on Mon 30/04/2007 22:52:32
I'd store the original object position before the drag, and when 'isobjectcolliding' hits, put it back on the original position.

Well I had thought of this. But the problem with this is that the player would have to slide the pieces perfectly between one another, which I think would become quite irritating to the player as the piece returns to it's original place everytime they move a pixel in the wrong direction.... Thanks for the suggestion though.

Quote from: SimB on Mon 30/04/2007 23:16:58
If you're doing nice grid-based movements I'd suggest using a 2d array. 

Hmm I've not tried doing anything like that yet so I shall have a look into it. It sounds like a good idea, thanks.

I'm also looking at defining the object x any y coordinates in repeatedly execute and then if 'isobjectcolliding' is true then instead of recording the object x and y coordinate, setting it to the variable from the previous movement....If that makes any sense??! I havn't got this to work either yet but will keep trying.

Shane 'ProgZmax' Stevens

Basically all you REALLY need is to test if you're over an empty area that's for the piece in question when you release the mouse button, right?  What I would do in that case is setup hotspot zones (either in the precise shape of the puzzle piece or a sphere near the center of the blank area) and do a hotspot.getatscreenxy  check for the hotspot that piece fits with.

Basically you make a static hotspot called puzzle_fit or whatever:  static Hotspot *puzzle_dest;

When you click on a specific piece (provided the pieces are objects/chars) you can add something like this to the interaction editor:
puzzle_dest=hpuzzleslot1;  Each piece would have a different hpuzzleslot# and you would draw the hotspots on screen where they go.

When you release the piece it would then do:  if(hotspot.getatscreenxy(mouse.x,mouse.y)==puzzle_dest)
{ //place the piece}
else
{
//reset the piece location and set puzzle_dest to null
}


I can think of plenty of ways to do what you're doing but this just seemed the most obvious.  You'll also want some kind of variable to store the puzzle pieces as they are solved and weigh them against some count to see if you've finished it.

Hopefully this isn't too esoteric.

Steve84

Well I've got it working perfectly now. But I decided to take out the mouse function altogether. I've set it up so that you can use the enter key to scroll through the puzzle pieces which show up in a small window to the side. Then whichever piece is the 'active' one, it can be moved around using the arrow keys. I set up a 2d array as suggested by SimB and it all works perfectly now. So thank you all for your help!

And thanks ProgZmax too... I've already set it up with arrow keys now but I'll keep your suggestion in mind if I need to set up anything similar again!

SMF spam blocked by CleanTalk