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.


Topics - guitarmatt99

Pages: [1]
1
General / Simple Collision Help
« on: November 10, 2013, 03:35:15 pm »
Hello! I have been recently experiment with collision between two shapes but I can't quite get it to work. It only worked with two opposite sides.

How would I do collision correctly?

Here's some minimal code that detects the collision but doesn't stop movement. Movement it controlled by holding the mouse button down and dragging.

I have removed code that isn't affecting anything.

int main()
{
    //Player
    sf::RectangleShape player(sf::Vector2f(80.f, 80.f));
    player.setOrigin(40.f, 40.f);
    player.setPosition(400.f, 40.f);

    //Enemy
    sf::RectangleShape enemy(sf::Vector2f(80.f, 80.f));
    enemy.setOrigin(40.f, 40.f);
    enemy.setPosition(600.f, 350.f);
    enemy.setFillColor(sf::Color(232,74,63));

    while(window.isOpen())
    {
        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            player.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));

        //Collision
        if(enemy.getGlobalBounds().intersects(player.getGlobalBounds()))
            player.setFillColor(sf::Color(40,39,39));
        else
            player.setFillColor(sf::Color(170,227,210));

        //Render
        window.clear(sf::Color(240, 240, 240));
        window.draw(player);
        window.draw(enemy);
        window.display();
    }
}
 

2
General / (SOLVED) Collision Problem with Sprite
« on: October 29, 2013, 07:45:14 pm »
I have fixed it, I needed to set the origin for the sprite.

Hello, I have found some code that implements gravity and tried to do it with a sprite (the other code used shapes), it works fine while using shapes but not when using a texture, the sprite doesnt go on top of the floor, it places itself underneath it.

Here's the code I based mine off - https://github.com/eXpl0it3r/Examples/blob/master/SFML/SimpleAABB.cpp

And here's mine:

int main()
{
    //WINDOW
    sf::RenderWindow window(sf::VideoMode(800, 600), "Gravity");
    window.setFramerateLimit(60);

    //FRAMETIME
    sf::Clock frametime;

    //PLAYER DATA
    enum playerDirection{ Left, Right };
    sf::Vector2i playerPosition(600 - 32, 350 - 32);
    sf::Vector2i playerSource(1, Right);
    sf::Vector2f playerSpeed(0.f, 0.f);

    //GRAVITY
    const float gravity = 980.f;

    //COLLISION
    bool inAir = true;

    //SHAPES
    sf::RectangleShape floor(sf::Vector2f(800.f, 40.f));
    floor.setPosition(0.f, 560.f);
    floor.setFillColor(sf::Color(10, 180, 30));

    //TEXTURES
    sf::Texture playerTexture;
    if(!playerTexture.loadFromFile("playerTexture.png"))
        std::cout << "Texture Error" << std::endl;

    //SPRITES
    sf::Sprite player;
    player.setTexture(playerTexture);
    player.setPosition(playerPosition.x, playerPosition.y);
    player.setTextureRect(sf::IntRect(playerSource.x * 32, playerSource.y * 32, 32, 32));

    //GAME LOOP
    while (window.isOpen())
    {
        float dt = frametime.restart().asSeconds();

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

        //SLOW HORIZONTAL SPEED
        if(playerSpeed.x > 6.f)
            playerSpeed.x -= 5.f;
        else if(playerSpeed.x < -6.f)
            playerSpeed.x += 5.f;
        else
            playerSpeed.x = 0.f;

        //ADJUST VERTICLAL SPEED
        if(!inAir)
            playerSpeed.y = gravity;
        else if(playerSpeed.y < gravity)
            playerSpeed.y += 10.f;
        else if(playerSpeed.y > gravity)
            playerSpeed.y = gravity;

        //HORIZONTAL MOVEMENT
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            playerSpeed.x = 0.f;
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //Move Right
            playerSpeed.x = 120.f;
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //Move Left
            playerSpeed.x = -120.f;

        //JUMPING
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && !inAir)
            playerSpeed.y = -300.f;

        //MOVE PLAYER
        player.setPosition(player.getPosition().x + playerSpeed.x * dt, player.getPosition().y + playerSpeed.y * dt);

        //CHECK COLLISION
        if(floor.getGlobalBounds().intersects(player.getGlobalBounds()))
        {
            player.setPosition(player.getPosition().x, floor.getPosition().y - player.getOrigin().y);
            inAir = false;
        }
        else
            inAir = true;

        //RENDER
        window.clear();
        window.draw(player);
        window.draw(floor);
        window.display();
    }
    return 0;
}
 

3
General / Player Movement with Classes.
« on: October 26, 2013, 12:20:20 pm »
Hello, I am still learning SFML and have created a basic program where you can move a character around the screen with an animation. You can also sprint which also speeds up the animation!

I wanted to ask if I have done everything correctly and you can correct me if not.

main.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include "player.h"

int main()
{
    //Window
    sf::RenderWindow window(sf::VideoMode(1200, 700), "Testing");
    window.setKeyRepeatEnabled(false);

    //Load Player Texture
    sf::Texture playerTexture;
    if(!playerTexture.loadFromFile("IMAGE/playerSprite.png"))
        std::cout << "Texture Error" << std::endl;

    //OBJECTS
    Player player(playerTexture); //Player

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

        window.clear(); //Clear Window

        //PLAYER MOVEMENT
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) //Move Up
            player.moveUp();

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) //Move Down
            player.moveDown();

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //Move Right
            player.moveRight();

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //Move Left
            player.moveLeft();

        //SPRINT
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
            player.setSpeed(0.35, sf::milliseconds(50));
        else
            player.setSpeed(0.20, sf::milliseconds(80));

        window.draw(player.getSprite()); //Draw Player Sprite
        window.display(); //Display Window
    }

    return 0;
}
 


player.h

#ifndef PLAYER_H
#define PLAYER_H

class Player
{
    public:
        Player(); //Constructor
        Player(sf::Texture&); //Overload Constructor
        ~Player(); //Destructor

        //PLAYER MOVEMENT FUNCTIONS
        void moveUp();
        void moveDown();
        void moveRight();
        void moveLeft();
        void setSpeed(float, sf::Time);

        //ACCESSOR FUNCTIONS
        sf::Sprite getSprite() const;

    private:
        sf::Sprite _Sprite; //Declare Player Sprite
        sf::Vector2i _Source; //Declare Source (Sprite Sheet Crop)
        enum _Direction{ Down, Left, Right, Up }; //Declare Direction Enumeration

        //ANIMATION DATA
        float _Speed; //Player Speed
        sf::Clock _AnimClock; //Player Animation Clock
        sf::Time _AnimTime; //Player Animation Time
};

#endif // PLAYER_H
 


player.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include "player.h"

Player::Player() //Constructor
{

}

Player::Player(sf::Texture& TEMP_Texture) //Overload Constructor
{
    //PLAYER SPRITE
    _Sprite.setTexture(TEMP_Texture); //Set Sprite Texture
    _Sprite.setPosition(600 - 32, 350 - 32); //Set Sprite Position (Centre)
    _Sprite.setScale(1.5f, 1.5f); //Sprite Scale
    sf::Vector2i _Source(1, Down); //Default Sprite Sheet Crop
    _Sprite.setTextureRect(sf::IntRect(_Source.x * 32, _Source.y * 32, 32, 32)); //Crop Sprite Sheet (Default Crop)

    //PLAYER / ANIMATION SPEED
    _AnimTime = sf::milliseconds(80); //Animation Speed
    _Speed = 0.20; //Player Speed
}

Player::~Player() //Destructor
{
}

//ACCESSOR FUNCTIONS
sf::Sprite Player::getSprite() const //Player Sprite
{
    return _Sprite;
}

//PLAYER MOVEMENT
void Player::moveUp()
{
    _Source.y = Up; //Set '_Source.y' Equal To 'Up' (_Direction Enum)
    _Sprite.move(0, -_Speed); //Move Player Sprite

    if(_AnimClock.getElapsedTime() > _AnimTime)
    {
        _Sprite.setTextureRect(sf::IntRect(_Source.x * 32, _Source.y * 32, 32, 32)); //Crop Sprite Sheet

        //Animation
        _Source.x++;
        if(_Source.x * 32 >= _Sprite.getTexture()->getSize().x)
        {
            _Source.x = 0;
        }
        _AnimClock.restart();

    }

}

void Player::moveDown()
{
    _Source.y = Down; //Set '_Source.y' Equal To 'Up' (_Direction Enum)
    _Sprite.move(0, _Speed); //Move Player Sprite

    if(_AnimClock.getElapsedTime() > _AnimTime)
    {
        _Sprite.setTextureRect(sf::IntRect(_Source.x * 32, _Source.y * 32, 32, 32)); //Crop Sprite Sheet

        //Animation
        _Source.x++;
        if(_Source.x * 32 >= _Sprite.getTexture()->getSize().x)
        {
            _Source.x = 0;
        }
        _AnimClock.restart();

    }
}

void Player::moveRight()
{
    _Source.y = Right; //Set '_Source.y' Equal To 'Up' (_Direction Enum)
    _Sprite.move(_Speed, 0); //Move Player Sprite

    if(_AnimClock.getElapsedTime() > _AnimTime)
    {
        _Sprite.setTextureRect(sf::IntRect(_Source.x * 32, _Source.y * 32, 32, 32)); //Crop Sprite Sheet

        //Animation
        _Source.x++;
        if(_Source.x * 32 >= _Sprite.getTexture()->getSize().x)
        {
            _Source.x = 0;
        }
        _AnimClock.restart();
    }
}


void Player::moveLeft()
{
    _Source.y = Left; //Set '_Source.y' Equal To 'Up' (_Direction Enum)
    _Sprite.move(-_Speed, 0); //Move Player Sprite

    if(_AnimClock.getElapsedTime() > _AnimTime)
    {
        _Sprite.setTextureRect(sf::IntRect(_Source.x * 32, _Source.y * 32, 32, 32)); //Crop Sprite Sheet

        //Animation
        _Source.x++;
        if(_Source.x * 32 >= _Sprite.getTexture()->getSize().x)
        {
            _Source.x = 0;
        }
        _AnimClock.restart();
    }
}

void Player::setSpeed(float TEMP_Speed, sf::Time TEMP__AnimTime) //Sprint Speed
{
    _Speed = TEMP_Speed;
    _AnimTime = TEMP__AnimTime;
}
 

4
General / Accessing Variables in Functions in Classes!
« on: September 30, 2013, 08:23:21 pm »
Hello, I know there is no SFML in my code but my code was very long so I recreated the problem with a simple program.

What am I doing wrong?

I want to access the String variable that is in a function called MyFunction(); which is in a class called MyClass() but it says String was not declared in this scope.

main.cpp

#include <iostream>
#include <string>
#include "myclass.h"
using namespace std;

int main()
{
    MyClass MyClassOb;
    MyClassOb.MyFunction();

    cout << String.size();
    return 0;
}
 

myclass.cpp

#include "myclass.h"
#include <iostream>
using namespace std;

MyClass::MyClass()
{
    //ctor
}

MyClass::void MyFunction();
{
    string String = "hello";
}
 

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
    public:
        MyClass();
        void MyFunction();
};

#endif // MYCLASS_H
 

5
General / Moving Spites Help! (Speed Problem)
« on: September 28, 2013, 08:27:56 pm »
Hello there! I have recently played around with moving characters (with animation) from a single sprite sheet. I want to know whether I have done this correctly or in the most efficient way? (it does work!)

My sprite sheet contained 4 different angles of my character with 3 different positions for each. ( 96 x 128 )

EDIT: I am also having a problem with the speed at which is goes through the animation (not the speed it moves across the screen), is there anyway to control how fast it cycles through the sprite sheet?

Here's my code!

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>

int main()
{
    enum Direction{ Down, Left, Right, Up };
    sf::Vector2i source(1, Down);

    sf::RenderWindow window(sf::VideoMode(800, 450), "Testing", sf::Style::Fullscreen);

    sf::Texture pTexture;
    sf::Sprite pChar;

    //load sprite sheet
    if(!pTexture.loadFromFile("image.png"))
        std::cout << "Error" << std::endl;

    pChar.setTexture(pTexture);

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

        window.clear();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            source.y = Up;
            pChar.move(0, -0.1);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            source.y = Down;
            pChar.move(0, 0.1);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            source.y = Right;
            pChar.move(0.1, 0);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            source.y = Left;
            pChar.move(-0.1, 0);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        {
            window.create(sf::VideoMode(800, 450), "Testing", sf::Style::Default);
        }

        pChar.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        window.draw(pChar);
        window.display();

    }

    return 0;
}
 

ALSO, when I change the size of my window so does the sprite, how do I give it a fixed size?

6
General / Set up Help!
« on: June 22, 2013, 03:50:29 pm »
Hello! I am new to SFML and I can't get it to work with Code::Blocks 12.11.

I downloaded the "GCC 4.7 MinGW(DW2) - 32 bits" SFML 2.0 file if that is any use!

I have set everything as posted in the"SFML and Code::Blocks(MinGW)" but when I build and run the program, the console comes up but it immediately says that "the program is not responding".

All my files are in the correct folders but I don't know what is wrong!

Please help!

Pages: [1]
anything