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

Pages: [1]
1
That worked! Thanks a lot, this really helped since I've been struggling with this for about 3 days. Thanks again!

2
That sounds great! I'm currently trying to implement this, but I'm having some trouble finding the right functions and what not. Could you possibly give me a more detailed example.

Though I'm sure it's completely wrong, here's the line of code I currently have for setting the initial position:
bulletStart = sf::Vector2f(player.sprite.getTransform().transformPoint(player.x, player.y));
 

3
General / Moving an X, Y Point Forward Relative to a certain Rotation
« on: July 17, 2020, 11:10:10 am »
Hello everybody,

I'm making a 2D Top-Down-Shooter game, and I currently have a player who can move around the screen, face the mouse, and almost properly shoot bullets. The problem I'm having is when setting the current bullet's position, it starts at the player's shoulder and not the muzzle of the gun. I know I should probably be adding a value to the initial position of the bullet which depends on the player's rotation and location, but I'm having trouble figuring this value out. If you didn't understand my description, here's a clip of the current game running: https://streamable.com/wzr3qd.

This is what my main.cpp file (where I'm handling shooting) looks like:

#include <SFML/Graphics.hpp>
#include <vector>
#include <random>
#include <utility>
#include <cmath>
#include <iostream>
#include "Player.h"
#include "Enemy.h"
#include "Bullet.h"

int screenX = 1200, screenY = 1200;

sf::RenderWindow window(sf::VideoMode(screenX, screenY), "");
sf::Event event;


int main() {
    Player player;
    Bullet b1;
    std::vector<Bullet> bullets;
    sf::Vector2f bulletStart, mousePosition, aimDir, aimDirNorm;
    while (window.isOpen()) {
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
            }
        // Bullet vector math
        bulletStart = sf::Vector2f(player.x, player.y);
        mousePosition = sf::Vector2f(sf::Mouse::getPosition(window));
        aimDir = sf::Vector2f(mousePosition - bulletStart);
        aimDirNorm = sf::Vector2f(aimDir.x / sqrt(pow(aimDir.x, 2) + pow(aimDir.y, 2)), aimDir.y / sqrt(pow(aimDir.x, 2) + pow(aimDir.y, 2)));

        // Shoot
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
            b1.shape.setPosition(bulletStart);
            b1.shape.setRotation(player.angle);
            b1.currVelocity = aimDirNorm * b1.maxSpeed;
            bullets.emplace_back(Bullet(b1));
        }
        for (auto &bullet : bullets) {
            bullet.shape.move(bullet.currVelocity);
        }
        // Draw
        window.clear();
        player.update(window);
        player.show(window);
        for (Bullet &bullet : bullets)
            window.draw(bullet.shape);
        window.display();
    }
    return 0;
}
 

Any help would be highly appreciated!

4
General / Changing the world origin in C++ SFML
« on: June 28, 2020, 10:47:14 am »
Hello Everybody,

I've recently gotten into SFML and as an exercise to get more comfortable (and have fun), I started translating some Coding Challenges done by Daniel Shiffman on his Youtube channel, The Coding Train. Upon attempting to translate a star field effect in SFML, I started searching for the right function in SFML that would change the world origin from the top-left of the screen to the center. The closest thing I found to this was the setOrigin function, but that only works for sprites and not the whole window/screen. If you didn't understand my description of this function, it would be the equivalent of the translate(x, y) function in Processing. Any help would be appreciated.

Pages: [1]
anything