2D Toolkit Forum

2D Toolkit => Support => Topic started by: Mezcaholic on January 19, 2013, 02:13:33 am

Title: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 19, 2013, 02:13:33 am
To learn Unity + 2DTookit I want to make a simple memory game.  The board would be comprised of a 9 x 9 grid.  I have rendered the pieces in POV Ray along with animated rotations. Each piece has a symbol and a backside.

To save space I have only included 1 copy of the backside rotating animation and all of the symbol animations.  I need to animate the back facing card until it is perpendicular to the view plan and then switch to another clip of the symol starting from perpendicular to front facing.  If it is not a match I need to go from front facing to perpendicular and then back facing again but completing a full 360 degrees of the rotation.

It's a total of 4 clips and 30 frames.

1) Can you string clips together or is it better to create long clips of everything I want to do?
2) Do I create a master grid array and load animations into each grid point and then swap out the clips in code

I know these are basic questions and I don't want people to write the code for me I just want to understand maybe what some of the approaches would be and what the tradeoffs are.

Thanks in advance for any help.

-E
Title: Re: How to implement rotating puzzle pieces (A noob's odyssey)
Post by: Mezcaholic on January 19, 2013, 02:52:42 am
It looks like picking through Demo 5 will get me a long way towards asking better questions.  I'll update with my findings.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 19, 2013, 04:24:00 pm
Yup, was gonna suggest the same thing. The demo goes through most of the features.

To answer your questions:
1. You can do either. You can string clips together using animation complete delegates to start playing the next clip after the first one is down.
2. That should work fine.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 19, 2013, 07:57:27 pm
Thanks again for your reply.  Yes demo 5 was very helpful.  The message sample threw me for a loop until I saw that in the clip editor in the timeline there was one small line above the frames and that is where the triggered even was.  That's a pretty sweet setup.

So I get creating a game object in the editor and then adding the components in the GUI and manipulating it from code(sort of) :)  But, if I want my game to support 3 x 3 to say 10 x 10 grids how do I create these programatically?  I'm trying to figure out how I would construct this on the fly create the GameObjects that I need when I need them.

Noob on a mission,
-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 19, 2013, 08:05:40 pm
Create a prefab, store a link to the prefab to your game controller object, and Instantiate as may instances as you need. Check the unity docs on how to do this. Once you grasp this concept a you'll find it is very powerful indeed.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 21, 2013, 07:54:59 pm
Thanks again Unikron, I worked through that and I have my board populating from prefabs.  You are right, this is very powerful.

I'm now at an implementation issue.  The images on the board need to be buttons or something like them.  I need to get notified if someone clicks on it so that I can begin the animation sequence.  I'm not quite sure how to do this.  Do I pass GUILayout.Button() an invisible image the size of my images?

I also messed around with GUILayout for well layout and it works will but I don't know to know where the buttons are laid out.  So when I do get my button press I won't be able to draw my animation where the button was.  There is probably a straight forward way to approach this but I have not found it yet.

-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 22, 2013, 06:32:27 am
Looks like I am having another issue.  When I instantiated my prefabs before I was not collecting the returned object.  I'm using C# btw.  So I created a tk2dAnimatedSprite called mySprite and then I did something like

mySprite = (tk2dAnimatedSprite) Instanciate(gridCell, Vector3.zero, Quaternion.identity);

This give me a bad cast error, so then I changed it from tkwdAnimatedSprite to (Transform) and I get past the cast but when I try to use the animation it says there is no animation attached to the Clone.  So if I instantiate a bunch of prefabs clones how do I go about addressing them and manipulating them?

This is probably something basic but I kind of stuck at this point.

Thanks for any help.

-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 22, 2013, 07:02:32 am
Ok progress.  I guess setting up my public prefab variable as a Transform was a no no.  I have set it up as a tk2dAnimatedSprite and at least I am getting results that I would expect.  The current plan is to create a list of my animation objects and then in my OnGUI routine create invisible buttons over each of these.

Then key the animations off of clicks on these.  If there are better ways to do this I would appreciate a heads up from anyone really.  Also, if this is not the proper forum for these kinds of questions let me know too.  I don't want to gum up the works.

Thanks,
-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 22, 2013, 12:07:26 pm
Each gameobject in Unity consists of > 1 component. It MUST at least have a transform component.

When you create a reference to an object by using a child component, instantiate will return that particular object. If your reference is a GameObject, it will return a GameObject. If you reference a Transform, you get a transform back, and so on.

If you store a reference to a GameObject, you can use something like this to get to an attached component you don't have a reference to.
GameObject go = Instantiate(...) as GameObject;
tk2dAnimatedSprite animSprite = go.GetComponent<tk2dAnimatedSprite>();


To anwer your second question about clicking and stuff, keep away from the GUILayout stuff. Look at the tk2dButton sample. You could probably use that as a basis to implement what you need exactly.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 23, 2013, 07:30:33 am
Yup, I have that part working now.  I just have to skin my knees a bunch but I'm picking up speed.  Things are looking pretty good.  I've changed to project to a Sudoku game, similar layout.  But now I want to group my tiles in mini grids.  I have the code working, it's essentially code I had running on XNA.

However, in XNA the 2DTexture used screen coordinates to draw with.  I found a utility function on the Unity forums that said it did the screen coordinate conversion.  I had to modify it for it to somewhat work for me.  You replied to that post saying that you had added a utility function as well that handled different camera types and such. What's the name of it?

Also, my scaling is not working properly.  In XNA I had everything lining up regardless of screen res, but currently I have gaps between the tiles of my mini grids.  I guess I need to figure out how to derive the proper scaling in the local bounds.

Anyway thanks a bunch, things really are progressing and I find your toolkit to be everything that has been advertised.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 23, 2013, 09:12:36 am
Do you use the tk2dCamera? If so, then the world positions = screen coordinates. Should be a whole lot easier to deal with in a game like yours.

What kind of gaps? If its really tiny black gaps, have you done this? http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,294.0.html
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 25, 2013, 05:03:55 am
Ok, I switched to tk2dCamera and my coordinates are good now.  The gaps are gone since I followed the instructions at the link you passed me.  I am looking through your button code to try to get something going with that.

Main problem I have right now is that my animations are too small.  At 1920 x 1080 I have to scale them up 400 x and y to get them to the anticipated size.  When the resolution goes up or down from there there are gaps again.  Not the one pixel something funny is going on gaps more like I need to scale those things again.  I was expecting the size of my sprites in pixels to translate to actual pixels.  I'm definitely doing something wrong.

-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 25, 2013, 09:54:13 am
Yup. Go into your sprite collection, and in settings tick Use tk2dCamera and commit. Everything should be the right size now.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 25, 2013, 03:59:12 pm
That's it, I am at 100% feature parity with what I had in XNA plus I have a dynamic background.  Your toolkit is awesome and your support is even better.  I have the buttons implemented and invisible overlaid my board cells.  I'll figure out how to attach them to actions I need.

Now to another bit of functionality.  I need to create 2 custom controls. The first is a carousel control for the symbols that can be placed in the cells.  I have a vision for what I want but I don't know the best way to implement it.  The control should be 3D or at least appear 3D.

Is it best to actually place the carousel pieces with Z depth for the 3d Control?  Would I need a separate camera for that since my main camera is ortho? If so how do I combine the outputs of the 2 cameras? 

If an actual 3D control is not practical I can simulate it with scaling and vertical offsetting and I think I can figure out the rest.

As always thanks for your help unikron,
-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 25, 2013, 08:16:22 pm
Its up to you - with what you're planning you probably need a perspective camera. Doing this is easy enough, but its not easy to get close to 1:1 mapping with a perspective camera + viewport. A much easier solution will be to do it in 2D like you mentioned.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 27, 2013, 03:29:41 am
Is there an easy way to setup the tk2dCamera so that the origin is at the top left and positive Y is down?

-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 27, 2013, 09:29:16 am
No there isn't.
Its quite a bit trickier than it sounds -
Its easy to modify the camera so it does that. However, that means that the y axis is flipped, and all sprites will be flipped vertically. To fix it, all sprites will need to be flipped vertically internally to compensate, but this means that the sprites won't be compatible with normal cameras any more.
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 28, 2013, 03:53:44 pm
Yeah, I found out the hard way.  :)  But I learned a bit more in the process.  I was able to modify my code to work with the given coordinate system.  It was really only and issue where I try to layout tiles from an array of data.  Everywhere else it's very natural to use that system.

Thanks,
-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 29, 2013, 04:14:11 pm
Ok I have my 2d 3D carousel and now I need to animate it.  The real intended audience for this is touch enabled devices.  They should be able to touch any where along the control and swipe left or right to spin it or depress the front facing sprite to select it.

Thing is I don't really know how to approach this.  I can put an invisible button on the front facing symbol but how do I get continuous live control for the spinning part?  I'll post to unity forum as well as I know this is not really a 2dtoolkit issue but any insights from the 2dtoolkit user base would be greatly appreciated.

Thanks,
-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: unikronsoftware on January 29, 2013, 08:10:17 pm
What do you mean by the "continuous live control"? Isn't this whole thing just a big illusion - you scale and offset your sprites based on how its spinning, so the control itself still remains static?

Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 29, 2013, 11:58:39 pm
Yes, it is an illusion.  Though the buttons don't track mouse movement, they only have up down events so I am not sure how I would go about getting those values.  Is there a way for me to track down the movement of the mouse or fingers in a specific region?

Everything else I have working fine with the buttons but the don't need to feed movement to the control, they just need to know they have been pressed.  I apologize if this is a very basic question but I'm not sure how to do that.  I'm still digging around trying to find what I need.

Thanks,
-E
Title: Re: How to implement rotating puzzle pieces (a noob's odyssey)
Post by: Mezcaholic on January 30, 2013, 03:36:36 am
Ok, it looks like Input.GetAxes is what I am looking for.  I'll report back with what I find.

-E