Greetings from a newbie!

I'm creating a game of a sort where objects move and collide on a 2D ASCII map ( vector<vector if u will ). I'm having hard time calculating the projectories of ranged weaponry. Not sure if this problem is more mathematical than C++ related, but here goes.

When shooting a missile I create an object of the class Missile. It's member function moves it from source (x0,y0) to destination(x1,y1) moving one square at a time, in otherwords updating the objects private variables.

int x_; int y_;

Any tips how I should calculate the change in those variables in C++ ? A slope (maths!) would do the trick in algebra, but this puzzles me...

-Topi

This does have to do with a slope, but of a different sort. When you mean - moving an object in a 2D vector, what do you mean? Do you mean the STL's std::vector ? or mathematical vectors?

^

Yes, STL:s vector.
The so called 2D vector :

vector< vector< char > > v;  // this would be the 2D vector
v.at( x_ ).at( y_)  // and this would be the character on the "map"
// at (x_,y_)

Okay, in view of this, I would recommend …

## All 5 Replies

This does have to do with a slope, but of a different sort. When you mean - moving an object in a 2D vector, what do you mean? Do you mean the STL's std::vector ? or mathematical vectors?

^

Yes, STL:s vector.
The so called 2D vector :

vector< vector< char > > v;  // this would be the 2D vector
v.at( x_ ).at( y_)  // and this would be the character on the "map"
// at (x_,y_)

^

Yes, STL:s vector.
The so called 2D vector :

vector< vector< char > > v;  // this would be the 2D vector
v.at( x_ ).at( y_)  // and this would be the character on the "map"
// at (x_,y_)

Okay, in view of this, I would recommend you employ the concept of mathematical vectors, which would allow you to calculate distances, trajectories etc between two discrete points on a graph (In your case, the graph would be the 2D Vector). Vectors(Mathematics) are immensely useful in the world of computer graphics and game design. A good tutorial on vector math, and its uses in computer graphics can be found here.

You should perhaps look laws governing the trajectories. I am not going to derive them here (although proof is trivial from Newtons Equation of Motion) but am listing the paramatarized (wrt time) equations governing projectiles.
$$x=v_0\times cos(\theta) \times t\$$
and
$$y=(v_{0}\times sin(\theta) \times t) - (\frac{1}{2}\times g\times t^2)$$

where $$v_0$$ is the initial speed, $$\theta$$ is the angle of projection from horizontal, $$g$$ is accelaration due to gravity (nearly $$9.8m/s^2$$) and $$t$$ is time.

You should perhaps look laws governing the trajectories. I am not going to derive them here (although proof is trivial from Newtons Equation of Motion) but am listing the paramatarized (wrt time) equations governing projectiles.
$$x=v_0\times cos(\theta) \times t\$$
and
$$y=(v_{0}\times sin(\theta) \times t) - (\frac{1}{2}\times g\times t^2)$$

where $$v_0$$ is the initial speed, $$\theta$$ is the angle of projection from horizontal, $$g$$ is accelaration due to gravity (nearly $$9.8m/s^2$$) and $$t$$ is time.

These two formulae are for bodies that are in parabolic motion (or semi-parabolic)... these would make sense for the "ranged weaponry" mentioned, but for normal, planar motion, you would have to employ some other means to find their trajectories. Anyway, in both cases, you would have to employ the idea of vectors...

As for collisions, you would have to decide if the bodies are going to collide in a frictionless environment (not realistic, but simpler to implement in code), or vice-versa. Accordingly, you would have to figure out whether the collisions are elastic or inelastic (all this is physics), and employ the respective equations.

There's a wealth of information regarding this on the web, but you will have to grasp the concept of motion in two dimensions properly to understand some equations...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.