c++ - Issue with angle limiting -


i creating game using sfml 2.2 , box2d. game similar peggle in have cannon attached top of screen. have movement of cannon working limit rotation cannot rotate , point straight (off screen). have implemented code below , works extent. cannon can move fine within bounds, if hits 1 of 2 edges becomes stuck there. if press opposite direction, instead of rotating around jumps opposite edge, causing cannon either way right or way left.

in game, when cannon pointing straight down angle 0/360 degrees, way left 90 degrees , way right 270 degrees.

//******************************/ // aiming controls //******************************/ if (sf::keyboard::iskeypressed(sf::keyboard::key::left)) {     float sin = sinf(-m_frotationspeed*elapsedtime);     float cos = cosf(-m_frotationspeed*elapsedtime);      sf::vector2f newdirection;     newdirection.x = (cos*m_vaimdirection.x) - (sin * m_vaimdirection.y);     newdirection.y = (sin*m_vaimdirection.x) + (cos*m_vaimdirection.y);        //update sprite rotation     // find angle of rotation acos(v1dotv2)     float rotationangle = acosf(1 * m_vaimdirection.y);      // check make sure right direction!     if (m_vaimdirection.x < 0)         rotationangle = (b2_pi*2.0f) - rotationangle;      // convert degrees     rotationangle *= 180.0f / b2_pi;       // here check bounds     if (rotationangle > 88.0f && rotationangle < 272.0f)         rotationangle = 88.0f;     else         m_vaimdirection = newdirection;       //apply new rotation     m_scannon.setrotation(rotationangle); } else if (sf::keyboard::iskeypressed(sf::keyboard::key::right)) {     float sin = sinf(m_frotationspeed*elapsedtime);     float cos = cosf(m_frotationspeed*elapsedtime);      sf::vector2f newdirection;     newdirection.x = (cos*m_vaimdirection.x) - (sin * m_vaimdirection.y);     newdirection.y = (sin*m_vaimdirection.x) + (cos*m_vaimdirection.y);        //update sprite rotation     // find angle of rotation acos(v1dotv2)     float rotationangle = acosf(1 * m_vaimdirection.y);      // check make sure right direction!     if (m_vaimdirection.x < 0)         rotationangle = (b2_pi*2.0f) - rotationangle;      // convert degrees     rotationangle *= 180.0f / b2_pi;       // here check bounds     if (rotationangle < 272.0f && rotationangle > 88.0f)         rotationangle = 272.0f;     else         m_vaimdirection = newdirection;     //apply new rotation     m_scannon.setrotation(rotationangle); } 


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -