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

Author Topic: Mouse and sprite distance formula always return true...  (Read 3224 times)

0 Members and 1 Guest are viewing this topic.

kadajett

  • Newbie
  • *
  • Posts: 5
    • View Profile
Mouse and sprite distance formula always return true...
« on: December 16, 2009, 04:50:15 am »
Any Ideas?
Code: [Select]
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
using namespace std;

bool checkCollision(int x1, int x2, int y1, int y2);

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

sf::Image Gah;
sf::Input input;



if(!Gah.LoadFromFile("Arrow.png"))
{return 1;}
sf::Sprite sprite;
sprite.SetImage(Gah);





sprite.SetPosition(100.0, 100.0);
    // Start game loop
    while (App.IsOpened())
    {
sf::Event Event;



        // Process events
       
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }



sprite.FlipX(true);
}


        // Clear the screen (fill it with black color)
        App.Clear();
cout<< "drawing sprite"<<endl;
App.Draw(sprite);

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}



bool checkCollision(int x1, int x2, int y1, int y2)
{
float dis;

dis = sqrt((float)( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) );

if(dis <= 30)
{
cout << "collide!" << endl;
return true;
}
if(dis >= 31)
{
cout<< "not collide" << endl;
return false;
}

return false;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mouse and sprite distance formula always return true...
« Reply #1 on: December 16, 2009, 08:26:05 am »
And where is it called? With what values?
Laurent Gomila - SFML developer

kadajett

  • Newbie
  • *
  • Posts: 5
    • View Profile
Mouse and sprite distance formula always return true...
« Reply #2 on: December 16, 2009, 12:25:22 pm »
My fault :) I must have deleted it when I was copy pasting it to the forum

Code: [Select]
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include <string>

using namespace std;

bool checkCollision(int x1, int x2, int y1, int y2);

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

    sf::Image Gah;
    if(!Gah.LoadFromFile("Arrow.png"))
        return 1;

    sf::Sprite sprite;
    sprite.SetImage(Gah);

    sf::Font MyFont;
    sf::String mouseX("Hello World", MyFont, 30.0 );

    sprite.SetPosition(100.0, 100.0);
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        if (checkCollision(sprite.GetPosition().x, input.GetMouseX(), sprite.GetPosition().y, input.GetMouseY()))
        {
            sprite.FlipX(true);
        }

        // Clear the screen (fill it with black color)
        App.Clear();

        App.Draw(sprite);
        App.Draw(mouseX);
        if (checkCollision(sprite.GetPosition().x, input.GetMouseX(), sprite.GetPosition().y, input.GetMouseY()))
        {
            sprite.FlipX(true);
        }
        if (!checkCollision(sprite.GetPosition().x, input.GetMouseX(), sprite.GetPosition().y, input.GetMouseY()))
        {
            sprite.FlipX(false);
        }

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}

bool checkCollision(int x1, int x2, int y1, int y2)
{
    float dis = sqrt((float)( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) );

    if (dis <= 150)
    {
        cout << "collide!" << endl;
        return true;
    }
    else
    {
        cout<< "not collide" << endl;
        return false;
    }

    return false;
}


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mouse and sprite distance formula always return true...
« Reply #3 on: December 16, 2009, 12:41:54 pm »
Hi

I took the liberty to format your code, it was hardly readable.

The problem is simple, you use an empty sf::Input instance instead of retrieving App.GetInput().
Laurent Gomila - SFML developer

kadajett

  • Newbie
  • *
  • Posts: 5
    • View Profile
Mouse and sprite distance formula always return true...
« Reply #4 on: December 16, 2009, 12:59:54 pm »
Sorry about the readability, I was so tired when I wrote that when I was half asleep and I was getting frustrated. Thanks for the help, it worked. Kinda angry cause I didn't see anything about that in my reading :P

I appreciate it.

kadajett

  • Newbie
  • *
  • Posts: 5
    • View Profile
Mouse and sprite distance formula always return true...
« Reply #5 on: December 16, 2009, 05:23:51 pm »
Now that I look at that code, it makes me want to cry a little. I am throwing it all into classes now. Since I have a basic knowledge of  SFML. I am renaming all of the variables to go along with my style. And my game is progressing very well.

 

anything