Hey guys, here's an update on my Missile Command game:
I did some sketches on graph paper to flesh out my thoughts so the logic wasn't so abstract for me, and after 2-3 days of just working through it, I finally got the cannon to follow the mouse! Yay!
It's not perfect but it's a start.
Here's a screenshot GIF of it:
http://i.imgur.com/sEV0XKU.gifv
Man that took me over 2 weeks to figure out, and I wanted to thank MarcusAseth, Aerodactyl55 and cmac for their wonderful feedback in helping me out.
What finally got it to work was actually my center point, it was in the wrong position!
Instead of the "center bottom" of the cannon pipe, I changed it to the dead center of the pipe (width / 2, height / 2), relative to the drawing rectangle of the cannon itself, called atan2 with the displacement between the mouse x and y and the cannon's actual x and y and voila it worked!
Here's the updated code:
Game.cpp
void Game::Update(float delta)
{
// Game logic
// Input, get mouse position to determine
// the cannon pipe's angle for rendering
int mX, mY;
SDL_GetMouseState(&mX,&mY);
// "Snap" the cannon angle to the angle between
// cannon and mouse
SetCannonAngle( mX - cannon->x, mY - cannon->y );
// If there are no more silos,
// start a new game
if(GetSiloCount() == 0)
{
NewGame();
}
// Update the cannon's angle
cannon->Update(delta);
/*
Test Data
*/
// Stream the X, Y, Angle and Center values
xValue.str(""); // Clear the stream before piping the xValue
xValue << "X: " << mX;
yValue.str(""); // Clear the stream before piping the yValue
yValue << "Y: " << mY;
angleValue.str(""); // Clear the stream before piping the angleValue
angleValue << "Angle: " << cannon->angle;
// Set font color to WHITE
SDL_Color textColor = {255,255,255};
//***************************************
// DEBUG - Prepare the fonts for testing
//***************************************
// Render the X-coordinate text
SDL_Surface* temp = TTF_RenderText_Solid( fontMouseX, xValue.str().c_str(), textColor );
xTexture = SDL_CreateTextureFromSurface( renderer, temp );
xWidth = temp->w;
xHeight = temp->h;
SDL_FreeSurface(temp);
// Render the Y-coordinate text
temp = TTF_RenderText_Solid( fontMouseY, yValue.str().c_str(), textColor );
yTexture = SDL_CreateTextureFromSurface( renderer, temp );
yWidth = temp->w;
yHeight = temp->h;
SDL_FreeSurface(temp);
// Render the angle text
temp = TTF_RenderText_Solid( fontCannonAngle, angleValue.str().c_str(), textColor );
aTexture = SDL_CreateTextureFromSurface( renderer, temp );
aWidth = temp->w;
aHeight = temp->h;
SDL_FreeSurface(temp);
}
This was the game changer - I initialized the center coordinates to the very center of the texture:
Cannon.cpp
// Set the cannon pipe's rotation pivot point,
// relative to the cannon pipe
center.x = (width / 2);
center.y = (height / 2);
After this, the angling worked. Now I have to figure out how to get the cannon to not angle beyond 0 and 180 degrees and stay above ground.
Oh and I'm not sure if these coordinate values are correct or not, but when the angle is 45 degrees, it is facing bottom-right, straight down at 90, to the left at 180, top left at 225, straight up at 270 and top-right at 315 degrees respectively.
Is this correct?
EDIT: The above degrees were from my test data and not actually shown in the animated GIF. For some reason the angle value shows negative values for the angles, and right now I'm not sure how to convert that so it shows values from 0 to 360 degrees.
As my game is not done, is it proper convention to continue the post?
Or do I open a new post regarding the next logic challenge of my Missile Command game when I run into trouble?
Once again, I appreciate everybody's help. I'll go to sleep now, goodnight.