I have a horrible problem that's making my brain box hurt. I'm writing a 'vector' style Missile Command game with the Windows API. Everything has been going great. I've got the Missile class up and running EXCEPT one huge method. The method is called Move() (takes no parameters and returns void). Its purpose is to take the Missile class's X and Y position member variables and shifts them based on where the missile started out and where its headed. I'll just show you the missile class real quick.
class Missile
{
protected:
//member variables
int m_iPosX, m_iPosY; //where it is
int m_iStartX, m_iStartY; //where it starts
int m_iDestX, m_iDestY; //where its headed (mouse position)
int m_iSpeed; //speed of the missile, in pixels
BOOL m_bSource; //start at left or right? (this won't be used until after testing...so ignore it
public:
//constructor(s)/destructors
Missile(int a, int b, int x, int y, int speed, BOOL leftright);
virtual ~Missile();
//general methods
void Draw(HDC hDC);
void Move();
void Explode();
//accessors
void SetX(int x) { m_iPosX = x; }
int GetPosX() { return m_iPosX; }
void SetY(int y) { m_iPosY = y; }
int GetPosY() { return m_iPosY; }
//void SetStartX(int x) { m_iStartX = x; }
int GetStartX() { return m_iStartX; }
//void SetStartY(int y) {m_iStartY = y; }
int GetStartY() { return m_iStartY; }
void SetDestX(int x) { m_iDestX = x; }
int GetDestX() { return m_iDestX; }
void SetDestY(int y) { m_iDestY = y; }
int GetDestY() { return m_iDestY; }
};
Ok, if some things look a bit disorganized or unnecessary, that's how my classes always start out. Just ignore it, please.
Ok. Here's the Move() method.
void Missile::Move()
{
m_iSpeed = 5;
const maxSpeed = m_iSpeed * 2;
double X, Y, A, B;
}
The way I'm displaying graphics is done completely without any sort of image files. Its all 'vector' style like the original missile command meaning I'm using the Windows API drawing commands. IE creating a pen then using the API functions to draw primitive lines, ellipses, rectangles, etc.
As you can see, this Move() method really doesn't do anything. I can't figure out an all purpose formula that will take the Missile's X and Y position and increase them each frame until it hits the EXACT X and Y position of its destination. Of course, it can be wrong by about 3 to 5 pixels because once the missile explodes the player won't care/know if it was off a little bit.
I wrote a version of Move() that got pretty close but started to fall apart when I tested it with different destination values. Weird behaviors started cropping up.
However, the formula needs to not care how fast (how many pixels) the missile moves, or where its destination is, it just have to figure out how to move the missile in increments until it gets there.
Please don't tell me things like, "Your constructor shouldn't have vague variables as parameters like int a, int b, int x, int y" because I understand the basics of programming. But I prefer to write a working class before I make it all pretty. Otherwise, if you're good at math and know some way to make the missile accurate with all of these different variables then thanks!
If you're patient enough to read it, I came up with a formula that works on paper, but for some reason seems to 'break' when I try and translate it into C++.
Basically it took the X distance from the source to the destination and the Y distance from the source to the destination and divided them through each other until they were turned into percentages. So let's say the X distance is 480 and the Y distance is 180 it would divide them by each other so that the X 'factor' would be 0.625 and the Y 'factor' would be 0.375 (they add up to one.......) then multiply both of those values by the maxSpeed and add the result into the respective X or Y position of the missile. I don't know if that made sense, but anyway, it works on paper but not in C. When I wrote it into the game it made the missile move all funky. I think the Y value wasn't even changing. Anyway, thanks for the help.












