Game Development Community

Rotation Help

by Danny Mejia · in Torque Game Builder · 02/22/2007 (4:29 pm) · 5 replies

I have create two block like this:

function createBlock()
{
GetBlockType();
%piece = new t2dAnimatedSprite() { scenegraph = scenewindow2d.getSceneGraph(); };
%piece.setSize(5.25, 5.25);
%piece.playAnimation($Name);
%piece.setLayer(0);
%piece.setLinearVelocityY(5);
return %piece;
}

function nextBlock(%val)
{
if(%val == 1)
{
$nextPivot = createBlock();
$nextPivot.setPosition(30,-10);

$nextSlave = createBlock();
$nextSlave.setPosition($nextSlave.getPositionX() +35.5 SPC -10);
}
}

I need to rotate the $nextSlave around the $nextPivot. At position north, west, south, and east. I know that the $nextSlave will only be:
1. One block up
2. One block to the left
3. One block down
4. One block to the right

Like this:

AB A BA B
B A

Where A and B are the blocks. The GetBlockType(); pick the animation for the blocks.

#1
02/23/2007 (1:43 am)
Thats simple trigonometrie :)

// This is for full 360 degree precision
  %slaveX = pivotX + distX * mCos(angle);
  %slaveY = pivotY + distY * mSin(angle);

where distX / distY is the distance you want it to have to its parent.

For your case you can even make it faster:

%slaveX = pivotX + distX * X;
  %slaveY = pivotY + distY * Y;

where X is 1 for right, 0 for up / down and -1 for left,
where Y is 1 for down, 0 for left / right and -1 for up.

That way you can create tuples X, Y for all 4 possible positions.
up: 0, -1
right: 1, 0
down: 0, 1
left: -1, 0

Hope thats help
#2
02/23/2007 (9:38 am)
Thanks...I have try this for each possible. But I need some help on set up the IF statment. Here is what I have:

function rotate(%val)
{
%distX = 5;
%distY = 5;
%x = 0;
%y = 1;
%pivotX = $nextPivot.getPositionX();
%pivotY = $nextPivot.getPositionY();
if(%val == 1)
{
%slaveX = %pivotX + %distX * %x;
%slaveY = %pivotY + %distY * %y;

$nextSlave.setPosition(%slaveX, %slaveY);
}
}

How would I change the %x, %y value. So that I can keep rotate the block around.
#3
02/23/2007 (4:36 pm)
I have tied to use a switch statment for change the value of %x and %y. But that dose not work. Thanks for any help.
#4
02/24/2007 (1:51 am)
Thats not that hard.

You would "identify" each val as a given x y combination:

switch( %val)
{
  case 0:
    // right
    %x = 1;
    %y = 0;
  case 1:
    // up
    %x = 0;
    %y = -1;
  case 2:
    // left
    %x = -1;
    %y = 0;
  case 3:
    // down
    %x = 0;
    %y = 1;
  default:
    // default is right
    %x = 1;
    %y = 0;
}

This should work

In the function you raise val, you simply can do

%val = (%val + 1) % 4
#5
02/24/2007 (4:14 pm)
Thanks..I had the %val setup wrong.