Game Development Community

Need help rotate many objects

by Danny Mejia · in Torque Game Builder · 08/19/2006 (6:06 pm) · 1 replies

I have 16 object like this:
****
****
****
When I click on one of the objects I want the other object that thouch it to rotate. So If I click on object_1 I want object_2 and five to rotate. Could someone point me to some info on how to do this?

Thanks

#1
08/19/2006 (7:59 pm)
I doubt you can do this automatically using physics or collisions since your objects aren't really touching each other and you're wanting to activate it based on a mouseclick. I'd write one function that rotated a piece. Have it take a parameter of the location (1-16, however your objects are numbered) and have it rotate the piece however far you want it rotated. Then write a second function that finds neighbors based on what you pass it and calls the rotation function on them (store a value that labels each location 1-16). You'll probably have to hard code the neighbors though. But what i'm thinking ultimately is that you'll click and:

function object::onMouseClick(%this) {
   rotateFunction(%this.value);  // value is a predefined location defining number 1-16
   findNeighbors(%this.value);
}


findNeighbors(number) {
   switch (number) {
      case 1: rotateFunction(2); rotateFunction(5);
      case 2: rotateFunction(1); rotateFunction(3); rotateFunction(6);
      case 3: rotateFunction(2); rotateFuncation(4); rotateFunction(7);
      etc, etc, etc
      case 16: rotateFunction(12); rotateFunction(15);
   }
}

rotateFunction(number){
   rotate("object" @ number);  //assumes your variables are called "object1" - "object16"
}

You might even be able to do the rotateFunction thing inline without having to write a seperate function. Not sure how the graphical rotation function works, but this method would also allow you to keep up with whatever was being changed if you wanted to keep up with anything else other than just rotating graphics. Hope that's clear and works for you as a solution. It by no means is final code; just meant to give you an idea of how to solve your problem.

Edit: I keep thinking more about it and editing it as such. The above should work for a small group, hardcoding neighbors, but you could also do it like the following if your groups are going to get larger. Save your width in a variable (a 4x4, 16 cell group, the width would be 4) and your max number in another variable and write four seperate checks for the top, left, right and bottom limits in place of the findNeighbors function above. Let's just look at it in code, shall we?

//top check
if ((%this.value - width) > 0) rotateFunction(%this.value-width);

//bottom check
if ((%this.value + width) < maxnumber) rotateFunction(%this.value+width);

//right check - not sure of mod symbol in TS
if ((%this.value mod width) != 0) rotateFunction(%this.value +1);

//left check - left most value will always have a remainder of 1 when divided by the width
if ((%this.value mod width) != 1) rotateFunction(%this.value - 1);

if we think about this one at a time...
top check - a value of 1-4 will yield a negative or zero value when subtracted from the width (4), so you won't rotate. Everything else from 5 - 16 will yield a positive number, so it's northern neighbor will get rotated.
bottom check - a value of 13 - 16 will yield a number higher or equal to than the max, so they won't get their lower neighbors rotated, everything else passes the check.
right check - 4, 8, 12 and 16 are equal to zero when modded by the width, 4 (4/4 = 1 with no remainder, etc) so they fail the check. everything else isn't equal to zero, so we rotate the next one up in the list (right neighbor).
left check - this one is confusing. we WANT the remainder when dividing by the width to be 1 in order to NOT rotate. 1, 5, 9 and 13 are all the left side, when you divide them by 4, you get a remainder of 1 (1/4 = 0 with a remainder of 1, etc) so those guys don't get their left neighbor rotated, everything else does (2/4 = 0 with a remainder of 2, etc).

That was probably WAY too much explanation and i've probably confused you more than you'd care to be, but that will work if your field ever gets too big to hard code neighbors in.