Trajectory with C#

  • Thread starter Thread starter James
  • Start date Start date
J

James

Hi there,
I'm a beginner level programmer and am wondering how to achieve a Simple
Cannon Ball trajectory with c#.
See, i know how to work out the initial horizontal and vertical velocity of
the cannon ball:

double gravity = 9.8;
double initialVelocity = 45;
double angle = 45;

xVel = initialVelocity * Math.sin(angle);
yVel = initialVelocity * Math.cos(angle);

after this stage i don't know what to do to see an animation. I know i need
a loop and i need to change the x and y positions of the ball but i do not
know where to place them and how they should be calculated.

I also thought of a do/while loop such a:
int i = 1;
do{
x = x + xVel;
y = y + yVel;
i++;
}while (i < 100);

but this just creates all sorts of undesireable effects.

Any help will be highly appreciated.

Thanks

James
 
James said:
...
I also thought of a do/while loop such a:
int i = 1;
do{
x = x + xVel;
y = y + yVel;
i++;
}while (i < 100);

Assuming you don't want to use some fancy runge-kutta formulas, I'd change
that loop to the following:

double t = 0;
const double dt = 1e-3; // time step per iteration
do{
x = x + xVel*dt;
y = y + yVel*dt;
t+=dt;
}while (t < 100);
but this just creates all sorts of undesireable effects.

Really?
There's no input/output in that loop - I didn't think it would produce any
effect...

If you only want to see something, use Console.WriteLine to print out the
calculated coordinates.

For more advanced graphics, you probably should create a WinForm
application; The MSDN articles on that topic aren't bad, and there are
plenty of good tutorials on the web!

Niki
 
You're also not accounting for gravity...

Keep in mind that the screen x-coordinates increase from left to right and
the y-coordinates increase from bottom to top, so you'll probably want to
invert the y coordinates when drawing, otherwise your cannonball will fly in
an upside-down arc.

If you want to keep things simple, I'd create a basic form and place a panel
on the form. Then you can draw your cannonball on the panel.

The way I'd handle it, and this will add some complexity for you, is to use
a timer. The timer will activate an event, say every second. Each time, you
get that, you can do something like "myPanel.Invalidate()" which will then
activate the Paint event, which you should use to draw your cannonball.

Instead of doing things in a loop, I'd simply update the coordinates of the
cannonball each time the timer event is invoked.

Assuming you draw the cannonball in a method called "DrawCannonball(int x,
int y)", to calculate the cannonball's location (with the inverted y axis),
you would use code something like:

DrawCannonball(x, myPanel.Height - y);

Hope this helps give you a start.

Pete
 
James said:
I also thought of a do/while loop such a:

Before drawing trajectory I would check what's the size of Panel? IMHO you
only need to have y values for some limited (by number of screen pixels)
number of x values.

RGDS PSG
 
Here's some pseudo-code for you...

while(isSimulating)
{
dt = time() - oldtime;
oldtime = time();
x += xVel*dt;
yVel += gravity*dt;
y +=yVel*dt;
redrawScene();
}
 
Back
Top