Thursday, May 03, 2007

Flash Puzzling

Okay, I’m currently trying to recreate an already existing game in Flash, and I’m having some trouble. I’m trying to use a bunch of nested objects since the objects have an obvious parent/children relation, like branches in a tree. Unfortunately, because of this, it’s sorta hard to get the exact location of different objects in the tree, in relation to the root.

For example, let’s say you have object #5, which is nested inside object #4, which is nested in #3, and so on. If you ask Flash for the x co-ordinate of object #5, it will compare its x co-ordinate with object #4, and say something like “I’m 50 pixels right of object #4”, but if you want to know how it compares to object #1 or the root, it’s a bit more complicated. You need to know how object #4 relates to #3, and so on. So eventually you can find out that #5 is 50 px from #4, and #4 is 100px from #3, and #3 to #2 is 200px, and #2 to #1 is 80px, so using some sort of almost recursive function, you find out #5 is 430 pixels from object #1.

So that’s great and all, but then when you start rotating objects… well, unless I don’t know some special function, it all gets really messy. If you rotate object #5, it will still think that it’s 50px from #4, rather than 50pixels X cos(30) or something like that. So, it’s probably not too hard to figure out how far #5 is from #4… but when you have all these objects nested, I think it’s a bit harder.

Anyway, so now I have this problem, and I don’t feel like doing all the complicated math… it’s making my head hurt, haha. Well, I could work on it, but if you want to help too, that’d be awesome… or if you just want to play around with some math, here’s your chance.

Okay, here are 4 puzzles – the first 2 are practice puzzles, but the real meat is in puzzle 3 and 4. I’ve only done puzzle 2 so far, heh. In puzzles 1-3, you find the co-ordinates of point “a”. So check out these puzzles:

Puzzle 1:



Puzzle 2:



I’ve only done puzzle 2. I think it’s correct… I won’t give you the exact answer yet, but if your x value is close to 7.33 and your y value is close to 5.70 (or negative these values, blah blah blah), then we have the same answer.

Puzzle 3 I think, is the actual way my program is going to work. So a few things shifted, so maybe puzzle 3 is easy after puzzle 2, but I’m not sure, I haven’t tried it yet.



Puzzle 4 has no picture, but it is simply this; add more blocks, and create a (semi?) recursive function to calculate the co-ordinates of any possible “a” points. I’ll give you what I have for the x-value semi recursive.

So each of these blocks is identical, and like the example, let’s call them Object #X. All of them are the same, except object #0, or the very first one. All of the objects above 0 have the function

Function getx()
{ return _x + _parent.getx();
}

So from #5, when you call this function, it returns “50” and its parents getx() function, which for #4, will return 100 plus whatever else is at the bottom, and it keeps doing so until object0, which has a getx function which looks like

Function getx()
{ return _x;
}

So that the semi-recursive function stops there. I say semi recursive because it doesn’t use the same function over and over, although… well, it does, but on different objects. I guess it’s easier to understand than a fully recursive function. So, that’s puzzle 4. You can assume that the blocks know a lot, like _x, _y, _rotation, _width, _height, etc. As an additional bonus to puzzle 4, if you could use variables instead of the constants I provided (2, 6, 30 degrees, etc), that’d be good.

Labels: , , ,

4 Comments:

Blogger Unknown said...

wow... I think after posting this question and seeing puz1 and puz2 on the same screen, I realize maybe I should've done puz1 first, because then I can see how the recursive function might look like.

12:57 PM  
Blogger Unknown said...

okay, I think I got it now. I finished puzzle #4, using puzzle #3, with a few modifications.

2:18 PM  
Blogger transcendent said...

I guess the problem wasn't that complicated after all, eh, Bao?

3:12 PM  
Blogger transcendent said...

P.S. I take it you've never had to analyze a robot arm before.

3:13 PM  

Post a Comment

<< Home