On 7/18/2017 at 0:28 AM, Glydion said: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); // Create the target vector, normalize it // and pass it to the cannon to set the // current angle target = new Vector2(mX,mY); SetCannonAngle( target ); ... } void Game::SetCannonAngle( Vector2* theVector ) { // Calculate arc tangent between the // the mouse's position and the center // axis of the cannon and convert to degrees double theAngle = atan2( theVector->x, theVector->y ); theAngle = ConvertDegrees(theAngle); cannon->angle = theAngle; } double Game::ConvertDegrees(double radians) { // Convert the angle from radians to // degrees and return the value return radians * ( 180 / 3.141592653589793238 ); }
Looks like you're assuming the cannon is at the world origin. You want to know the angle between the cannon and the mouse cursor, but you're testing the angle between the world origin and the mouse cursor. Subtract the cannon's position from "theVector" to get the displacement vector.
Additionally, you should store your conversion constants in variables or macros to prevent unnecessary arithmetic. Eg.
#define MATH_180_OVER_PI (57.295779513082320876798154814105f)
#define MATH_PI_OVER_180 (0.01745329251994329576923690768489f)
#define RAD_TO_DEG(rad) (rad*MATH_180_OVER_PI)
#define DEG_TO_RAD(deg) (deg*MATH_PI_OVER_180)