I'm making a 2D game in c++ and am trying to shoot enemies. When a bullet collides with the first enemy rendered then the enemy is killed fine, and it is removed from the enemys vector. However after the first enemy has been killed, other enemies no longer die.
This is where the check is carried out in the update function.
size = enemys.size();
for (int i = 0; i<size; i++)
{
double x = enemys[i].getEnemyX();
double y = enemys[i].getEnemyY();
bool isShot = enemyShot(x,y);
if(isShot == true){
enemys.erase(enemys.begin()+i);
size = size - 1;
}
}
This is the enemyShot function.
bool GameActivity::enemyShot(double enemyX, double enemyY)
{
int size = bullets.size();
for (int i = 0; i<size; i++)
{
double x = bullets[i].getX();
double y = bullets[i].getY();
if (x >= enemyX-5.0 && x <= enemyX+5.0 && y >= enemyY-5.0 && y <= enemyY + 5.0){
return true;
}
else{
return false;
}
}
}
The problem is that your vector of enemies is updated after each erasure - thus, the current index is no longer correct.
A better way to iterate the enemys vector is to start from the end of the enemys vector. That way, when you erase an element, the index is still correct:
for (size_t i = enemys.end (); i> 0; --i) {
double x = enemys[i].getEnemyX();
double y = enemys[i].getEnemyY();
bool isShot = enemyShot(x,y);
if(isShot == true){
enemys.erase(enemys.begin()+i);
}
}
Related
I'm building Space Invaders in C++ (using the MBed platform) for a microcontroller. I've used a 2D Vector of object pointers to organise the invaders.
The movement algorithm is below, and runs in the main while loop for the game. Basically, I get the highest/lowest x and y values of invaders in the vector, and use those to set bounds based on screensize (the HEIGHT variable);
I also get the first invader's position, velocity, and width, which I apply changes to based on the bounds above.
Then I iterate through the whole vector again and apply all those changes. It sort of works – the invaders move – but the bounds don't seem to take effect, and so they fly off screen. I feel like I'm missing something really dumb, thanks in advance!
void Army::move_army() {
int maxy = HEIGHT - 20;
int Ymost = 0; // BOTTOM
int Yleast = 100; // TOP
int Xmost = 0; // LEFT
int Xleast = 100; // RIGHT
int first_row = _rows;
int first_column = _columns;
int firstWidth = 0;
Vector2D firstPos;
Vector2D firstVel;
for (int i = 0; i < _rows; i++) {
for (int n = 0; n < _columns; n++) {
bool state = invaders[i][n]->get_death();
if (!state) {
if (i < first_row && n < first_column) {
firstPos = invaders[i][n]->get_pos();
firstVel = invaders[i][n]->get_velocity();
firstWidth = invaders[i][n]->get_width();
}
Vector2D pos = invaders[i][n]->get_pos();
if (pos.y > Ymost) {Ymost = pos.y;} // BOTTOM
else if (pos.y < Yleast) {Yleast = pos.y;} // TOP
else if (pos.x > Xmost) {Xmost = pos.x;} // LEFT
else if (pos.x < Xleast) {Xleast = pos.x;} // RIGHT
}
}
}
firstVel.y = 0;
if (Xmost >= (WIDTH - 8) || Xleast <= 2) {
firstVel.x = -firstVel.x;
firstPos.y += _inc;
// reverse x velocity
// increment y position
}
else if (Ymost > maxy) {
_inc = -_inc;
// reverse increment
}
else if (Yleast < 2) {
_inc = -_inc;
// reverse increment
}
for (int i = 0; i < _rows; i++) {
int setx = firstPos.x;
if (i > 0) {firstPos.y += 9;}
for (int n = 0; n < _columns; n++) {
invaders[i][n]->set_velocity(firstVel);
invaders[i][n]->set_pos(setx,firstPos.y);
setx += firstWidth + 2;
}
}
It looks like you have your assignment cases reversed. Assignment always goes: right <- left, so in the first case you're changing the YMost value, not pos.y. It looks like if you swap those four assignments in your bounds checking it should work. Good luck!
I have written a compare-function that should compare two possible move options for a player in a game that works like Chess. Each Move contains a Figure that should do the move and a Point, where it will move. The Points are already checked, so all of them are valid moves. When I try to sort a list containing all of the moves currently available using strict weak ordering and the std::sort function, I get a SIGSEG after my compare function got confrontet with some garbage Figure Pointer in one Move.
I already tried to figure out where the garbage pointer came from, but ony found, that the std::sort function somehow put it in the mix of all the other moves. The same seg fault occurs when i try to sort with std::stable_sort. I also thougt about stack issues due to the fact, that I had some before, but this isn't the case either.
bool cmpWhite(White_Move m1, White_Move m2) {
if (m1.f == nullptr) {
return true;
} else if (m2.f == nullptr) {
return true;
}
int sum1 = 0;
double avg1 = 0;
Figure *f1 = m1.f;
Point origin1 = f1->getCoordinatesAsPoint();
int sum2 = 0;
double avg2 = 0;
Figure *f2 = m2.f;
Point origin2 = f2->getCoordinatesAsPoint();
Point p;
movePiece(field_pub, f1, m1.p);
std::vector<Point> moves = black_king->getAllNewPossiblePositions();
for (int i = 0; i < moves.size(); i++) {
p = moves[i];
if (!black_king->isPositionBlocked(field_pub, p.x, p.y)) {
sum1++;
// avg1 += sqrt((p.x - target_pub.x) * (p.x - target_pub.x) + (p.y - target_pub.y) * (p.y - target_pub.y));
}
}
p = black_king->getCoordinatesAsPoint();
if (!black_king->isPositionBlocked(field_pub, p.x, p.y)) {
sum1++;
}
// avg1 = (double)sum1;
movePiece(field_pub, f1, origin1);
movePiece(field_pub, f2, m2.p);
moves = black_king->getAllNewPossiblePositions();
for (int i = 0; i < moves.size(); i++) {
p = moves[i];
if (!black_king->isPositionBlocked(field_pub, p.x, p.y)) {
sum2++;
// avg2 += sqrt((p.x - target_pub.x) * (p.x - target_pub.x) + (p.y - target_pub.y) * (p.y - target_pub.y));
}
}
p = black_king->getCoordinatesAsPoint();
if (!black_king->isPositionBlocked(field_pub, p.x, p.y)) {
sum2++;
}
// avg2 = (double)sum2;
movePiece(field_pub, f2, origin2);
std::cout << "cmp: " << sum1 << " " << sum2 << std::endl;
return sum1 < sum2;
}
std::vector<White_Move> sortBestMovesForWhite(Figure **figures, int size, King *bKing, int **field, Point target) {
target_pub = target;
field_pub = new int *[FIELD_WIDTH];
for (int x = 0; x < FIELD_WIDTH; x++) {
field_pub[x] = new int[FIELD_HEIGHT];
for (int y = 0; y < FIELD_HEIGHT; y++) {
field_pub[x][y] = field[x][y];
}
}
black_king = bKing;
std::vector<White_Move> moves;
for (int i = 0; i < size; i++) {
Figure *f = figures[i];
std::vector<Point> m_point = f->getAllNewPossiblePositions();
for (int j = 0; j < m_point.size(); j++) {
if (!f->isPositionBlocked(field, m_point.at(j).x, m_point.at(j).y)) {
White_Move move = {f, m_point.at(j)};
moves.push_back(move);
}
}
}
// std::stable_sort(moves.begin(), moves.end(), cmpWhite);
std::sort(moves.begin(), moves.end(), cmpWhite);
for (int x = 0; x < FIELD_WIDTH; x++) {
delete[] field_pub[x];
}
delete[] field_pub;
return moves;
}
One of your return true at the start of your compare function should be return false. The comparator for std::sort must meet the conditions specified here: https://en.cppreference.com/w/cpp/named_req/Compare. If this is not the case std::sort will have undefined behaviour.
The correct implementation should look something like:
bool cmpWhite(White_Move m1, White_Move m2) {
if (m1.f == nullptr) {
return m2.f != nullptr;
}
if (m2.f == nullptr) {
return false;
}
...
}
So I'm relatively new to coding in SFML, so apologies if I made some newbie mistake. My project is Space Invaders in SFML. When I was working on the shooting, a problem arose. How do I do collision? I was familiar with shape.getGlobalBounds().intersect(), and it worked on previous projects. It didn't work. So I tried simplifying it. I used RectangleShape for both the enemy shape, and the bullet shape.
Here is the actual for loop, for the collision:
for (int y = 0; y <= 2; ++y) {
for (int x = 0; x <= 6; ++x) {
if (shootShape.getPosition().x < e.aliensX[x] && shootShape.getPosition().x > e.aliensX[x] + 15 ||
shootShape.getPosition().y < e.aliensY[y] && shootShape.getPosition().y > e.aliensY[y] + 15) {
e.aliensX[x] = -10;
e.aliensY[y] = -10;
shooting = false;
reload = false;
}
}
}
This is the shooting function:
void Player::shoot() {
if (reload) {
shootX = posX + 5;
shootY = posY - 50;
shootShape.setPosition(shootX, shootY);
shooting = true;
reload = false;
}
if (shooting) {
shootY -= 150 * 2 * deltaTime;
shootShape.setPosition(shootX, shootY);
}
}
And this is how I draw the enemies (I don't know how to create multiple enemies):
void Enemy::drawAliens() {
for (int j = 0; j <= arraySizeY; ++j) {
for (int i = 0; i <= arraySizeX; ++i) {
actualShape.setPosition(aliensX[i], aliensY[j]);
if (aliensY[i] <= p.shootY && aliensY[i] >= p.shootY) {
aliensX[i] = -10;
aliensY[i] = -10;
}
else {
win.draw(actualShape);
}
}
}
}
Explanation behind some of the variables:
aliensX is an array which contains the different x positions for the enemies.
aliensY is an array which contains the different y positions for the enemies.
shooting is a bool variable, which is true when the bullet is travelling.
reload is whether you can shoot.
EDIT:
Intersect will not work because of the way I created my enemies. They are all in ONE shape. I need it to work with specific coordinates, because I don't know a better way to create multiple enemies all at once. If there is a way to do that, advice would be appreciated!
It's rather easy, if you rely on SFML's templated sf::Rect class. Just retrieve the rectangle for both objects as global boundaries and try to intersect them:
const bool collides = firstDrawable.getGlobalBounds().intersect(secondDrawable.getGlobalBounds());`
Same can be done with a small temporary rectangle you can fill with dynamic values not directly associated with a sf::Drawable:
const sf::FloatRect bullet(x - width / 2, y - height / 2, width, height);
const bool collides = firstDrawable.getGlobalBounds().intersect(bullet);`
Your if statement is never true. You have
shootShape.getPosition().x < e.aliensX[x] && shootShape.getPosition().x > e.aliensX[x] + 15.
Your shootShape.getPosition().x can never be smaller than e.aliensX[x] and bigger than e.aliensX[x] + 15 at the same time. That's impossible. The same for y position. Change your if statement to
for (int y = 0; y <= 2; ++y) {
for (int x = 0; x <= 6; ++x) {
if (shootShape.getPosition().x > e.aliensX[x] && shootShape.getPosition().x < e.aliensX[x] + 15 ||
shootShape.getPosition().y > e.aliensY[y] && shootShape.getPosition().y < e.aliensY[y] + 15) {
e.aliensX[x] = -10;
e.aliensY[y] = -10;
shooting = false;
reload = false;
}
}
}
I'm trying to implement a space invader game, now I'm trying to check if bullets hit an invader, then I remove it.
I'm ONLY get an exception when I try to shoot the last invader in the row.
For e.g If I have just one row and 8 invaders, the last one, when the bullet hits it, I got a strange boundary out of exception.
The initialization is like that, in pseudo code:
struct Invaders_t
{
vector<Invader*> *invaders;
}
for (int j = 0; j < 1; j++)
{
x_spacing = 15;
for (int i = 0; i < 8; i++)
{
Invader *invader = new Invader();
g_Invaders.invaders->push_back(invader);
}
for each frame:
for (int x = 0; x < 1; x++)
{
for (int y = 0; y < 8; y++)
{
Invader *pInvader = (*(g_Invaders.invaders))[x*8 + y];
if (pInvader != nullptr && pInvader->AlienAtals->GetImage() != nullptr)
{
for (list<Bullet*>::iterator it = ship->Bullets->begin(); it != ship->Bullets->end(); it++)
{
float left = (*it)->Position.x - 5;
float top = (*it)->Position.y - 13;
if (CheckCollision(left, top, pInvader->AlienSprite->m_X, pInvader->AlienSprite->GetImage()->GetWidth()/4
, pInvader->AlienSprite->m_Y, pInvader->AlienSprite->GetImage()->GetHeight()/4))
{
std::vector<Invader*>::iterator iter = g_Invaders.invaders->begin();
std::advance(iter, (x *8) + y);
Invader *invader = *iter;
g_Invaders.invaders->erase(iter);
delete invader;
}
}
}
}
}
When you delete an invader (g_Invaders.invaders->erase(iter);), the size of the vector decreases by 1 (all vector's elements after iter shift by 1 position to the beginning, filling the vacant space). Your code doesn't expect this and continues iterating over the vector as if still had the full set of elements.
I am trying to speed up a piece of code that is ran a total of 150,000,000 times.
I have analysed it using "Very Sleepy", which has indicated that the code is spending the most time in these 3 areas, shown in the image:
The code is as follows:
double nonLocalAtPixel(int ymax, int xmax, int y, int x , vector<nodeStructure> &nodeMST, int squareDimension, Mat &inputImage) {
vector<double> nodeWeights(8,0);
vector<double> nodeIntensities(8,0);
bool allZeroWeights = true;
int numberEitherside = (squareDimension - 1) / 2;
int index = 0;
for (int j = y - numberEitherside; j < y + numberEitherside + 1; j++) {
for (int i = x - numberEitherside; i < x + numberEitherside + 1; i++) {
// out of range or the centre pixel
if (j<0 || i<0 || j>ymax || i>xmax || (j == y && i == x)) {
index++;
continue;
}
else {
int centreNodeIndex = y*(xmax+1) + x;
int thisNodeIndex = j*(xmax+1) + i;
// add to intensity list
Scalar pixelIntensityScalar = inputImage.at<uchar>(j, i);
nodeIntensities[index] = ((double)*pixelIntensityScalar.val);
// find weight from p to q
float weight = findWeight(nodeMST, thisNodeIndex, centreNodeIndex);
if (weight!=0 && allZeroWeights) {
allZeroWeights = false;
}
nodeWeights[index] = (weight);
index++;
}
}
}
// find min b
int minb = -1;
int bCost = -1;
if (allZeroWeights) {
return 0;
}
else {
// iteratate all b values
for (int i = 0; i < nodeWeights.size(); i++) {
if (nodeWeights[i]==0) {
continue;
}
double thisbCost = nonLocalWithb(nodeIntensities[i], nodeIntensities, nodeWeights);
if (bCost<0 || thisbCost<bCost) {
bCost = thisbCost;
minb = nodeIntensities[i];
}
}
}
return minb;
}
Firstly, I assume the spent time indicated by Very Sleepy means that the majority of time is spent allocating the vector and deleting the vector?
Secondly, are there any suggestions to speed this code up?
Thanks
use std::array
reuse the vectors by passing it as an argument of the function or a global variable if possible (not aware of the structure of the code so I need more infos)
allocate one 16 vector size instead of two vectors of size 8. Will make your memory less fragmented
use parallelism if findWeight is thread safe (you need to provide more details on that too)