#### Archived

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.

## 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 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;else    return 0;

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009401
• ### Who's Online (See full list)

There are no registered users currently online

×