Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Unexpected crash  (Read 1049 times)

0 Members and 1 Guest are viewing this topic.

madc0de

  • Newbie
  • *
  • Posts: 2
    • View Profile
Unexpected crash
« on: March 04, 2012, 12:44:10 am »
Code: [Select]

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>

using std::cout;
using std::endl;

#include "RenderManager.h"

int relativeX, relativeY;
float speed = 0.05;

const float PI = 3.14159265;

int rotation;

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    // Render Manager
    cge::RenderManager myRenderManager(window);

    // Load a sprite to display
    sf::Texture texture;
    if (!texture.LoadFromFile("missile.gif"))
        return EXIT_FAILURE;
    sf::Sprite missile(texture);
    myRenderManager.add(missile);

    // Start the game loop
    while (window.IsOpened())
    {
        // ## PROCESS ##
        if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Escape))
        {
            window.Close();
            //return 0;
        }
           
        relativeX = sf::Mouse::GetPosition(window).x - missile.GetPosition().x;
        relativeY = sf::Mouse::GetPosition(window).y - missile.GetPosition().y;

        rotation = atan(relativeX/relativeY)*180/PI;

        if (sf::Mouse::GetPosition(window).x > missile.GetPosition().x)
            missile.Move(.05, 0);
        else
            missile.Move(-.05, 0);

        if (sf::Mouse::GetPosition(window).y > missile.GetPosition().y)
            missile.Move(0, .05);
        else
            missile.Move(0, -.05);

        // ## OUTPUT ##
        //std::cout<< sf::Mouse::GetPosition().x<< std::endl;
        //std::cout<< missile.GetPosition().x<< std::endl;
        //std::cout<< atan(0.1)*180/PI<< endl;

        // Clear screen
        window.Clear();

        // Draw the sprite
        myRenderManager.draw();

        // Update the window
        window.Display();
    }

    return 0;
}


I want to compute arctan of relativeX and Y, but if i use (relativeX/relativeY) anywhere in program it crashes after about 3 seconds. So it compiles. There is no error, or exception even a warning. Nothing.

I'm stuck. :(

Thanks.

madc0de

  • Newbie
  • *
  • Posts: 2
    • View Profile
Unexpected crash
« Reply #1 on: March 04, 2012, 01:10:57 am »
I think i fixed it.

At some point relativeY becomes = 0 so,

relativeX / relativeY becomes like 1432 / 0.. And that cause the problem, i think.

There was no errors though.

Sorry.  :oops:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Unexpected crash
« Reply #2 on: March 04, 2012, 02:18:51 pm »
You should use atan2 instead of atan.
Laurent Gomila - SFML developer

 

anything