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.


Topics - coltranenaima

Pages: [1]
1
General / Vector of Vectors Collision Detection
« on: May 28, 2018, 04:28:06 am »
Hello,

I am having issues with collision detection of player bullets against enemies.

I have three "types" of enemies and plan on having many more with distinct characteristics.

enemyOne(sf::Color::Yellow, 20.f, 20.f)
enemyTwo(sf::Color::Red, 40.f, 40.f)
enemyThree(sf::Color::Cyan, 80.f, 80.f)

that feed into

std::vector<Enemy> enemiesOne;
std::vector<Enemy> enemiesTwo;
std::vector<Enemy> enemiesThree;

I currently have a not so elegant function that won't scale very well with my goal...however it does work.

void Bullet::BulletEnemyCollision(sf::RectangleShape playerBulletShape, std::vector<Enemy>& enemyVectorsOne, std::vector<Enemy>& enemyVectorsTwo, std::vector<Enemy>& enemyVectorsThree)
{
        for (unsigned int i = 0; i < enemyVectorsOne.size(); i++)
        {
                if (playerBulletShape.getGlobalBounds().intersects(enemyVectorsOne[i].enemyShape.getGlobalBounds()))
                {
                        enemyVectorsOne.erase(enemyVectorsOne.begin() + i);
                }
        }
        for (unsigned int i = 0; i < enemyVectorsTwo.size(); i++)
        {
                if (playerBulletShape.getGlobalBounds().intersects(enemyVectorsTwo[i].enemyShape.getGlobalBounds()))
                {
                        enemyVectorsTwo.erase(enemyVectorsTwo.begin() + i);
                }
        }
        for (unsigned int i = 0; i < enemyVectorsThree.size(); i++)
        {
                if (playerBulletShape.getGlobalBounds().intersects(enemyVectorsThree[i].enemyShape.getGlobalBounds()))
                {
                        enemyVectorsThree.erase(enemyVectorsThree.begin() + i);
                }
        }
}


An idea I have is to feed each enemy vector into a vector and simply have the function iterate through that vector of vectors. This currently does not work.

std::vector<std::vector<Enemy> >& enemyVectors


void Bullet::BulletEnemyCollision(sf::RectangleShape playerBulletShape, std::vector<std::vector<Enemy> >& enemyVectors)
{
        for (unsigned int i = 0; i < enemyVectors.size(); i++)
        {
                for (unsigned int j = 0; j < enemyVectors[i].size(); j++)
                {
                        if (playerBulletShape.getGlobalBounds().intersects(enemyVectors[i][j].enemyShape.getGlobalBounds()))
                        {
                                enemyVectors[i].erase(enemyVectors[i].begin() + j);
                        }
                }
        }
}

Below is a brief recording of the game with the not so elegant code in place.

https://gfycat.com/gifs/detail/LivelySpottedChevrotain

You may notice the lack of pointers and keywords like auto...etc. This game for me is an exercise in learning classes & functions at a basic level. The next game I plan on taking a stab at utilizing pointers and super useful keywords. The last game I made I was using header files for implementation...baby steps.

Thanks for reading and I appreciate the help.


2
General / Fire Bullet At Angle
« on: April 22, 2018, 03:31:14 am »
Hello,

I am working on a game that so far has:
  • A square moving around with a basic velocity system (wasd)
  • The Square also rotates based on mouse position

I am now trying to implement bullets that will fire in the direction the square (player) is rotated, I made a very simple system with the last game I made (thanks again Hapax) that only went in one direction.

The bullets fire (yay), just not at the right angle (boo). I debugged and looked at the rotation each time and the numbers matched....it just doesn't come out correctly visually.

Thanks!

This is main.cpp (I chopped a major chunk of code to isolate what the issue may be)

#include "Player.h"
#include "Bullet.h"
#include <iostream>

int main()
{
        //declare
        float delta, deltaCd, bulletTime, bulletCd, bulletSpeed, bulletAngle;
        float& dCd = deltaCd;
        sf::Clock gameClock, bulletClock;
        Player playerOne;
        Player& pO = playerOne;
        Bullet playerBullet;
        Bullet& pB = playerBullet;
        std::vector<sf::RectangleShape> bulletVector;
        std::vector<float> bulletAngleVector;

        //initialize
        delta = .0f;
        deltaCd = .25f;
        bulletTime = 1.0f;
        bulletCd = 2.0f;
        bulletSpeed = 1.5f;
        bulletAngle = .0f;

        playerBullet.bulletShape.setPosition(sf::Vector2f(playerOne.playerShape.getPosition().x,
       
        //window loop
        //TODO: Create game loop
        while (win.isOpen())
        {
                window.clear();
                delta = gameClock.restart().asSeconds();
                playerOne.PlayerRotation(win, pO.playerShape);
                playerBullet.BulletAngle(win, pB.bulletShape);

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && bulletTime > bulletCd)
                {
                        bulletTime = bulletClock.restart().asSeconds();
                        playerBullet.bulletShape.setPosition(sf::Vector2f(playerOne.playerShape.getPosition().x, playerOne.playerShape.getPosition().y));
                        bulletAngle = playerBullet.bulletShape.getRotation();
                        bulletVector.push_back(playerBullet.bulletShape);
                        bulletAngleVector.push_back(bulletAngle);
                }              

                for (unsigned int i = 0; i < bulletVector.size(); i++)
                {
                        bulletVector[i].move(cos(bulletAngleVector[i]), sin(bulletAngleVector[i]));
                        window.draw(bulletVector[i]);
                }
               
                bulletTime = bulletClock.getElapsedTime().asSeconds();

                window.draw(pO.playerShape);
                window.display();
        }
}


bullet.cpp

#include "Bullet.h"

Bullet::Bullet()
{
        bulletSizeX = 5.f;
        bulletSizeY = 5.f;
        bulletShape.setSize(sf::Vector2f(bulletSizeX, bulletSizeY));
        bulletShape.setOrigin(sf::Vector2f(bulletShape.getGlobalBounds().width / 2.f, bulletShape.getGlobalBounds().height / 2.f));
        bulletShape.setFillColor(sf::Color::Red);
}

void Bullet::BulletAngle(sf::RenderWindow& win, sf::RectangleShape& bS)
{
        dx = 0.f;
        dy = 0.f;
        mouse = sf::Mouse::getPosition(win);
        mouseWorld = win.mapPixelToCoords(mouse);
        dx = mouseWorld.x - bS.getPosition().x;
        dy = mouseWorld.y - bS.getPosition().y;
        rotation = atan2(dy, dx) * 180 / PI;
        bS.setRotation(rotation);
}

bullet.h

#pragma once
#include <SFML/Graphics.hpp>

class Bullet
{
public:
        float bulletSizeX, bulletSizeY, dx, dy, rotation;
        const float PI = 3.14159265f;
        sf::Vector2i mouse;
        sf::Vector2f mouseWorld;
        sf::RectangleShape bulletShape;
        void BulletAngle(sf::RenderWindow& win, sf::RectangleShape& bS);
        Bullet();
};

3
General / Multiple bullets with isKeyPressed
« on: February 13, 2017, 12:41:56 am »
The bolded section is the area of concern. I am having a hard time wrapping my head around making a great bullet system that is easy to work with and manipulate when needed.

bullet_sprites is a vector of sprites, a new bullet sprite is pushed back into the vector when spacebar is pressed. I originally didn't have "&& bullet_sprites.size() == 0" in the first if statement, but it would create a stream of bullets and cause performance issues (see attached pic).

The bullet is erased when it is off screen and the issue I am having now is for one space bar keypress, two bullets are fired. When i say two, I mean one is fired and moved off screen and then a consecutive bullet is fired and moves off screen with only one keypress.

Thanks for any help, i can provide more code as well (like the header files being used). I watched youtube tutorials and even made a stripped down basic project that is having the same exact issue.

   while (main_window.isOpen())
   {      
      float delta = delta_clock.restart().asSeconds();
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
      {
         fired_bullet = true;
      }
      //clear...draw...
      main_window.clear();
      move_the_background(main_background, main_background2, delta, main_window);
      start_level.level_one(asteroid_sprites, main_window, delta, space_ship);
      start_level.level_two(asteroid_sprites, main_window, delta, space_ship);
      main_window.draw(space_ship);

      //keypress
      key_check.w_key(space_ship.getPosition(), space_ship);
      key_check.s_key(space_ship.getPosition(), space_ship);
      key_check.a_key(space_ship.getPosition(), space_ship);
      key_check.d_key(space_ship.getPosition(), space_ship);
      key_check.esc_key(main_window);

      if (fired_bullet == true && bullet_sprites.size() == 0)
      {
         sf::Sprite new_bullet = create_bullet.create_sprite("bullet.png", true);
         new_bullet.setPosition(space_ship.getPosition().x + 145.f, space_ship.getPosition().y + 0.f);
         bullet_sprites.push_back(new_bullet);
         fired_bullet = false;
      }

      for (int j = 0; j < bullet_sprites.size(); j++)
      {
         main_window.draw(bullet_sprites[j]);
         create_bullet.move_sprite(1500.f, 0.f, delta, bullet_sprites[j], false, 0.f, false, 0.f, 0.f, "pos");
         if (bullet_sprites[j].getPosition().x > 1940)
         {
            bullet_sprites.erase (bullet_sprites.end() - 1);
         }   
      }


      //display...
      main_window.display();
      
      //check to see if main window is closed
      close_event_checker(main_window);
   }

Pages: [1]
anything