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

Pages: [1]
1
Usually, in my games, I have a list of these ifstatements:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    // Stuff
}
(Or something similar)

Now yes, while this does work, it doesn't exactly look very neat, and sometimes, when I map over 3 keys to a single input, the line wraps and looks very messy.
A good fix would be to create an overload of the sf::Keyboard::isKeyPressed() function allowing for the input of a vector (or some other form of array) of keys. For example:
 std::vector<sf::Keyboard> Keys = {sf::Keyboard::A, sf::Keyboard::D, sf::Keyboard::W, sf::Keyboard::S};
if (sf::Keyboard::isKeyPressed(Keys)){
    //stuff
}

And this would have the same effect of this:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::D, sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))){
    // Stuff
}

Yes, this feature doesn't greatly impact the actual framework much, but overall it just makes code much cleaner and easier to read.

2
Worked. Thanks a lot

3
 For context, I have 3 classes: `AsteroidClass`, `RocketClass`, and `Spaceship`. Spaceship is the player controlled spaceship, whilst `AsteroidClass` are the asteroids, and `RocketClass` is the class for all projectiles shot by the player.
Shooting is checked inside the `Spaceship` classes' `Update()` function. When it detects space key pressed, it creates a new `RocketClass` instance and pushes it to the private vector `GlobalRockets`. At the end of the update function, this vector is looped through and individually calls the `Update` function on each `RocketClass` instance, and then drawing it to the window.
However, I never see any projectile rendered. I have tried manually creating a new `RocketClass` instance inside of the `main`
function, calling it's `Update`, then rendering it manually and no problems there. The only issue is rendering it inside of the Player's `Update`. Here is my code:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <cmath>
#include <vector>

enum Turn {
        Static,
        Glide
};

enum AsteroidType {
        Small,
        Medium,
        Large
};

const int WINDOW_LENGTH = 800;
const int WINDOW_HEIGHT = 600;

class RocketClass {
        private:
                const double MOVE_SPD=3.5;
                double ROTATION_RAD;
                const double OUTLINE_THICKNESS=1.5;
                const float SIZE=3;
        public:
                sf::CircleShape Rocket;
                RocketClass(sf::Vector2f StartPos, double Rotation){
                        Rocket.setRadius(SIZE);
                        Rocket.setFillColor(sf::Color(0, 0, 0, 0));
                        Rocket.setPosition(StartPos);
                        Rocket.setRotation(Rotation);
                        Rocket.setOutlineThickness(OUTLINE_THICKNESS);
                        ROTATION_RAD = Rotation*180/3.14159265;
                }
                void Update(double TimeFrame){
                        Rocket.move(MOVE_SPD*TimeFrame*cos(ROTATION_RAD), MOVE_SPD*TimeFrame*sin(ROTATION_RAD));
                        if (Rocket.getPosition().y > WINDOW_HEIGHT){
                                Rocket.setPosition(sf::Vector2f(Rocket.getPosition().x, 0));
                        }
                        if (Rocket.getPosition().y < 0){
                                Rocket.setPosition(sf::Vector2f(Rocket.getPosition().x, WINDOW_HEIGHT));
                        }
                        if (Rocket.getPosition().x > WINDOW_LENGTH){
                                Rocket.setPosition(sf::Vector2f(0, Rocket.getPosition().y));
                        }
                        if (Rocket.getPosition().x <0){
                                Rocket.setPosition(sf::Vector2f(WINDOW_LENGTH, Rocket.getPosition().y));
                        }
                }
};

class AsteroidClass {
        private:
                double ROTATE_SPD =0.8;
                double VELOCITY =70;
                double DIRECTION=0;
                double SIZE_MULTIP=0.25;
                const double OUTLINE_THICKNESS = 1;
                AsteroidType Type = AsteroidType::Large;
        public:
                sf::ConvexShape Asteroid;
                AsteroidClass(AsteroidType ConstType){
                        srand(time(NULL));
                        Type = ConstType;
                        switch(Type){
                                case AsteroidType::Small:
                                        SIZE_MULTIP =3.75;
                                        break;
                                case AsteroidType::Medium:
                                        SIZE_MULTIP= 6.25;
                                        break;
                                case AsteroidType::Large:
                                        SIZE_MULTIP=8.75;
                                        break;
                                }
                        switch(rand()%2+1){
                                case 1:
                                        Asteroid.setPointCount(11);
                                        Asteroid.setPoint(0, sf::Vector2f(-2*SIZE_MULTIP, -5*SIZE_MULTIP));
                                        Asteroid.setPoint(1, sf::Vector2f(0*SIZE_MULTIP, -5*SIZE_MULTIP));
                                        Asteroid.setPoint(2, sf::Vector2f(0*SIZE_MULTIP, -4*SIZE_MULTIP));
                                        Asteroid.setPoint(3, sf::Vector2f(2*SIZE_MULTIP, -4*SIZE_MULTIP));     
                                        Asteroid.setPoint(4, sf::Vector2f(4*SIZE_MULTIP, -2*SIZE_MULTIP));
                                        Asteroid.setPoint(5, sf::Vector2f(4*SIZE_MULTIP, 2*SIZE_MULTIP));
                                        Asteroid.setPoint(6, sf::Vector2f(2*SIZE_MULTIP, 4*SIZE_MULTIP));
                                        Asteroid.setPoint(7, sf::Vector2f(1*SIZE_MULTIP, 4*SIZE_MULTIP));
                                        Asteroid.setPoint(8, sf::Vector2f(0*SIZE_MULTIP, 3*SIZE_MULTIP));
                                        Asteroid.setPoint(9, sf::Vector2f(-3*SIZE_MULTIP, 3*SIZE_MULTIP));
                                        Asteroid.setPoint(10, sf::Vector2f(-2*SIZE_MULTIP, 2*SIZE_MULTIP));
                                        Asteroid.setPoint(11, sf::Vector2f(-2*SIZE_MULTIP, 1*SIZE_MULTIP));
                                        Asteroid.setPoint(12, sf::Vector2f(-4*SIZE_MULTIP, 0*SIZE_MULTIP));
                                        Asteroid.setPoint(13, sf::Vector2f(-4*SIZE_MULTIP, -4*SIZE_MULTIP));
                                        Asteroid.setPoint(14, sf::Vector2f(-3*SIZE_MULTIP, -4*SIZE_MULTIP));
                                        Asteroid.setPoint(15, sf::Vector2f(-2*SIZE_MULTIP, -5*SIZE_MULTIP));
                                        Asteroid.setOutlineThickness(OUTLINE_THICKNESS);
                                        Asteroid.setPosition(sf::Vector2f(100, 100));
                                        Asteroid.setFillColor(sf::Color(0, 0, 0, 0));
                                        break;
                               
                                case 2:
                                        Asteroid.setPointCount(17);
                                        Asteroid.setPoint(0, sf::Vector2f(-3*SIZE_MULTIP, -4*SIZE_MULTIP));
                                        Asteroid.setPoint(1, sf::Vector2f(2*SIZE_MULTIP, -4*SIZE_MULTIP));
                                        Asteroid.setPoint(2, sf::Vector2f(2*SIZE_MULTIP, -3*SIZE_MULTIP));
                                        Asteroid.setPoint(3, sf::Vector2f(4*SIZE_MULTIP, -1*SIZE_MULTIP));
                                        Asteroid.setPoint(4, sf::Vector2f(4*SIZE_MULTIP, 2*SIZE_MULTIP));
                                        Asteroid.setPoint(5, sf::Vector2f(3*SIZE_MULTIP, 3*SIZE_MULTIP));
                                        Asteroid.setPoint(6, sf::Vector2f(2*SIZE_MULTIP, 3*SIZE_MULTIP));
                                        Asteroid.setPoint(7, sf::Vector2f(1*SIZE_MULTIP, 4*SIZE_MULTIP));
                                        Asteroid.setPoint(8, sf::Vector2f(-2*SIZE_MULTIP, 4*SIZE_MULTIP));
                                        Asteroid.setPoint(9, sf::Vector2f(-2*SIZE_MULTIP, 3*SIZE_MULTIP));
                                        Asteroid.setPoint(10, sf::Vector2f(-3*SIZE_MULTIP, 3*SIZE_MULTIP));
                                        Asteroid.setPoint(11, sf::Vector2f(-4*SIZE_MULTIP, 2*SIZE_MULTIP));
                                        Asteroid.setPoint(12, sf::Vector2f(-3*SIZE_MULTIP, 1*SIZE_MULTIP));
                                        Asteroid.setPoint(13, sf::Vector2f(-3*SIZE_MULTIP, -1*SIZE_MULTIP));
                                        Asteroid.setPoint(14, sf::Vector2f(-4*SIZE_MULTIP, -2*SIZE_MULTIP));
                                        Asteroid.setPoint(15, sf::Vector2f(-4*SIZE_MULTIP, -3*SIZE_MULTIP));
                                        Asteroid.setPoint(16, sf::Vector2f(-3*SIZE_MULTIP, -4*SIZE_MULTIP));
                                        Asteroid.setOutlineThickness(OUTLINE_THICKNESS);
                                        Asteroid.setPosition(sf::Vector2f(100, 100));
                                        Asteroid.setFillColor(sf::Color(0, 0, 0, 0));
                        }
                }
                void Update(double FrameTime){
                        Asteroid.rotate(ROTATE_SPD);
                        const int Degrees_Rotation = DIRECTION*180/3.14159265;
                        Asteroid.move(VELOCITY*FrameTime*cos(Degrees_Rotation), VELOCITY*FrameTime*sin(Degrees_Rotation));
                        if (Asteroid.getPosition().y > WINDOW_HEIGHT){
                                Asteroid.setPosition(sf::Vector2f(Asteroid.getPosition().x, 0));
                        }
                        if (Asteroid.getPosition().y < 0){
                                Asteroid.setPosition(sf::Vector2f(Asteroid.getPosition().x, WINDOW_HEIGHT));
                        }
                        if (Asteroid.getPosition().x > WINDOW_LENGTH){
                                Asteroid.setPosition(sf::Vector2f(0, Asteroid.getPosition().y));
                        }
                        if (Asteroid.getPosition().x <0){
                                Asteroid.setPosition(sf::Vector2f(WINDOW_LENGTH, Asteroid.getPosition().y));
                        }
                }
};


class SpaceShip {
        private:
                std::vector<RocketClass> GlobalRockets = {};
                const Turn TURN_TYPE = Turn::Static;
                const int OUTLINE_THICKNESS = 1;
                const int SIZE_MULTIP=4;
                const double ACCELERATION_SPD=0.02;
                const double DECELERATION_SPD=0.018;
                const double MAX_SPD = 4;
                double TURN_SPD=2.5;
                const double GLIDE_TURN_SPD = 0.07;
                const double MAX_TURN_SPD = 7.5;
                double Velocity =0;
                double TurnVelocity =0;
                double Degrees_Rad = 0;
                const double FRAME_TIME_MULTIP=75;
        public:
                sf::ConvexShape Ship{(5)};
                SpaceShip(){
                        Ship.setPoint(0, sf::Vector2f(4*SIZE_MULTIP, 0));
                        Ship.setPoint(1, sf::Vector2f(-4*SIZE_MULTIP, -3*SIZE_MULTIP));
                        Ship.setPoint(2, sf::Vector2f(-2*SIZE_MULTIP, 0));
                        Ship.setPoint(3, sf::Vector2f(-4*SIZE_MULTIP, 3*SIZE_MULTIP));
                        Ship.setPoint(4, sf::Vector2f(4*SIZE_MULTIP, 0));
                        Ship.setOutlineThickness(OUTLINE_THICKNESS);
                        Ship.setPosition(sf::Vector2f(100, 100));
                        Ship.setFillColor(sf::Color(0, 0, 0, 0));
                        if (TURN_TYPE == Turn::Glide){
                                TURN_SPD = GLIDE_TURN_SPD;
                        }
                }
                void Update(double FrameTime, sf::RenderWindow &Window){
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                                Velocity+=ACCELERATION_SPD;    
                                if (Velocity > MAX_SPD){
                                        Velocity = MAX_SPD;
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                                Velocity-=DECELERATION_SPD;
                                if (Velocity < MAX_SPD*-1){
                                        Velocity=MAX_SPD*-1;
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                        TurnVelocity+=TURN_SPD;
                                if (TurnVelocity >= MAX_TURN_SPD){
                                        TurnVelocity = MAX_TURN_SPD;
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                                TurnVelocity -= TURN_SPD;
                                if (TurnVelocity <= MAX_TURN_SPD*-1){
                                        TurnVelocity = MAX_TURN_SPD*-1;
                                }
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
                                RocketClass Projectile(Ship.getPosition(), Ship.getRotation());
                                GlobalRockets.push_back(Projectile);
                        }
                        switch (TURN_TYPE){
                                case Turn::Static:
                                        Ship.rotate(TurnVelocity*(FrameTime*FRAME_TIME_MULTIP));
                                        TurnVelocity = 0;
                                        break;
                                case Turn::Glide:
                                        Ship.rotate(TurnVelocity*(FrameTime*FRAME_TIME_MULTIP));
                                        break;
                        }
                        Degrees_Rad = Ship.getRotation() *3.14159/180;
                        Ship.move(sf::Vector2f(Velocity*FrameTime*FRAME_TIME_MULTIP*cos(Degrees_Rad), Velocity*FrameTime*FRAME_TIME_MULTIP*sin(Degrees_Rad)));

                        if (Ship.getPosition().y > WINDOW_HEIGHT){
                                Ship.setPosition(sf::Vector2f(Ship.getPosition().x, 0));
                        }
                        if (Ship.getPosition().y < 0){
                                Ship.setPosition(sf::Vector2f(Ship.getPosition().x, WINDOW_HEIGHT));
                        }
                        if (Ship.getPosition().x > WINDOW_LENGTH){
                                Ship.setPosition(sf::Vector2f(0, Ship.getPosition().y));
                        }
                        if (Ship.getPosition().x <0){
                                Ship.setPosition(sf::Vector2f(WINDOW_LENGTH, Ship.getPosition().y));
                        }
                        for (int i=0;i<GlobalRockets.size();i++){
                                GlobalRockets.at(i).Update(FrameTime);
                                Window.draw(GlobalRockets.at(i).Rocket);
                        }
                }
};

int main(){
        const double MAX_FPS = 60;
        SpaceShip Player;
        AsteroidClass AsteroidInstance(AsteroidType::Large);
        sf::RenderWindow Window(sf::VideoMode(WINDOW_LENGTH, WINDOW_HEIGHT), "Asteroids");
        sf::Clock FrameTime;

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

                if (FrameTime.getElapsedTime().asSeconds() >= 1/MAX_FPS){
                        Player.Update(FrameTime.getElapsedTime().asSeconds(), Window);
                        AsteroidInstance.Update(FrameTime.getElapsedTime().asSeconds());
                        Window.clear();
                        Window.draw(Player.Ship);
                        Window.draw(AsteroidInstance.Asteroid);
                        Window.display();
                        FrameTime.restart();
                }
        }
}
 
I think the main focus is the contents of the `Spaceship` class, however the rest of the code is there for context.

4
General / Re: Bouncing ball on 2D pane
« on: August 10, 2024, 07:28:08 pm »
Figured I can achieve an effect like what I want by doing `Ball.setRotation(Ball.getRotation()*-1)`

5
General / Bouncing ball on 2D pane
« on: August 10, 2024, 05:04:55 pm »
I'm creating a simple pool game in SFML and C++. As of current, you can launch the cue ball, however it doesn't bounce, and simply stops whenever it hits the wall, which is intentional, as of current.
I want it to bounce similar to that of when you're actually playing pool. I've thought on how to do this, and I can't think of a way to implement it the way I want. I've looked at other sites, however most are trying to implement a falling ball, or have some other strange system they want implemented aswell.
So how would I do this?

6
Graphics / Cannot set pointer to an sf::RectangleShape
« on: July 18, 2024, 08:44:30 pm »
I'm making a physics engine, as of current I'm implementing gravity. I've implemented it fine, however I preferably want multiple shapes to play around with. So, I created a function
std::vector<RectangleData> CreateRectangle(int Amount, std::vector<sf::Vector2f> Pos);
, in which RectangleData is a union consisting of an sf::RectangleShape pointer, bounciness of the rectangle, it's current fall speed, and it's terminal velocity.
So then, in the function, I create the RectangleShape, set the pointer in the RectangleData union to be pointing to it, set all the other values, add it to the return array, however when drawing it, it segfaults. After testing this, it appears to be segfaulting as the pointer in the union is a null pointer, despite I set it to be pointing. Here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

union RectangleData {
        sf::RectangleShape *Rectangle=nullptr;
        double RectangleTerminalVelocity;
        double RectangleBouncyness;
        double RectangleFallSpd;
};

bool IsInside(sf::Vector2f Size, sf::Vector2i Pos1, sf::Vector2f Pos2);
std::vector<RectangleData> CreateRectangle(int Amount, std::vector<sf::Vector2f>Pos);
//void HandleRectanglePhysics(std::vector<*sf::RectangleShape> Rectangles, std::vector<double> TerminalVelocity, std::vector<double>Bouncyness);

int main() {
        std::vector<RectangleData> Rectangles = CreateRectangle(1, {sf::Vector2f(100, 0)});
        double WantedFps = 60;
        double AccelerationSpd = 1.5;
        double Rectangle1TerminalVelocity =4;
        double Rectangle1Bouncyness = 1.525;
        sf::RenderWindow Window(sf::VideoMode(800, 600), "Physics Engine");
        sf::RectangleShape Rectangle1(sf::Vector2f(50, 50));
        double Rectangle1FallSpd =0;
        sf::Clock Frame;
        while (Window.isOpen()){
                if (Frame.getElapsedTime() >= sf::seconds(1/WantedFps)){
                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && IsInside(Rectangle1.getSize(), sf::Mouse::getPosition(Window), Rectangle1.getPosition())){
                                Rectangle1.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(Window)));
                                Rectangle1.move(sf::Vector2f(Rectangle1.getSize().x/-2, Rectangle1.getSize().y/-2));
                                Rectangle1FallSpd = 0;
                        }
                        else {
                                Rectangle1FallSpd += AccelerationSpd/WantedFps;
                                Rectangle1.move(sf::Vector2f(0, Rectangle1FallSpd));
                                if (Rectangle1FallSpd >= Rectangle1TerminalVelocity){
                                        Rectangle1FallSpd = Rectangle1TerminalVelocity;
                                }
                        }
                        if (Rectangle1.getPosition().y >= 550){
                                Rectangle1.setPosition(sf::Vector2f(Rectangle1.getPosition().x, 550));
                                Rectangle1FallSpd -= Rectangle1FallSpd*Rectangle1Bouncyness;
                        }
                        sf::Event Event;
                        while (Window.pollEvent(Event)){
                                if (Event.type == sf::Event::Closed){
                                        Window.close();
                                }
                        }
                        Window.clear();
                        Window.draw(Rectangle1);
                        //Window.draw(*Rectangles.at(0).Rectangle);
                        Window.display();
                        Frame.restart();
                }
        }
}

bool IsInside(sf::Vector2f Size, sf::Vector2i Pos1i, sf::Vector2f Pos2){
        sf::Vector2f Pos1 = static_cast<sf::Vector2f>(Pos1i);
        if (Pos2.x + Size.x >= Pos1.x && Pos2.y + Size.y >= Pos1.y && Pos1.x >= Pos2.x && Pos1.y >= Pos2.y){
                return true;
        }
        return false;
}      

std::vector<RectangleData> CreateRectangle(int Amount, std::vector<sf::Vector2f> Pos){
        std::vector<RectangleData> RectangleDataVec;
        for (int i=0; i<Amount; i++){
                sf::RectangleShape RectangleInstance(sf::Vector2f(50, 50));
                RectangleInstance.setPosition(Pos.at(i));
                RectangleData RectInstance;
                RectInstance.Rectangle = &RectangleInstance;
                RectInstance.RectangleTerminalVelocity = 4;
                RectInstance.RectangleFallSpd = 0;
                RectInstance.RectangleBouncyness=1.525;
                RectangleDataVec.push_back(RectInstance);
                std::cout << RectInstance.Rectangle << "\n" << &RectangleInstance;
        }
        return RectangleDataVec;
}
 

7
Window / Re: One function to clear, draw, and display the window?
« on: January 02, 2024, 06:37:13 pm »
This worked great, thanks!

8
Window / One function to clear, draw, and display the window?
« on: January 01, 2024, 05:07:28 pm »
Hi, I'm currently developing a game using SFML, and to make my code look better I was wondering if I could somehow make one function to clear, draw on, and display the window without having to list each object I want to draw as a separate parameter.
Thanks!

9
It worked perfectly. Thank you a lot.

10
Hi, I'm trying to develop a rougelike game using SFML, and in said game I have a blaster.
I've read the documentation, but I can't figure out for the life of me how to move a sprite in it's pointed direction.
I would really appreciate it if you could tell me how to do this.

Thank you for your time.

Pages: [1]
anything