tank bullet

Started by
4 comments, last by JoeJ 2 months ago

I am working on a windows form application that draws two tanks and bullets. my question is how do I get the tanks to fire the bullets in a parabolic fashion. here is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        int x1 = 525;
        int x2 = 20;
        float y1 = 405.0f;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Image myImage = Image.FromFile("tankspriteone.png");
            e.Graphics.DrawImage(myImage, x1, 405);

            Image myImage_two = Image.FromFile("tanksprite.png");
            e.Graphics.DrawImage(myImage_two, x2, 405);

            Image myImage_three = Image.FromFile("bkgnd.png");
            for(int i=0; i<=580;i+=20)
            {
                e.Graphics.DrawImage(myImage_three, i, 425);
            }

            Image myImage_four = Image.FromFile("tankbullet.png");
            e.Graphics.DrawImage(myImage_four, x2+40, y1);

            Image myImage_five = Image.FromFile("tankbulletone.png");
            e.Graphics.DrawImage(myImage_five, x1-5, y1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            x1 -= 5;
            Invalidate();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            x1 += 5;
            Invalidate();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            x2 -= 5;
            Invalidate();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            x2 += 5;
            Invalidate();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            x1 -= 5;
            y1 = (-0.5f) * (x1 * x1) + 35.0f;
            Invalidate();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            x2 += 5;

            Invalidate();
        }
    }
}
Advertisement
  1. Start by assigning your bullet a position : P.x=0, P.y = 0.5 (it's a half meter above the ground)
  2. Assign a velocity to the bullet: V.x = 50, V.y = 50 (it's moving RIGHT and UP at a 45° angle)
  3. Update the bullet for every tim step: P.x += V.x*dt, P.y+=V.y*dt
  4. If you stopped at step 3, your bullet would go up forever. To apply gravity, you need to reduce the velocity in the Y-direction by 9.8 units (or some other constant) for every second. So: V.y -= 9.8*dt. Eventually V.y will go from a positive number to a negative number.

It's really easy to test this out in a spreadsheet

P.S. It looks like you're loading a graphic file everytime you paint the screen. You should load the graphic once upon initialization, store it in a variable or class, and then use the variable to draw it.

scott8 said:
P.S. It looks like you're loading a graphic file everytime you paint the screen.

Phil: How can i make my player jump using C++ and OpenGL?

The world: Don't use GLUT. It's dead. And it's just bad to make games with. Use GLFW, SDL, or make your life easy using SFML or Raylib… anything, but not GLUT.

Phil: hmmmm…
How can a make an action game using… WINDOWS FORMS ??? wtf - seriously ??? /:O\

Guess i have to post that Todd Rundgren song again, dear Phil!

I just wanted to do something different

But you don't do any different? You still try to make a game.

Just, Windows Forms isn't meant for games. It's meant to make some GUI i guess.

So why do you invest time in learning about a GUI library, but not into learning about a library that gives sprites, tiled backgrounds, handling input, etc. for games?

But well, if you like it, have fun.

private void button6_Click(object sender, EventArgs e)
        {
            x1 -= 5;
            y1 = (-0.5f) * (x1 * x1) + 35.0f;
            Invalidate();
        }

Btw, i think this should give a parabola. But maybe you can't see it because it's falling too quickly.

You could try to be more verbose about physics, so tweaking parameters becomes more intuitive.

private void button6_Click(object sender, EventArgs e)
        {
            x1 -= 5;
            float t = -x1 * 0.01f; // getting some measure of time from x in a hacky way
            y1 = (-0.5f) * (t * t) + 35.0f;
            Invalidate();
        }

However, that's calculating the parabola analytically, assuming no external events such as the bullet colliding with something on it's way.

Scotts proposal does integration, allowing simulation, which can react to such external events.
So that's usually better for games.

This topic is closed to new replies.

Advertisement