Game Development Community

Any easier way to rotate a turret around a tank?

by John Klimek · in Torque Game Builder · 03/18/2006 (9:52 am) · 8 replies

Here is my tank which is one sprite and then my turret which is another sprite...
www.lagworld.net/images/t2dtank.jpg
In the screenshot, the turret is mounted onto the tank sprite but I can't seem to get it rotate 180 degrees around the tank without it going inside of the tank, etc...

I've tried changing mount points, using setMountRotation, etc, but I can't seem to figure this out smoothly...

Anybody have any ideas to help me?

Thanks...

#1
03/18/2006 (10:12 am)
Do you want it to actually rotate (which would somehow look strange due to the top of it which ends with a topdown version of it on the other side) or shall it only point to the other side? In that case flipping should be what you look for.
#2
03/18/2006 (10:40 am)
Ah yes... an age old problem... if you search google on this site you should come across this problem several times and the same solution. The solution is pretty simple, but completely non-obvious.

You need to make your turret sprite TWICE the width of the actual art so that the mount point of the turret is in the exact middle of the turret image map. Why? Because rotation in TGB is always at the center point of the sprite. So, with cruddy ascii art, you need to make an image like this:

+----------------------------|----------------------------+
|*****************************                            |
|*****************************                            |
|*****************************                            |
|*****************************                            |
|*****************************                            |
+----------------------------|----------------------------+

where "*" = art and " " = transparent pixels

The rotation point is at the exact middle. Mount this sprite and viola! You should have what you need.
#3
03/18/2006 (5:53 pm)
Ahh... thanks for the advice!

However, I'm having some problems...

Here is my starting code...
$turret.setRotation(0);

$tank.setRotation(0);
$tank.setMountRotation(0);  // just to make sure it's zeroed out before mounting

After mounting my turret...
$turret.mount($tank, "0 -1"); // this will mount the center-point of my turret at the top of the tank

It looks like this:

www.lagworld.net/images/t2dturret-1.jpg
Looks fine, right? Now if I rotate my mounted turret it should still be at the TOP of the tank, right? (because it rotated around it's center-point)

Here is my code...
$tank.setMountRotation(90);

...and here is the result:

www.lagworld.net/images/t2dturret-2.jpg
It would appear as though the mount point was RESET to "0 0" (the center of the tank) and THEN the turret was rotated 90 degrees.

Is this a TGB bug or am I doing something wrong?
#4
03/18/2006 (8:21 pm)
Cool looking tank!

Part of the issue may be a misunderstanding on our part of what you mean by "rotate"--do you want it to rotate as if it were "pinned" to the screen (literally, like playing pin the tail on the donkey), such that the turret would point up, then right, then down, then left, then up again in a 360 degree rotation?

Or do you want the turret to rotate "into" and then "out of" the screen? This second option is quite a bit different in execution.

With that out of the way (your image and comment suggest the first is fine), I'd suggest that you look into using links instead of mounts--they are more appropriate for this type of scenario. As Jason mentioned, mounting is to the center of the object being mounted, but if you define a link point on the tank, you can then mount your turret to that, and it should work much more smoothly for you. Just a thought here, but it seems more appropriate for what you want at first glance.
#5
03/18/2006 (8:57 pm)
Thanks!

It looks like link points might be what I need...

Quote:
As Jason mentioned, mounting is to the center of the object being mounted, but if you define a link point on the tank, you can then mount your turret to that, and it should work much more smoothly for you. Just a thought here, but it seems more appropriate for what you want at first glance.

How do I mount my turret to the link point?

The following code works GREAT, but if I move my tank the turret doesn't follow... (since it's not mounted)
$m = $tank.addLinkPoint("0 -0.25");
  $turret.setPosition($tank.getLinkPoint($m));
  $turret.setRotation(90);  // looks GREAT and properly positioned!

  $tank.setPosition("10 10");  // PROBLEM-  the turret does not follow the tank....

When I try this code, I get an error: t2dSceneObject::mount() - Couldn't find/Invalid object '3'.
$turret.mount($m, "0 0");

The only reason I think I'd want the turret "mounted" onto the tank is so that if I move the tank it will follow it AND if I rotate the tank, the turret will be rotated accordingly (so if my tank is now on a hill, the turret will adjust to the rotation of the tank)
#6
03/19/2006 (8:23 am)
Sorry, I should have suggested this as well!

$turret.setLinkPoint($tank.getLinkPoint($m), "0 0" );

This is the "equivalent" of mount, except to a link point instead of the pivot point of the object to be mounted.
#7
03/19/2006 (4:09 pm)
That doesn't seem to be working for me...

After I execute the command above in the console, the turret does not move onto the tank and if I move the tank the turret does not follow... (in fact, the command above appears to do nothing :))
#8
03/22/2006 (12:38 pm)
I had a similar problem with expectations about mount() in the Poser Example

I solved this by embedding the sprite inside another empty dummy sprite and offsetting it
the required amount so I didn't have to offset my images with transparent pixels, etc.

Perhaps if you look in the AnimNode.cs code it will give you some ideas.

edit:

From the code comments:

Quote:
// AnimNode.setRotation:
// * Set node rotation explicitely.
// We have to keep our children up to date manually (and recursively)
// as T2D does not (YET) allow rotating mounted objects
// automatically AND independently.
// Note that we rebuild all child transforms as we
// move down the hierarchy, otherwise all child localRotations
// would be set to %r.

~neo