Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Rotating tank and projectile angles

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m making a fairly simple top down strategy game. I have tanks that move around and fire, and what not. The problem comes when they engage each other in combat. I''ve read alot about 2D hemiplane tests, but I just can''t seem to get it right. As it is now, the tanks rotate and fire at each other with varying success. Sometimes the tank shell fires out the back, or off to the side, etc. The tank rotates by doing a line test on the target. If the target lies to the right, it rotates right a bit, and likewise if it''s on the left.
yaw = (float)Math.toRadians(yaw);
float c = (float)Math.cos(yaw);
float s = (float)Math.sin(yaw);

if (c == 0) c = 0.001f;
if (s == 0) s = 0.001f;

float func = (ex - x) / c - (ey - y) / s;

if (func > 1) return 1;
if (func == 0) return 0;
if (func < 1) return -1;

This is working properly, I believe. The issue is yaw. How should I handle this number? The way the tank rotates, and the way yaw is tabulated don''t seem to be in sync. Any help is appreciated. Thanks.

Share this post

Link to post
Share on other sites
You have the target and active tank, their positions, and the active tank''s turret and base rotation (in radians), right?

1) Translate both tank positions such that active tank is at origin.
2) Rotate target tank around origin by -(active.base) - (active.turret), such that the active tank is now aiming straight up a specific axis.
3) If target tank is to the clockwise of that axis, return ROTATE_CLOCKWISE. If target tank to the counterclockwise, guess.

So, assuming that zero rotation is the +X axis, positive rotation goes counterclockwise, and the ground plane is the XY axis...

TANK Me, Him; /* TANK = { vector pos, float base, float turret } */
VECTOR tmp = Him.pos;

tmp = tmp - Me.pos;
tmp = tmp.RotateZ( -(me.base + me.turret) );

if ( tmp.y < 0 )
return -1;
else if ( tmp.y > 0 )
return 1;
return 0;

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!