# Tracking and applying that to rotation to follow what I want tracked

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know this is silly and been asked on here 500 million times. Only I couldn't find any of those times so I figured I would make a topic while thinking on this

I got a turret that needs to constantly rotate to a target

My tracking and rotation seems to be way off
this is what i'm trying so far. I'm actually trying just to get it to track on the x axis right now then when I get it working put it to the Y axis as well

[source]

distance = sqrt((float)(pow(position.x - target.x, 2)+pow(position.z-target.z, 2)));
xChange = asin((position.x - target.x)/distance);
yChange = sin(target.y - position.y);
D3DXQUATERNION y;
if(oldXChange!=xChange)
{
D3DXQuaternionRotationAxis(&y, &D3DXVECTOR3(0.0f, 1.0f , 0.0f), xChange);//*(D3DX_PI/180)); // main rotation for the gun
gunRotation = y * gunRotation;
}
oldXChange = xChange;
[/source]

2 issues i'm having here. Fixing one might fix the other
1. if I move the turret position it actually just spins around and around rather than attempting to track to the target
2. obviously its not tracking.

another thing, I shouldn't have to convert from degrees to radians or vice versa right? it should all be in radians correct?

Any ideas on what the crap I'm doing wrong?
C++ directx9 btw

Thanks for anything you might be able to help me with

##### Share on other sites
The asin function does return angles in radians, but there are a few things that I find a bit questionable:

1. Why are you casting pow(position.x - target.x, 2)+pow(position.z-target.z, 2) to a float?
2. Shouldn't your distance calculation involve a y component also?
3. If you want to rotate around the x-axis, shouldn't you have D3DXVECTOR3(1.0f, 0.0f , 0.0f) instead of D3DXVECTOR3(0.0f, 1.0f, 0.0f)?

I hope that helps you, although it is a bit unclear what exactly you are trying to do.

##### Share on other sites
Hi,

I've attached a simple Turret class, which shows how I would go about solving this problem. It does not use any 3rd party libraries so you should have no trouble adding it to your game.

-Josh

##### Share on other sites

The asin function does return angles in radians, but there are a few things that I find a bit questionable:

1. Why are you casting pow(position.x - target.x, 2)+pow(position.z-target.z, 2) to a float?
2. Shouldn't your distance calculation involve a y component also?
3. If you want to rotate around the x-axis, shouldn't you have D3DXVECTOR3(1.0f, 0.0f , 0.0f) instead of D3DXVECTOR3(0.0f, 1.0f, 0.0f)?

I hope that helps you, although it is a bit unclear what exactly you are trying to do.

1. it was just there from previous tries and I was going to eventually get it out of there

2. Well its a 3d game. so in order to get the rotation on the x plane I am just using X and Z coordinates. And when I want it to move up and down I'll use Y and X

3. and no, in a quaternion rotation in order to get to turn left or right you have to use the Y component that way it rotates around the Y axis

I'm hoping this helps clear things up. Probably not

and I don't have anything to open a .tar.gz right now since I'm just quickly on my way and i'm not sure if thats for 2d or 3d.
I've been able to do it in a 2d game before like 4 years ago but this time I think its the rotations screwing me up big time

Anyhow thanks for the replies
does asin return in degrees then?

##### Share on other sites
As I said, asin returns in radians. Now something else I noticed is that you don't set the gunRotation to the quaternion you made, but rather multiplied it, so I believe that if the angle was anything other than 0 this would cause the turret to always rotate.

##### Share on other sites

Hi,

I've attached a simple Turret class, which shows how I would go about solving this problem. It does not use any 3rd party libraries so you should have no trouble adding it to your game.

-Josh

I know you meant this for someone else, but I happen to be thinking of a game that will use turrets and I'd like to see your code. Unfortunately, the archive is corrupt. Can you reupload this?

##### Share on other sites

[quote name='jjd' timestamp='1325528721' post='4899026']
Hi,

I've attached a simple Turret class, which shows how I would go about solving this problem. It does not use any 3rd party libraries so you should have no trouble adding it to your game.

-Josh

I know you meant this for someone else, but I happen to be thinking of a game that will use turrets and I'd like to see your code. Unfortunately, the archive is corrupt. Can you reupload this?
[/quote]

Hmm, that's unfortunate. Ok, I'll try uploading it again, but here is a URL to the gist in case the tarball gets corrupted again,

git clone git://gist.github.com/1563274.git gist-1563274

-Josh

 unfortunately it looks like the tarball gets corrupted when I upload it. Let me know if you have any trouble getting the gist. Edited by jjd

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×