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

Pages: [1]
2
this might be more of a C++ question rather than SFML

why does the following code work
Collider playerCollider = player.GetCollider();

platform1.GetCollider().CheckCollision(playerCollider, 0.0f);
 

but this doesn't
platform1.GetCollider().CheckCollision(player.GetCollider(), 0.0f);
 

for reference this is player.h
class Player
{
public:
        Player(sf::Texture* texture, sf::Vector2u imageCount, float swtichTime, float speed);
        void Update(float deltaTime);
        void Draw(sf::RenderWindow& window);

        sf::Vector2f GetPosition() { return body.getPosition(); }
        Collider GetCollider() { return Collider(body); }
private:
        sf::RectangleShape body;
        Animations animation;
        unsigned int row;
        float speed;
        bool faceRight;
        float idleTime;
};

 

platform.h
class Platform
{
public:

        Platform(sf::Texture* texture, sf::Vector2f size, sf::Vector2f Postion);
        ~Platform();

        void Draw(sf::RenderWindow& window);
        Collider GetCollider() { return Collider(body); }
private :
        sf::RectangleShape body;
};

 

Collider.h
class Collider
{
public:
        Collider(sf::RectangleShape& body);
        ~Collider();

        void Move(float dx, float dy) { body.move(dx, dy);  }

        bool CheckCollision(Collider & other, float push);
        sf::Vector2f GetPosition() { return body.getPosition();}
        sf::Vector2f GetHalfSize() { return body.getSize() / 2.0f; }

       
private:
        sf::RectangleShape& body;
       
};

 

3
Hi I fixed it,

I realized I'm just resetting the total time with deltatime in my code instead of adding delta time to total time.
it works as expected now thanks anyways

4
Hi

I need help with animations,
the Issue is the animations are way to jittery and inconsistant I feel iam using the deltaTime wrong,
as it is almost always lower than my switchTime (time to switch the animation) and if I lower the switchTime the animations becomes too fast.
Is it just because iam on a fast machine and there is no way around it ?

main.cpp
#include <iostream>
#include <SFML\Graphics.hpp>
#include "Animations.h"

int main()
{
    sf::RenderWindow window(sf::VideoMode(512, 512), "SFML", sf::Style::Close | sf::Style::Resize);
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setPosition(206.0f, 206.0f);
        sf::Texture playerTexture;
        playerTexture.loadFromFile("bat-sprite.png");
        player.setTexture(&playerTexture);
       
        Animations animation(&playerTexture, sf::Vector2u(4, 4), 0.005f);

        float deltaTime = 0.0f;
        sf::Clock clock;       
       
        while (window.isOpen())
        {
                deltaTime = clock.restart().asSeconds();

                sf::Event evnt;
                while (window.pollEvent(evnt))
                {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed :
                                window.close();
                                break;
                        }
                }

                animation.update(1, deltaTime);
                player.setTextureRect(animation.uvRect);
                window.clear(sf::Color(150,150,150));
                window.draw(player);
                window.display();
        }

    return 0;
}

Animation.h
#include <SFML\Graphics.hpp>

class Animations
{
public:
        Animations(sf::Texture* texture, sf::Vector2u imageCount, float swtichTime);
        void update(int row, float deltaTime);
public:
        sf::IntRect uvRect;
private:
        sf::Vector2u imagecount;
        sf::Vector2u currentImage;

        float totalTime;
        float switchTime;

       
};

 

Animations.cpp
#include "Animations.h"
#include <iostream>

Animations::Animations(sf::Texture* texture, sf::Vector2u imageCount, float swtichTime)
{
        this->imagecount = imageCount;
        this->switchTime = swtichTime;
        totalTime = 0.0f;
        currentImage.x = 0;

        uvRect.width = texture->getSize().x / float(imageCount.x);
        uvRect.height = texture->getSize().y / float(imageCount.y);
}

void Animations::update(int row, float deltaTime)
{
        currentImage.y = row;
        totalTime = deltaTime;


        //std::cout << "Total Time : " << totalTime << " SwitchTime: " << switchTime << " result :" << (totalTime >= switchTime) << std::endl;
        if (totalTime >= switchTime)
        {
                //std::cout << currentImage.x << std::endl;
                totalTime -= switchTime;
                currentImage.x++;

                if (currentImage.x >= imagecount.x) {
                        currentImage.x = 0;
                }
        }
       
       
        uvRect.left = currentImage.x * uvRect.width;
        uvRect.top = currentImage.y * uvRect.height;

}
 

Any help is appreciated

Pages: [1]
anything