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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - zalkore

Pages: [1]
1
General / Re: sprite rotation for following mouse
« on: January 04, 2013, 12:57:27 am »
I'm definitely starting to recognize that. I guess take everything with a grain of salt eh? ha

I went ahead and picked up SAMS Teach Yourself C++ in One Hour a Day, 6th Edition. Thanks for the help and quick responses everyone!

2
General / Re: sprite rotation for following mouse
« on: January 03, 2013, 05:10:23 pm »
@eXpl0it3r
gah I didn't realize I was doing things so messy. I adjusted the naming to clarify what were member variables and classes, etc.
the boolean was the lack of sleep seeping in heh. glad you caught it.
I was following codingmadeeasy on youtube and he uses an initialize function. I thought that the constructor was for that purpose but didn't look into it to make sure   :-\

as for the passing of the sprite member, I intend to have an array of different "equipable" sprites for the character that will be rotated. therefor, I need to pass which sprite is tracking the mouse.

@masskiller
ah excellent information on keeping the correct origin for the sprites. I hadn't really thought of that heh.


Most importantly, it's working now. Thank you very much!

3
General / sprite rotation for following mouse
« on: January 03, 2013, 03:12:14 pm »
okay this is driving me crazy. I want to rotate a sprite so that it follows the mouse cursor. I know there's quite a few posts on the subject ( I've looked through all that I could find) and my code seems correct so I don't know what to do.

The issue is when I implement the code all inside of main() it runs the way it should. When I put the code inside of a class though, it does this

the rotation doesn't follow the cursor all the way around the screen, nor does it track the angle at a consistent rate.

here's the original code that works:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <math.h>
#include <iostream>


int main()
{
    sf::Texture playerTexture;
    playerTexture.loadFromFile("StickFig.png");

    sf::Sprite playerSprite;
    playerSprite.setTexture(playerTexture);

    playerSprite.setOrigin(50,50);
    playerSprite.setPosition(400,300);
    playerSprite.setRotation(0);

    sf::Vector2i mouse;
    float angle;
    sf::Vector2f playerPosition;
    double a, b;

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        playerPosition = playerSprite.getPosition();
        a = mouse.x - playerPosition.x;
        b = mouse.y - playerPosition.y;

        mouse = sf::Mouse::getPosition(window);
        angle = -atan2( a , b) * 180 / 3.14;
        playerSprite.setRotation(angle);
        window.clear(sf::Color::White);
        window.draw(playerSprite);
        window.display();
    }

    return 0;
}

 

and here's the code using the class that is messing up:

main()
#include <SFML/Graphics.hpp>
#include "player.h"

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
    while (window.isOpen())
    {
        player playerOne;
        playerOne.initialize();
        playerOne.loadContent();

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::White);
        playerOne.draw(window);
        window.display();
    }

    return 0;
}

 

player.cpp

#include "player.h"
#include <iostream>

player::player()
{
    //ctor
}

player::~player()
{
    //dtor
}

void player::initialize()
{
    playerSprite.setOrigin(50,50);
    playerSprite.setPosition(400,300);

}
void player::loadContent()
{
    playerTexture.loadFromFile("player.png");
    playerSprite.setTexture(playerTexture);


}
void player::draw(sf::RenderWindow &window)
{
    this->trackMouse(true, playerSprite, window);
    window.draw(playerSprite);
}
void player::update(sf::RenderWindow &window)
{

}

void player::trackMouse(bool value, sf::Sprite &sprite, sf::RenderWindow &window)
{
    if(value == true)
    {
        window.convertCoords(mouse);

        //gets sprite origin coordinates and mouse coordinates
        this->spritePosition = sprite.getPosition();
        this->mouse = sf::Mouse::getPosition();

        mouseAngle = -atan2( mouse.x - spritePosition.x , mouse.y - spritePosition.y) * 180 / 3.14159; //angle in degrees of rotation for sprite

        playerSprite.setRotation(mouseAngle);
    }
}
 

any help would just be fantasmical

4
General / Re: Help understanding the tutorials?
« on: January 02, 2013, 10:05:00 am »
I know this is an older thread but seeing as how I was having trouble with this same issue (and this post was among the top google results) I thought I'd post my solution. The cause for the error is slightly different than the OP but still, might help.

In the Project Build Options under Linker Settings, I had the dependencies in the wrong order.  Changing these into the correct ordered fixed my errors.

Pretty simple oversight heh.

p.s.
Excellent explanation VPellen, thank you.

Pages: [1]
anything