Tank game. Help with rotation please!!!

Started by
7 comments, last by shoryuken 21 years, 10 months ago
I am newbie at game programming. I have read many other posts similar to this, but just not what I was looking for, or maybe I just didn''t understand it. Ok, my problem is simple. I am trying to make the very oh so popular tank game. I have position and velocity vectors for the tank. I want to be able to rotate it, and move it forward and backward. How do I do this? I think I need a vector to specify which direction is the front of my tank. But other than that, I have been thinking about this for two days now, and have no idea how to do this. I realize this may seem like too simple a problem to require that much effort, however, I am fairly dumb. Like, how do I rotate the forward vector, in the case that I have one. How do I move my tank back and forth along that vector? Any help would be very very very much appreciated. Thank you.
Advertisement
If you want to make a 2D Tank Game you may do it this way:
I would make a struct for the tank which contains the current move direction in form of an angle and the current coordinates of the tank.

...
struct tank
{
...
float posx posy;
float angle;
...
};
...

to move your tank forward an backward just use this

tank.posx = tank.posx + cos(tank.angle) * speed;
tank.posy = tank.posy + sin(tank.angle) * speed;

if you set the speed negative the tank moves backward.
I don''t use vectors and velocity nevertheless I hope it helps.
In 3D you must do this with coordinate transformations.

e.g. in OpenGL:

void Render()
{
...
glLoadIdentity(); // clear the current matrix

// Render Tank
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0.f, 0.0f, -10.0f); // Tank moved to postion 0.0,0.0,10.0

...

}

Austrian Coder

shoryuken,

I and the other GameDev moderators take a VERY dim view of people continually reposting a question as a new thread, simply because they aren''t getting answers fast enough, or the answers you want. If you aren''t getting a response to a question and want to remind people of it, respond to your own initial post (but only once!) asking for help again.

Do not do as you did and start 3 threads asking the same question.

That goes for anyone else reading this thread who may consider this behaviour reasonable.

Timkin
I think this is just a case of him resubmitting because he got an error. Happens all the time. You can delete your own duplicated posts by clicking on edit.

Anyways, to answer your question, to update your position with the velocity vector, you add the velocity times the elapsed time to the position.

position += velocity * elapsed_time;

Remeber velocity is units distance / units time. For instance, if I am at 10,10 and time 0, with a velocity of 1,0 pixels per second, after five seconds, I will be at:

new_position = position + velocity * elapsed_time
new_position = [10,10] + [1,0] * 5
new_position = [10,10] + [5,0]
new_position = [15,10]

So i will be at 15,10 at time 5.

Rotation can be accomplished with simple trig:

new_velocity.x = velocity.x * cos (angle) - velocity.y * sin (angle);
new_velocity.y = velocity.x * sin (angle) + velocity.y * cos (angle);
quote:Original post by invective
I think this is just a case of him resubmitting because he got an error. Happens all the time. You can delete your own duplicated posts by clicking on edit.


Not likely given the dates and times of the postings.

Cheers,

Timkin

Timkin,

Thanks for following up on this, and for closing the two duplicate threads.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Actually, given the dates and times of the postings, it''s very likely that they were accidental multiple posts.

I may be doing the same thing with my space ship game, rotating and then moving inib the direction of rotation. Here''s a clip of code form the game.


  void C_NOVA_SHIP::Rotate( int rotate_direction ){  float radian;  int degree;   // change the angle of sprite by adding rotation direction  nova_ship.sprite_rotation += rotate_direction;    // Rotate the sprite and then reload bitmask  nova_ship.Rotate();  nova_ship.Load_Bitmask();  // add 90 degrees to the ship rotation and change to radian  // This is for vector direction for the sprite  degree = nova_ship.sprite_rotation + 90;  radian = degree * 0.0174533f;    // calculate the cos and sin of the above radian  cos_dir =  cosf( radian );    sin_dir = -sinf( radian );    // set the ships new velocities  // I use magnitude so I can play with the sprite speed  nova_ship.Set_Vel_X( cos_dir * vector_magnitude );  nova_ship.Set_Vel_Y( sin_dir * vector_magnitude );}// end Rotate  


I had to write my own sprite rotation algo for this.
I may incorporate the directional velocties into the sprite library to simplify it''s use.

This algo assumes the sprite is pointing upwards before rotation.

Guy

This topic is closed to new replies.

Advertisement