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

Pages: [1]
1
General / Re: Sprite rotated hit box broken
« on: April 17, 2021, 11:14:35 am »
I am going to try to write the code. Thanks for the reply!

2
General / Sprite rotated hit box broken
« on: April 16, 2021, 12:09:04 pm »
I tried to make it so if the player intersects the sprite global bound it despawns. But if the sprite is rotated the hitbox is wierd and it hits in some wierd parts it isn't supposed to hit. Sorry for the bad english
bool checked = false;
                for (int i = 0; i < enemy1.vectorOfEnemies.size(); i++) {
                    for (int j = 0; j < bullet1.vectorOfBullets.size() && checked == false; j++) {
                        if (enemy1.vectorOfEnemies[i].getGlobalBounds().intersects(bullet1.vectorOfBullets[j].getGlobalBounds())) {

                            bullet1.vectorOfBullets.erase(bullet1.vectorOfBullets.begin() + j);
                            bullet1.vectorOfRotations.erase(bullet1.vectorOfRotations.begin() + j);
                            enemy1.vectorOfEnemies.erase(enemy1.vectorOfEnemies.begin() + i);
                            enemy1.vectorOfRotations.erase(enemy1.vectorOfRotations.begin() + i);

                           

                            j--;

                            killEnemy.playAudio();

                            bullet1.cateGloante++;
                            checked = true;

                        }
                    }
                }
Can I make it so the hitbox seems more normal if the sprite is rotated?

3
General / Understanding delta time
« on: April 16, 2021, 09:33:52 am »
I am trying to make a function that return the delta time value but I have no idea how to do that. Can someone explain how can I calculate the delta time every frame?
Here is what I did so far but I don't think its right.
 float deltaTime() {
    sf::Clock deltaClock;
        sf::Time dt = deltaClock.restart();
        std::cout << dt.asSeconds() << std::endl;
        return dt.asSeconds();
}

4
General / Make an object spin to face another one
« on: April 15, 2021, 07:50:46 pm »
I am trying to make the enemies face the player and I dont know what to do. I've tried this code but it didn't work:
 
void enemyRotatePlayer(sf::Sprite player) {
                float angle = atan2(player.getPosition().y - enemy.getPosition().y, player.getPosition().x - enemy.getPosition().x) * 180 / 3.141;
                enemy.setRotation(angle);
        }

5
General / Problem using delta time
« on: April 15, 2021, 12:13:09 pm »
I am trying to make an int function that returns delta time and it doesn't work. Any idea what can I do?
float deltaTime() {
    sf::Clock deltaClock;
        sf::Time dt = deltaClock.restart();
        std::cout << dt.asSeconds() << std::endl;
        return dt.asSeconds();
}

6
General / Change color when hovering over text
« on: April 14, 2021, 08:52:06 pm »
I tried to chance a text's color when hovering over it with the mouse but it gives me an error:
"error C4996: 'sf::Text::setColor': was declared deprecated"
This is the code:
void textColorRed() {
                if (sf::Mouse::getPosition().x > button.getGlobalBounds().left && sf::Mouse::getPosition().x < button.getGlobalBounds().left + button.getGlobalBounds().width)
                {
                        if (sf::Mouse::getPosition().y > button.getGlobalBounds().top && sf::Mouse::getPosition().y < button.getGlobalBounds().top + button.getGlobalBounds().height) {
                                button.setColor(sf::Color::Red);
                        }
                }
        }
Any idea what I did wrong?
PS: The two if's work because I tried to count in the console "1" and it works so I did something wrong with the color I think.

7
General / About game states
« on: April 13, 2021, 09:56:07 pm »
I am trying to make a small game(new to sfml) and I heard about theese game states, but I can not find any sites or  tutorials on what are them or how to use them. Can anyone link me to some good sites or briefly explain what are those?

8
General / Re: Problem with erasing elements from vector
« on: April 13, 2021, 07:22:00 pm »
It worked! Thanks so much, I've been strugleing with this for 3 days now:)

9
General / Problem with erasing elements from vector
« on: April 13, 2021, 10:48:04 am »
If I shoot a bullet and then another and the first bullet hits an enemy the program crashes.
If I shoot a bullet and hits an enemy but I dont shoot a 2nd one its fine.
If I shoot a bullet, it hits the enemy and then shoot a 2nd one its ok.
If I shoot any ammount of bullet and neither shooot the enemy its ok.
If I shoot a bullet that doesent it the enemy and another that hits while the first is active its ok.

The code for drawing the bullets:
void drawBullet(sf::RenderWindow& game, float playerRotation, float xPlayer, float yPlayer, sf::Vector2f vectorPosition) {
                float time2;
                time = clock.getElapsedTime();
                //std::cout << std::fixed << time.asSeconds() << std::endl;
                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && time.asSeconds() > 0.5 && cateGloante > 0) {
                                cateGloante--;
                               
                                bullet.setRotation(playerRotation);
                                bullet.setPosition(vectorPosition);
                                rota = playerRotation;
                                vectorOfRotations.push_back(rota);
                                clock.restart();

                                vectorOfBullets.push_back(bullet);
                        }
                        for (int i = 0; i < vectorOfBullets.size(); i++) {
                                game.draw(vectorOfBullets[i]);
                                vectorOfBullets[i].move(cos(vectorOfRotations[i] * 3.14189 / 180) * 3, sin(vectorOfRotations[i] * 3.14189 / 180) * 3);
                        }
        }
The code in the main.cpp
for (int i = 0; i < enemy1.vectorOfEnemies.size(); i++) {
            for (int j = 0; j < bullet1.vectorOfBullets.size(); j++) {
                if (enemy1.vectorOfEnemies[i].getGlobalBounds().intersects(bullet1.vectorOfBullets[j].getGlobalBounds())) {

                    bullet1.vectorOfBullets.erase(bullet1.vectorOfBullets.begin() + j);
                    bullet1.vectorOfRotations.erase(bullet1.vectorOfRotations.begin() + j);
                    enemy1.vectorOfEnemies.erase(enemy1.vectorOfEnemies.begin() + i);
                   
                    bullet1.cateGloante++;
                }
            }
        }
And the drawEnemies code
void drawEnemies(sf::RenderWindow& game) {

                srand(time(0));
                if (randomized == false) {
                        enemy.setPosition(1 + (rand() % 1920), 1 + (rand() % 1080));
                        randomized = true;
                }
                enemy.setOrigin(25, 62.5);
               
                if(oSinguraData == true) vectorOfEnemies.push_back(enemy);
                oSinguraData = false;

                for (int i = 0; i < vectorOfEnemies.size(); i++) {
                        game.draw(vectorOfEnemies[i]);
                }
        }
I do not know what doesent work while the 2 bullets are active...

10
General / I want to destroy an object from the screen and it crashes
« on: April 12, 2021, 08:12:22 pm »
I made a vector, vectorOfEnemies and vectorOfBullets witch I want to destroy the object when they intersect on the map(sry for the bad english).

for (int i = 0; i < enemy1.vectorOfEnemies.size(); i++) {
            for (int j = 0; j < bullet1.vectorOfBullets.size(); j++) {
                if (enemy1.vectorOfEnemies[i].getGlobalBounds().intersects(bullet1.vectorOfBullets[j].getGlobalBounds())) {
                    /*enemy1.vectorOfEnemies.erase(enemy1.vectorOfEnemies.begin() + i);
                    for (int k = i; k < enemy1.vectorOfEnemies.size() - 1; k++) {
                        enemy1.vectorOfEnemies[k] = enemy1.vectorOfEnemies[k + 1];
                    } enemy1.vectorOfEnemies.pop_back();
                    bullet1.vectorOfBullets.erase(bullet1.vectorOfBullets.begin() + i);
                    for (int l = i; l < bullet1.vectorOfBullets.size() - 1; l++) {
                        bullet1.vectorOfBullets[l] = bullet1.vectorOfBullets[l + 1];
                    } bullet1.vectorOfBullets.pop_back();
                    for (int p = i; p < bullet1.vectorOfRotations.size(); p++) {
                        bullet1.vectorOfRotations[p] = bullet1.vectorOfRotations[p + 1];
                    } bullet1.vectorOfRotations.pop_back();*/

                   
                   
                    bullet1.cateGloante++;
                    //std::cout << bullet1.cateGloante;
                    game.clear();
                    game.display();
                }
            }
        }


that is the code I tried to do to destroy the objects and when I run the program when the objects intersect the program crashes with the error "Expression: vecot subscript out of range"

11
General / Any way I can destroy objects?
« on: April 08, 2021, 09:03:07 pm »
I want to make it so if I shoot a bullet and hit something I destroy the bullet and the target. I tried to make it so the object wont draw, but if I don't draw it, it will still be there and kill the enemies so what should i do? I thought of a destructor function of the Bullets class but I have no idea how to use it._.
sorry for the bad english

12
General / Every time I get a random number I get the same one.
« on: April 08, 2021, 11:19:09 am »
I am trying to spawn enemies on a random point in the windwo and every timpe it sapwns the enemy in the same position.

This is the code I used for the random spawn:

enemy.setPosition(1 + (rand() % 1920), 1 + (rand() % 1080));


13
General / If an object is not drawn does it exist?
« on: April 07, 2021, 05:28:50 pm »
If an object is not drawn with the .draw() function is it still in the window?
I am making a kind of shooting game and I have a bullet object. I also have a thing that draws the object whenever you press E.
The question is does the object still "exist"? I mean if it somehow hits an enemy while not drawn will it kill the enemy or do I have to press E so i dray it?
Sry for the bad english.

14
I am new to SFML so I made a small code yesterday that worked perfectly. Today I changed a few things and it started showing this error:

1>------ Build started: Project: GAME, Configuration: Debug Win32 ------
1>LINK : fatal error LNK1104: cannot open file 'C:\Users\Adrian\Desktop\SFML environment\GAME\Debug\GAME.exe'
1>Done building project "GAME.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Going to the old code doesn't work either.

Pages: [1]
anything