Adding a projectile, how calc movement

Started by
6 comments, last by shawnre 17 years, 4 months ago
Hello everyone: I have a tank moving around the screen based off WASD movement pattern. The A and D keys increment a rotation variable to change the tanks direction. Everything works fine with the tank, forward, reverse, rotating. Rotation is now set at .05 increments based off A and D keys being pressed. I am now trying to add a shooting option to the tank. The space bar is used to shoot. When it is pressed I capture the rotation angle of the tank. I assumed doing this would allow me to assign a trajectory to the projectile based off the rotation angle. It is currently not working correctly. Every shell fired is going down and right, no matter what direction the tank is facing. Any suggestions??
Advertisement
Post the code where you create the projectile, and where you move it.
Perhaps you forgot to take positivity and negativity into account? You have to figure out which way the tank is facing first, then set the correct direction (for example: positive in x, negative in y)
This is piece of code capturing space and assigning variables:

if (userinput.IsKeyDown(Keys.Space))            {                reload_time += 1;                if (reload_time > 10)                {                    firing = true;                    soundBank.PlayCue("tankshoot");                    reload_time = 0;                    tankshellposition.X = tank1position.X + 30;                    tankshellposition.Y = tank1position.Y + 15;                    tankshellrotation.X = tankshell.Height/2;                    tankshellrotation.Y = tankshell.Width/2;                    shelltrajectory = rotation_angle1;                    tankshellactive = true;                }            }


Shelltrajectory variable is capturing the rotation_angle1 of the tank.

I have already mangled my code for the movement, but, here is what is left now:

if (tankshellactive)            {                tankshellsprite.Begin();                tankshellsprite.Draw(tankshell, tankshellposition, null, Color.White, (float)shelltrajectory, tankshellrotation, 1.0f, SpriteEffects.None, 0f);                tankshellsprite.End();                                    tankshellposition.X += (float)Math.Sin(shelltrajectory) * (-1)+ 2;                    tankshellposition.Y += (float)Math.Cos(shelltrajectory) + 2;                                                    //tankshellposition.X -= (float)Math.Sin(shelltrajectory) * (-1) + 2;                    //tankshellposition.Y -= (float)Math.Cos(shelltrajectory) + 2;                                    if (tankshellposition.X > 800)                {                    tankshellactive=false;                    shelltrajectory = 0;                }                if (tankshellposition.X < 0)                {                    tankshellactive=false;                    shelltrajectory = 0;                }                if (tankshellposition.Y > 600)                {                    tankshellactive=false;                    shelltrajectory = 0;                }                if (tankshellposition.Y < 0)                {                    tankshellactive = false;                    shelltrajectory = 0;                }            }

Could you also post the code where you update the tank's position? It seems that the projectile motion should be identical to the (presumably working) tank motion code, except perhaps for magnitude.
Tank movement Input:

if (userinput.IsKeyDown(Keys.W))            {                //double x_location;                //x_location = Math.Sin(rotation_angle1);                tank1position.X += (float)Math.Sin(rotation_angle1)*(-1);                tank1position.Y += (float)Math.Cos(rotation_angle1);                if (tank1position.X > 770)                {                    tank1position.X = 770;                }                if (tank1position.X < 30)                {                    tank1position.X = 30;                }                if (tank1position.Y > 570)                {                    tank1position.Y = 570;                }                if (tank1position.Y < 30)                {                    tank1position.Y = 30;                }            }            if (userinput.IsKeyDown(Keys.S))            {                tank1position.X -= (float)Math.Sin(rotation_angle1)*(-1);                tank1position.Y -= (float)Math.Cos(rotation_angle1);                if (tank1position.X > 770)                {                    tank1position.X = 770;                }                if (tank1position.X < 30)                {                    tank1position.X = 30;                }                if (tank1position.Y > 570)                {                    tank1position.Y = 570;                }                if (tank1position.Y < 30)                {                    tank1position.Y = 30;                }            }            if (userinput.IsKeyDown(Keys.A))            {                rotation_angle1 -= .05;            }            if (userinput.IsKeyDown(Keys.D))            {                rotation_angle1 += .05;            }if (userinput.IsKeyDown(Keys.S))            {                tank1position.X -= (float)Math.Sin(rotation_angle1)*(-1);                tank1position.Y -= (float)Math.Cos(rotation_angle1);                if (tank1position.X > 770)                {                    tank1position.X = 770;                }                if (tank1position.X < 30)                {                    tank1position.X = 30;                }                if (tank1position.Y > 570)                {                    tank1position.Y = 570;                }                if (tank1position.Y < 30)                {                    tank1position.Y = 30;                }            }            if (userinput.IsKeyDown(Keys.A))            {                rotation_angle1 -= .05;            }            if (userinput.IsKeyDown(Keys.D))            {                rotation_angle1 += .05;            }


Tank Updating Draw:

tank1sprite.Begin();            tank1sprite.Draw(tank1, tank1position, null, Color.White, (float)rotation_angle1, tank1rotation, 1.0f, SpriteEffects.None, 0f);            tank1sprite.End();


Yes, it looks ugly, but, its working, lol.

Aside from the tankshell.
Two things I would like to point out:

First, instead of calculating cos and sin each time you update the shells movement, why not store the x,y values in variables instead? Then when debugging, turn your tank at random values and see what is getting populated into said variables.

For example:

if (spacehit){ shellmovement.x = Math.Cos(rotation_angle1)*(-1); shellmovement.y = Math.Sin(rotation_angle1);}


Then when you call the function to draw it, you simply add shell movement to shell position, debug is your friend though, make sure all variables are set like they should be.
Hey all, thanks everyone for their suggestions. Turns out, after many hours of staring at the code, I simply was using one variable when I should have been using another. As ready4dis suggested, I attempted debugging my solution. I set up break points at various points and pointed at variable names. While the program is running, mousing over your variables will display their contents. That is how I discovered what was wrong here:

tankshellsprite.Draw(tankshell, tankshellposition, null, Color.White, (float)shelltrajectory, tankshellrotation, 1.0f, SpriteEffects.None, 0f);

Instead of using the tankshellrotation, I needed to use tank1rotation to take into account the direction of the tank.

Thanks for all suggestions!!!

Shawn

This topic is closed to new replies.

Advertisement