3 Replies - 524 Views - Last Post: 08 May 2018 - 12:12 PM Rate Topic: -----

#1 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Need help in moving a bullet with Mouse Aim in SFML

Posted 07 May 2018 - 11:55 PM

There are plenty of problems - :helpsmilie: :helpsmilie: :helpsmilie:

1* The Ycoordinate of bullet is working fine , but I am having problem in Xcoordinate
I am moving the bullet with move(X,Y)func. , I am trying to somehow get the X component of bullet equal to X component of mousePosition but the mousePosition.X is with respect to pixels(depending on the resolution) and the i am moving bullet with move(X,Y) func. which uses its own type of parameters , if i set X = 0, Y = -10 (move(0,-10)) then the bullet goes to the center of the window's upper border (to the coordinate 669,0 if I use resolution 1336x768)
I am unable to figure out how I can relate the coordinates of Mouse (which is w.r.t resolution)
to the type of coordinates used in bullets ;

2* I have loaded the bullet texture into the sprite(in the constructor) , but when i shoot all I see is a space ship shooting white polished bricks(no texture loaded in sprite)

3* The mouse aim of the ship is not accurate .

Please try to answer the first one , that is the real problem , but if 2nd and 3rd are also answered , I will be happy :dontgetit:

here is the code -
main.cpp
#include<iostream>
#include<stdlib.h>
#include<vector>

using namespace std;

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/window.hpp>

#include <Player.h>

int main ()
{
    sf::RenderWindow window(sf::VideoMode(1336,768),"Magenta Riddim");

    sf::Clock clock;

    Player hero (window.getSize().x , window.getSize().y);

    while(window.isOpen())
    {
        sf::Time deltaTime = clock.restart();

        sf::Vector2i mousePos = sf::Mouse::getPosition(window);
        sf::Event event ;
         while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            hero.Update();
            hero.playerSprite.setRotation(hero.Rotation(mousePos) + 270);
            hero.Shooting();
        }

        hero.MoveAllBullets(deltaTime ,mousePos);

        hero.Update();

        hero.BulletsRemaining();

        hero.CheckAllBulletsLifetime();

        hero.DestroyBullets();

        ///DRAWING HERE

        window.clear();
        window.draw(hero.playerSprite);
        for(int i =0;i<hero.nBullets;i++)
        {
            window.draw(hero.DisplayBullet(i));
        }
        window.display();
    }

    return 0;

}




Player.hpp
#ifndef PLAYER_H
#define PLAYER_H

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/window.hpp>

//#include <vector>

#include <iostream>

#include <Bullet.h>

class Player
{
   private:
       sf::Vector2f playerPos ;

       sf::Texture playerTexture ;

       std::vector<Bullet>bullets ;

       float rotation;
   public:

    sf::Sprite playerSprite ;
    int nBullets ;                                              /*    contains number of bullets      */

    public:
        Player(float ,float);                           /*2 floats are for window coordinates,this function initializes all the values*/
        void Update ();                                 /*Update Function called every frame , updates the playerPos value ; handles shooting ;*/
        float Rotation (sf::Vector2i mousePos);        /*will handle rotation*/

        void Shooting ();
        void MoveAllBullets (sf::Time deltaTime , sf::Vector2i mousePos);

        sf::Sprite DisplayBullet (int BulletNumber);          /*Returns the specified bullet for display*/

        void CheckAllBulletsLifetime ();
        void DestroyBullets ();

        virtual ~Player();

        //Console Debugging
        inline void BulletsRemaining ()
        {
            std::cout<<"No. of bullets not destroyed - "<<bullets.size()<<std::endl;
        }

};

#endif // PLAYER_H




Player.cpp
#include "Player.h"


Player::Player(float windowX ,float windowY)
{
    Player::playerPos.x = windowX / 2 ;
    Player::playerPos.y = windowY -100 ;

    Player::playerTexture.loadFromFile("textures/ship.png");
    Player::playerSprite.setTexture(Player::playerTexture);

    Player::playerSprite.setPosition(Player::playerPos);
    Player::playerSprite.setScale(0.13f,0.13f);

    Player::playerSprite.setOrigin(309.5,441.5);

    //ctor
}


void Player::Update()
{
    Player::playerPos = Player::playerSprite.getPosition() ;
    Player::nBullets = Player::bullets.size();
}

float Player::Rotation(sf::Vector2i mousePos)
{
    sf::Vector2f xy ;
    xy.x = Player::playerSprite.getPosition().x - mousePos.x;
    xy.y = Player::playerSprite.getPosition().y - mousePos.y;

    const float pi = 22/7 ;

    float degrees = (atan2(xy.y,xy.x)) * 180/pi ;
    Player::rotation = degrees ;
    return degrees;
}

void Player::Shooting()
{
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        Player::bullets.push_back(Bullet (Player::rotation , Player::playerSprite.getPosition(), sf::Mouse::getPosition()));
    }
}

void Player::MoveAllBullets(sf::Time deltaTime , sf::Vector2i mousePos)
{
    for(int i =0;i<Player::bullets.size();i++)
    {
        Player::bullets[i].MoveBullet(deltaTime , mousePos);
    }
}

sf::Sprite Player::DisplayBullet(int BulletNumber)
{
    return Player::bullets[BulletNumber].bulletSprite;
}

void Player::CheckAllBulletsLifetime()
{
    for(int i =0;i<Player::bullets.size();i++)
    {
        Player::bullets[i].CheckBulletLifetime();
    }
}

void Player::DestroyBullets()
{
    for(int i =0;i<Player::bullets.size();i++)
    {
        if(Player::bullets[i].destroy == true)
        {
            Player::bullets.erase(Player::bullets.begin() + i );
        }
    }
}

Player::~Player()
{
    //dtor
}




Bullet.hpp
#ifndef BULLET_H
#define BULLET_H

#include <SFML/Graphics.hpp>
#include <SFML/window.hpp>
#include <SFML/System.hpp>

class Bullet
{
  private:

    sf::Texture bulletTexture ;
    sf::Vector2f bulletPosition ;
    float MouseXPosition ;                                  /**Contains value of mousePos.x , useful for sending the bullet in the required X direction**/

  public:

    bool destroy ;
    sf::Sprite bulletSprite ;

    public:
        Bullet(float , sf::Vector2f ,sf::Vector2i);                         /*vector2i is position of mouse*//* Initialization of all values , Rotation w.r.t the player direction */
        void CheckBulletLifetime ();                                                                      /* Controls the value of destroy variable                             */
        void MoveBullet (sf::Time deltaTime , sf::Vector2i mousePos);                                    /* Moves the bullet with speed w.r.t current System Time              */
        virtual ~Bullet();
};

#endif // BULLET_H




Bullet.cpp
#include "Bullet.h"

Bullet::Bullet(float rotation, sf::Vector2f player , sf::Vector2i mousePos)
{
    Bullet::destroy = false ;                                           /*when the bullet reaches the limit it sends message to the player class and then bullet is destroyed*/

    Bullet::bulletTexture.loadFromFile("textures/bullet.png");
    Bullet::bulletSprite.setTexture(Bullet::bulletTexture);

    Bullet::bulletSprite.setPosition(player);
    Bullet::bulletSprite.setScale(0.1,0.1);

    Bullet::bulletSprite.setOrigin(Bullet::bulletTexture.getSize().x/2 , Bullet::bulletTexture.getSize().y/2);

    /** Rotation of the bullet towards the mouse Pointer **/

    Bullet::bulletSprite.setRotation(rotation +270);

    Bullet::MouseXPosition = mousePos.x  ;

    //ctor
}

void Bullet::MoveBullet(sf::Time deltaTime,sf::Vector2i mousePos)
{
    float dt = deltaTime.asSeconds();
    Bullet::bulletSprite.move(-0.5, -1000 * dt) ;
}

void Bullet::CheckBulletLifetime()
{
    if(Bullet::bulletSprite.getPosition().y == 0 || Bullet::bulletSprite.getPosition().y > 768 || Bullet::bulletSprite.getPosition().x == 0 || Bullet::bulletSprite.getPosition().x > 1336)
        Bullet::destroy = true;

        else if (Bullet::bulletSprite.getPosition().y < 0 )
            Bullet::destroy = true;
}

Bullet::~Bullet()
{
    //dtor
}




If my english is bad(which it is) then i will learn it after learning one language (C++)

dhanyawad if anybody helps :chinese:

Is This A Good Question/Topic? 0
  • +

Replies To: Need help in moving a bullet with Mouse Aim in SFML

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6673
  • View blog
  • Posts: 22,773
  • Joined: 05-May 12

Re: Need help in moving a bullet with Mouse Aim in SFML

Posted 08 May 2018 - 07:32 AM

On a high level, the problem that you have is needing to translate "world space" into "screen space" and vice versa. Your mouse is in "screen space", while your bullet is in "world space". Do a few Google searches on how to tackle this problem for your specific game/rendering engine.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6673
  • View blog
  • Posts: 22,773
  • Joined: 05-May 12

Re: Need help in moving a bullet with Mouse Aim in SFML

Posted 08 May 2018 - 07:37 AM

Moving to the "Game Development" subforum since this is more of a game programming question rather than a C/C++ question...
Was This Post Helpful? 0
  • +
  • -

#4 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Re: Need help in moving a bullet with Mouse Aim in SFML

Posted 08 May 2018 - 12:12 PM

oh thanks

the bullet texture is also not loading :helpsmilie:

btw what happened to the code , it got a bit different
a<href # ???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1