I'm developing a scenario to shoot incoming targets by a specific gunner unit, however I'm confused to actual logic of shooting an object with a gun. I'm working in 3D environment. I'v target's position vector i.e [x,y,z] and its rotation vector [x,y,z,angle], and also Gun's position vector i.e [x,y,z] and its rotation vector [x,y,z,angle]. The gun has two components, i.e a TopDondur and a set of Barrels, both components have the rotation along y-axis, i.e only angel of rotation vector [0 1 0 angle] is required to move the Gunner Unit to the target.

What rotataions shuould give the Gunner unit to rotate both TopDondur and Barrels, i.e TopDondur rotates to the target and Barrels are to move up/down with respect to that target.


1 Answer 1


A solution that would work in all cases would be inverse kinematics. You may want to look it up, but that is quite a complex topic.

Here is a general solution for your special problem:

I don't really understand how the TopDondur and Berrels are related (or even what TopDondur is supposed to be) so I will for sake of simplicity assume only on "character". I will also assume that orientations are handled as a R3 matrix (you won't get far with angle + axis).

This is basically what I call a "look at" problem and you can solve it similarly to the function with that same name (gluLookAt). For this you need a position, a target vector and an up vector. And we basically already have everything:

vec3 position = // given by the gunner
vec3 target   = // given by the target
vec3 up       = vec(0, 0, 1); // I assume that 
vec3 forward  = normalize(target - position);
vec3 side     = normalize(cross(forward, up));
// ensure that up really is orthogonal to side and up
up            = cross(side, forward);

mat3 orientation = mat3(forward, side, up);

Now you have the orientation to face the target. The cheap way would be to automatically apply the orientation to the gunner. You can interpolate the current orientation to "slowly" match the target orientation.

You may want to look at the Matrix and Quaternion FAQ on how to get from an angle axis to a R3 matrix and back. When interpolating you may want to go over quaternions for smooth interpolation.

An alternative would be to convert both the current orientation and the target vector to spherical coordinates. This gives you more fine control over "horizontal" and "vertical" movement. This may be of importance, since you state that you only allow movement along the Y-axis.

  • \$\begingroup\$ Thnkx for ur kind explanation, firstly, TopDondur is gunner unit hull and Barrells are its nossels, You can google the image of seazenith a gunner unit to understand this. And about the alternative, if I convert it to special coordinates, what step would I follow after this. ? \$\endgroup\$
    – Ahsan Ali
    Commented Jun 21, 2014 at 19:30
  • \$\begingroup\$ Well you have a set of UV (2d) cords you can then linearly interpolate and then convert back. \$\endgroup\$
    – rioki
    Commented Jun 21, 2014 at 20:55
  • \$\begingroup\$ i'm not getting your point dear, can u give me some example? \$\endgroup\$
    – Ahsan Ali
    Commented Jun 21, 2014 at 21:08
  • \$\begingroup\$ gluLookAt is a function of OpenGL, but im working in Matlab, kindly help me out of this. \$\endgroup\$
    – Ahsan Ali
    Commented Jun 22, 2014 at 0:01
  • \$\begingroup\$ What angle I use for both of rotations, either azimuth or elevation or r when converting to shperical coordinates \$\endgroup\$
    – Ahsan Ali
    Commented Jun 22, 2014 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .