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 - guitarmatt99

Pages: [1] 2
1
General / Re: Simple Collision Help
« on: November 10, 2013, 09:16:21 pm »
That looks complicated, am I under estimating simple collision? I know what I did works when you comment the other 3 statements but when you include them all it breaks, I just want to know why it breaks.

2
General / Re: Simple Collision Help
« on: November 10, 2013, 05:10:07 pm »
Thanks for answering but when I try this something goes wrong when positioning.

Did you mean do this?

if(player.getPosition().y + player.getOrigin().y >= enemy.getPosition().y - enemy.getOrigin().y){
    player.setPosition(player.getPosition().x, enemy.getPosition().y - player.getSize().y);
}
else if(player.getPosition().y - player.getOrigin().y <= enemy.getPosition().y + enemy.getOrigin().y){
    player.setPosition(player.getPosition().x, enemy.getPosition().y + player.getSize().y);
}
else if(player.getPosition().x - player.getOrigin().x <= enemy.getPosition().x + enemy.getOrigin().x){
    player.setPosition(enemy.getPosition().x + player.getSize().x, player.getPosition().y);
}
else if(player.getPosition().x + player.getOrigin().x >= enemy.getPosition().x - enemy.getOrigin().x){
    player.setPosition(enemy.getPosition().x - player.getSize().x, player.getPosition().y);
}
 

This is the way I tried before, if you can get it working could you post your code? This has been bothering me for 2 days! Haha!

3
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();
    }
}
 

4
General / Re: Trouble setting up SFML with Code::Blocks
« on: October 31, 2013, 11:59:09 am »
I used SFML 2.1 and Code::Blocks 12.11

Did you download it with the MinGW compiler? If so you need the 'GCC 4.7 TDM (SJLJ)' version of SFML. If not, you need the 'GCC 4.7 MinGW (DW2)' version. This is the problem I had.

5
General / Trouble setting up SFML with Code::Blocks
« on: October 30, 2013, 09:21:22 am »
Have you tried searching on the forums? I've seen many posts like this and I've even posted one myself with a similar problem.

Which version of Code::Blocks are you using? And which SFML did you download?

6
General / Re: Collision Problem with Sprite
« on: October 29, 2013, 09:23:02 pm »
Quote
Well obviously the "player" will be under the "floor" if you draw the "floor" on top of the "player".

This isn't the problem, I didn't phrase it very well, I don't know how I can explain it. I may have to post some screen shots to show you.

EDIT: I have fixed it, I needed to set the origin for the sprite.

7
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;
}
 

8
General / Re: Player Movement with Classes.
« on: October 26, 2013, 08:50:27 pm »
Thanks, it was the way it was worded that confused me.

9
General / Re: Player Movement with Classes.
« on: October 26, 2013, 07:10:02 pm »
Thanks for all of that! I will take it all in and improve it over time. I've recently been playing around with the way I name things which was actually a hard task. I wanted a way that would show what things are without it being hard to read which is why I chose '_Member' for member variables. Thanks for letting me know this is bad practice, I did try 'mVar' but I didnt like how it looked, shame.

When I put 'TEMP__AnimTime' that was unintentional, I didnt mean to have two underscores, I cleaned up my code last night and made a mistake with that! But thanks for pointing it out, I have changed it.

When you mentioned creating a 'Render' class, what exactly would it be doing, would it just be returning information?

Also what did you mean by the constructor not initializing the player?

10
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;
}
 

11
General / Re: Accessing Variables in Functions in Classes!
« on: September 30, 2013, 08:41:05 pm »
Thanks! Something so simple, brains not switched on at the moment haha!

12
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
 

13
General / Moving Spites Help!
« on: September 29, 2013, 09:56:17 am »
Okay thanks :) I was guessing it wasn't but I'm glad I asked haha, I'll get there eventually. I have an idea in my head now of how it works.

14
General / Moving Spites Help!
« on: September 28, 2013, 11:27:21 pm »
Thanks for helping, people on these forums are great, great community!

I'll have to brush up on classes as it's been a while since I've programmed anything, I'll post an update with my progress in the future, I'd rather do everything clean and functional rather than sloppy and functional, I hate having messy code which is why I thought something could be improved!

I'll also get to work on adding a background, I have no clue where this is going to end up, it's more of an experiment at the moment!

Oh, and is "myMove();" built into SFML?

Thanks.

15
General / Re: Moving Spites Help!
« on: September 28, 2013, 10:32:34 pm »
Very sorry for posting in the wrong section, I've actually just realised, is there a way to change it? I'm new to these forums.

Thanks for replying, I was going to ask about setting a background but I'll post it in a new post in the correct section!

Would I create a function that checks for keyboard input inside a class?

Also, the reason I used keyboard input to resize was just so I could get out of the program easily because I wanted it fullscreen but thanks for noticing!  :)

Pages: [1] 2
anything