Nicholas Ramsay
Nicholas Ramsay

Reputation: 475

C# Projectile Simulator - Applying Speed to X Position of Projectile Physics

First of all, here is the code:

decimal gravity = decimal.Parse(gforce.Text) / 1000;
        decimal speed = decimal.Parse(initialSpeed.Text) / 1000;

        decimal newAngle = DegreesToRadians(decimal.Parse(angle.Text));
        double doubleNewAngle = (double)newAngle;
        decimal Px = Math.Round(((decimal)Math.Cos(doubleNewAngle) * 0.001M), 13);
        decimal Py = Math.Round(((decimal)Math.Sin(doubleNewAngle) * 0.001M), 13);

        string PxTemp = "";

        for(decimal t = 0.001M; Py > 0; t = t + 0.001M)
        {
            gravity = gravity + gravity * 0.001M;
            Py = Py + speed - gravity;

            Px = (Px + speed * Px);

            graphics.DrawArc(new Pen(Color.Magenta, 10f), 45 + (float)Px, 475 - (float)Py, 1, 1, 0, 360);

            try
            {
                graphics.DrawArc(new Pen(Color.Magenta, 10f), 45 + (float)Px, 475 - (float)Py, 1, 1, 0, 360);
            }
            catch
            {
                MessageBox.Show("Error Px: " + Px.ToString() + " Py: " + Py.ToString(), "Error");
                this.Close();
            }

I am attempting to create a projectile simulator, I have successfully created the effect of gravity and acceleration on the y-axis. But however when applying the speed to the x axis(making the speed depend on the angle) I am having trouble. I can make it so every second the projectile moves 1 metre but for it to be correct the projectiles' speed across the x-axis should depend on the speed AND THE ANGLE.

To achieve this I have done:

Px = Px + (speed * Px)

Where Px is the value of distance across the axis Cosine of the angle:

decimal Px = Math.Round(((decimal)Math.Cos(doubleNewAngle) * 0.001M), 13);

When I do

Px = Px + (speed * Px)

The value returns some huge number for example 4412651515851.41214244121, I at first assumed this was because Px was going beyond its precision point but any rounding attempts I have made have failed, How should I achieve a correct Px number?

Here is an image to visualise it:

enter image description here

Any help would be greatly appreciated, I have been struggling all day and I couldn't find anything on-line. Thanks in advance.

Upvotes: 1

Views: 969

Answers (2)

Tongyi Shen
Tongyi Shen

Reputation: 50

For a useful tool to help workout the proper calculations.http://www.mrmont.com/teachers/physicsteachershelper-proj.html

Upvotes: 0

Lutz Lehmann
Lutz Lehmann

Reputation: 26040

The laws of motion are very different to the ones you use:

y'' = -g --> y(t) = y0 + vy0*t - g/2*t*t
x'' =  0 --> x(t) = x0 + vx0*t

These are the solutions for motion without air friction. Most complications of the equation of motion require numerical integration of the ODE.

The initial velocities vx0,vy0 are what you initially compute in Px,Py. But probably you should use

vx0 = speed*cos(angle)
vy0 = speed*sin(angle)

to get the initial velocity compatible with the inputs. Some additional unit conversions may be required.

Upvotes: 2

Related Questions