x

Started by
2 comments, last by Carbon101 9 years, 5 months ago

...

Advertisement

Hello there, you have made a good start. There are two ways of making the tracectory: A so-called analytic one, where the entire trajectory is computed once and for all. And a discrete one, where the projectile moves a little step at a time. For each step, gravity, and perhaps air resistance is added, and when you repeat the steps, the trajectory arc appears. The first method is good if you just want the trajectory and wants to know if you hit or not. The other one is good if you want an actual cannon ball which can bounce off planes, roll around, and be influenced by many different forces.

The code you showed seems very focused on angles. Once the bullet leaves the cannon, you don't need angles at all. Your initial velocity will be the vector (cos(cannon_angle), sin(cannon_angle)) * speed, and your initial position is the cannon's position. From that point, there are two ways you can proceed, as h4tt3n explained.

Analytically:

Point projectile_position_after_time(double time) {
  return initial_position + initial_velocity * time + acceleration * 0.5 * time * time;
}

Numerically:
void update(double delta_time) {
  Vector acceleration = gravity / mass;
  velocity += acceleration * delta_time;
  position += velocity * delta_time;
}

I would do it numerically, because that way you don't limit the kinds of interactions you may want to add later on (drag, bouncing off of things, etc.).

...

This topic is closed to new replies.

Advertisement