#Player only rotates counterclockwise despite the angle difference cout looking good

5 messages · Page 1 of 1 (latest)

sleek scarabBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dreamy ruin
#

;format clang gnu

karmic pecanBOT
# dreamy ruin ;format clang gnu
void
Player::handleMouseRotation (int mouse_x, int mouse_y)
{
    const float rotationSpeed = 0.01f;
    const float deltaTime = 0.05f;

    // Only update rotation if the mouse has moved
    if (mouse_x != last_mouse_x_position || mouse_y != last_mouse_y_position)
	{
	    // 1. Calculate the direction from the player to the mouse
	    glm::vec2 direction = glm::vec2 (mouse_x - mPosX, mouse_y - mPosY);

	    // Calculate the angle between player and mouse (target angle)
	    float targetAngle = glm::atan (direction.y, direction.x);

	    // Normalize the target angle to the range [-PI, PI]
	    targetAngle = glm::mod (targetAngle + glm::pi<float> (),
	                            2 * glm::pi<float> ())
	                  - glm::pi<float> ();

	    // 2. Get the current angle of the player (relative to its facing
	    // direction)
	    float currentAngle = getCurrentPlayerAngle ();

	    // Normalize currentAngle to the range [-PI, PI]
	    currentAngle = glm::mod (currentAngle + glm::pi<float> (),
	                             2 * glm::pi<float> ())
	                   - glm::pi<float> ();

	    // 3. Calculate the angle difference
	    float angleDifference = targetAngle - currentAngle;

	    // Normalize the angle difference to ensure it's the shortest path
	    if (angleDifference > glm::pi<float> ())
		{
		    // Wrap to negative range
		    angleDifference -= 2.0f * glm::pi<float> ();
		}
	    else if (angleDifference < -glm::pi<float> ())
		{
		    // Wrap to positive range
		    angleDifference += 2.0f * glm::pi<float> ();
		}

	    // PRINT cout angle difference
	    std::cout << "Angle Difference: " << angleDifference << "\n";

Powered by godbolt.org

sleek scarabBOT
#

@dreamy ruin

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

dreamy ruin
#

;format clang gnu