Rotating tank and projectile angles

Started by
0 comments, last by Zeugmal 20 years, 2 months ago
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.
Advertisement
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;else    return 0; 
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

This topic is closed to new replies.

Advertisement