Bounding box for C++ particle system not working - c++

I have created a particle system in c++ using Qt which includes gravity. I would like to include a bounding box so that the system can also include collisions however I cannot seem to get it to work. Here is my .h code:
typedef struct {
int top;
int bottom;
int left;
int right;
}BoundingBox;
BoundingBox makeBoundingBox(int top, int bottom, int left, int right);
.cpp:
BoundingBox makeBoundingBox(int top, int bottom, int left, int right)
{
BoundingBox boundingBox;
boundingBox.top = top;
boundingBox.bottom = bottom;
boundingBox.left = left;
boundingBox.right = right;
return boundingBox;
}
I have then updated the bounding box in my emitter class using this loop:
for(int i=0; i<m_numParticles; ++i)
{
if (m_pos.m_y >= _boundingBox.top)
{
m_dir.m_y = (-1)*(m_dir.m_y);
}
if (m_pos.m_y <= _boundingBox.bottom)
{
m_dir.m_y = (-1)*(m_dir.m_y);
}
if (m_pos.m_x <= _boundingBox.left)
{
m_dir.m_x = (-1)*(m_dir.m_x);
}
if (m_pos.m_x >= _boundingBox.right)
{
m_dir.m_x = (-1)*(m_dir.m_x);
}
m_particles[i].update(m_gravity, _boundingBox);
And have set the bounding box in my window as so:
m_emitter->setBoundingBox(makeBoundingBox(m_height, 0, 0, m_width));
I am not getting any errors however it doesn't seem to be working,
Any advice would be appreciated
Thanks

Assuming that decreasing X means going towards left and decreasing Y means going towards top, your conditions seem incorrect. I would also update the position alongside the velocity to make sure it doesn't get triggered multiple times in a row.
Example for the Y coordinate:
// Are we going up too much?
if (m_pos.m_y < _boundingBox.top)
{
m_dir.m_y = (-1)*(m_dir.m_y);
m_pos.m_y = _boundingBox.top + 1;
}
// Or are we going down too much?
else if (m_pos.m_y > _boundingBox.bottom)
{
m_dir.m_y = (-1)*(m_dir.m_y);
m_pos.m_y = _boundingBox.bottom - 1;
}

Shouldn't:
m_emitter->setBoundingBox(makeBoundingBox(m_height, 0, 0, m_width));
Be:
m_emitter->setBoundingBox(makeBoundingBox(0, m_height, 0, m_width));

Related

How do I get a destructor on an object in a vector not to throw a failed assertion? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm programming a Breakout game in C++. I'm having a HUGE problem that's preventing me from giving the game multi-ball functionality. I think it has something to do with the destructor. Have a look:
for loop for the balls (Driver.cpp):
for (Ball& b : balls) { // Loops over all balls
(...)
// Collision for when you miss
if (b.getYPos() > HEIGHT) { // If the ball falls below the defined height of the screen
balls.erase(balls.begin() + b.getID()); // Wipe the ball out of memory to make room (Troublesome line)
Ball::addToID(-1); // Shift the ball ID to assign to the next ball back one
(...)
}
And I get this error:
Debug Error!
Program: Breakout.exe
abort() has been called
(Press Retry to debug the application)
Do you know why this mysterious crash is happening? Or more importantly, a fix for it?
Here's a replicable piece of code to help:
Driver.cpp:
#include <vector>
#include <allegro5\allegro.h>
#include "Ball.h"
using namespace std;
vector<Ball> balls(0); // Pay attention to this line
const POS WIDTH = 1600, HEIGHT = 900;
int main {
while (running) {
if (al_key_down(&key, ALLEGRO_KEY_SPACE)) { // Spawn the ball
balls.push_back(Ball(WIDTH / 2, 500, 10, 10)); // Spawn the ball
balls[Ball::getIDtoAssign()].setYSpeed(5);
}
for (Ball& b : balls) { // Pay attention to this loop
b.draw(); // This line is what's crashing.
b.move();
(...)
// Collision for when you miss
balls.erase(
remove_if(balls.begin(), balls.end(),
[=](Ball& b) {
// Collision for when you miss
return b.getYPos() > HEIGHT; // If the ball falls below the defined height of the screen, wipe the ball out of memory to make room
}
),
balls.end()
);
}
}
}
}
return 0;
}
Ball.h:
#pragma once
#include <allegro5\allegro_primitives.h>
using namespace std;
class Ball {
public:
Ball();
Ball(float x, float y, float w, float h);
~Ball();
void draw();
void move();
float getYPos();
void setYSpeed(float set);
private:
float xPos; // Horizontal position
float yPos; // Vertical position (upside down)
float width; // Sprite width
float height; // Sprite height
float xSpeed; // Horizontal speed
float ySpeed; // Vertical speed (inverted)
}
Ball.cpp:
#include "Ball.h"
short Ball::ballIDtoAssign = 0;
Ball::Ball() {
this->xPos = 0;
this->yPos = 0;
this->width = 0;
this->height = 0;
this->xSpeed = 0;
this->ySpeed = 0;
}
Ball::Ball(float x, float y, float w, float h) {
this->xPos = x;
this->yPos = y;
this->width = w;
this->height = h;
this->xSpeed = 0;
this->ySpeed = 0;
}
Ball::~Ball() {
// Destructor
}
void Ball::draw() {
al_draw_filled_rectangle(xPos, yPos, xPos + width, yPos + height, al_map_rgb(0xFF, 0xFF, 0xFF));
}
void Ball::move() {
xPos += xSpeed;
yPos += ySpeed;
}
float Ball::getYPos() {
return yPos;
}
void Ball::setYSpeed(float set) {
ySpeed = set;
}
You cannot modify a container while you are iterating through it with a range-for loop. You don't have access to the iterator that the loop uses internally, and erase() will invalidate that iterator.
You can use the container's iterators manually, paying attention to the new iterator that erase() returns, eg:
for(auto iter = balls.begin(); iter != balls.end(); ) { // Loops over all balls
Ball& b = *iter:
...
// Collision for when you miss
if (b.getYPos() > HEIGHT) { // If the ball falls below the defined height of the screen
...
iter = balls.erase(iter); // Wipe the ball out of memory to make room
}
else {
++iter;
}
}
Alternatively, use the erase-remove idiom via std::remove_if() instead:
balls.erase(
std::remove_if(balls.begin(), balls.end(),
[=](Ball &b){
// Collision for when you miss
return b.getYPos() > HEIGHT; // If the ball falls below the defined height of the screen, wipe the ball out of memory to make room
}
),
balls.end()
);
UPDATE: now that you have posted more of your code, it is clear to see that you are trying to use ID numbers as indexes into the vector, but you are not implementing those IDs correctly, and they are completely unnecessary and should be eliminated.
The Ball::ballID member is never being assigned any value, so in this statement:
balls.erase(balls.begin() + b.getID()); // The troublesome line
Trying to erase() the result of balls.begin() + b.getID() causes undefined behavior since the iterator has an indeterminate value, thus you can end up trying to erase the wrong Ball object, or even an invalid Ball object (which is likely the root cause of your runtime crash).
Also, in this section of code:
balls.push_back(Ball(WIDTH / 2, 500, 10, 10)); // Spawn the ball
balls[Ball::getIDtoAssign()].setYSpeed(5);
Ball::addToID(1);
Since you want to access the Ball object you just pushed, that code can be simplified to this:
balls.back().setYSpeed(5);
And I already gave you code further above to show you how to remove balls from the vector without using IDs.
So, there is need for an ID system at all.
With that said, try something more like this:
Driver.cpp:
#include <vector>
...
#include "Ball.h"
using namespace std;
vector<Ball> balls;
const POS WIDTH = 1600, HEIGHT = 900;
int main {
...
while (running) {
...
if (input.type == ALLEGRO_EVENT_TIMER) { // Runs at 60FPS
...
if (al_key_down(&key, ALLEGRO_KEY_SPACE)) { // Spawn the ball
balls.push_back(Ball(WIDTH / 2, 500, 10, 10)); // Spawn the ball
balls.back().setYSpeed(5);
}
for (auto iter = balls.begin(); iter != balls.end(); ) {
Ball &b = *iter;
...
if (b.getYPos() > HEIGHT) { // Collision for when you miss
iter = balls.erase(iter);
}
else {
++iter;
}
}
/* alternatively:
for (Ball& b : balls) {
b.draw();
b.move();
}
balls.erase(
std::remove_if(balls.begin(), balls.end(),
[=](Ball &b){
// Collision for when you miss
return b.getYPos() > HEIGHT; // If the ball falls below the defined height of the screen, wipe the ball out of memory to make room
}
),
balls.end()
);
*/
}
}
return 0;
}
Ball.h:
#pragma once
...
class Ball {
public:
...
// NO ID METHODS!
private:
...
// NO ID MEMBERS!
}
Ball.cpp:
#include "Ball.h"
...
// NO ID MEMBER/METHODS!
OK, so I managed to figure out why the program crashes. It was because I had the erase-remove inside the for loop which can cause all sorts of problems.

How can I make my cellular automata quicker

I have a grid of 300x300 cells. I am checking each cell individually which means I am checking 90000 cells atleast every second. Now, I know, one second is pretty fast considering the cpu is going through 300^2 different cells. But this stuff just isn't fast enough. I watched a guy on youtube who got his simulation to update every 20 ms. And he was running 512^2 cells. How is this possible? Optimizations? Is my computer a potato? I'll show my code if anyone wants to see exactly what i'm doing.
for (int y = GY; y >= 0; y--)
for (int x = 0; x < GX; x++) {
cell* cell_pointer = &grid[x][y];
if (grid[x][y].CellT == type::SAND) {
if (grid[x][y+1].CellT == type::AIR) {
if (y+1 > GY-1) {
continue;
}
cell* cell_below = &grid[x][y+1];
cell_below->Color ={ 255,0,0 };
cell_below->CellT = type::SAND;
cell_pointer->Color ={ 0,0,0 };
cell_pointer->CellT = type::AIR;
}
else if (grid[x+1][y+1].CellT == type::AIR) {
cell* cell_below = &grid[x+1][y+1];
cell_below->Color ={ 255,0,0 };
cell_below->CellT = type::SAND;
cell_pointer->Color ={ 0,0,0 };
cell_pointer->CellT = type::AIR;
}
else if (grid[x-1][y+1].CellT == type::AIR) {
cell* cell_below = &grid[x-1][y+1];
cell_below->Color ={ 255,0,0 };
cell_below->CellT = type::SAND;
cell_pointer->Color ={ 0,0,0 };
cell_pointer->CellT = type::AIR;
}
}
}
The code above is my update function. I am updating the cells bottom to top, left to right. It's a sand simulation which is why we're checking the bottom left, bottom right as well as the bottom neighboring cells.
//Square//
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderClear(renderer);
//////////
int X = floor(mousePos.x/r.w);
int Y = floor(mousePos.y/r.h);
if (left) {
grid[X][Y].CellT = type::SAND;
grid[X][Y].Color ={ 255,0,0 };
}
if (right) {
grid[X][Y].CellT = type::WALL;
grid[X][Y].Color ={ 0,255,0 };
}
for (int y = 0; y < GY; y++)
for (int x = 0; x < GX; x++) {
cell* cell_pointer = &grid[x][y];
SDL_SetRenderDrawColor(renderer, cell_pointer->Color.r, cell_pointer->Color.g, cell_pointer->Color.b, 255);
r.x = cell_pointer->PosG.x;
r.y = cell_pointer->PosG.y;
SDL_RenderFillRect(renderer, &r);
}
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
The code above is my draw function. I am using SDL as my graphics API. I am getting the mouse position relative to the grids so that when I click left or right mouse buttons I can draw a sand cell or a wall cell. So I am checking each cell individually top to bottom and left to right and then drawing a square individually where the cell's position is.

Need Processing assistance. If statement

Can someone help me with my code. I am trying to get the square to appear on the left side of the window again once it moves off the right side. I almost have it, but once it goes off the right side, it suddenly appears on the left side. I need help making it appear smoothly on the left side of the screen.
Here is my code.
float x = 0;
void setup() {
size(200,200);
}
void draw() {
background(255);
drawRect();
if(x > width+5) {
x = 0;
}
}
void drawRect() {
fill(0);
for (int i = 0; i<width; i+=15){
rect(x +i*2,100,20,20);
}
x = x + 1;
}
I don't really get what the loop in drawRect() does, but it seems like you want to reset x such that the shape it draws is just off the left side of the screen, so something like this:
if (x > width+5) {
x = -50;
}

Smoothing Land Around Lakes

Sorry if that title isn't very descriptive. Anyway, I am working on something dealing with randomly generating landscapes. I made lakes, but due to how they are make, they often cause straight edges / dropoffs, which aren't desirable. I am trying to smooth it (right after making the lake, if possible), by defining a max variation amount (so land heights cannot vary more than it), and have it fix land if it varies to much, and quit if it is fine.
The problem:
My attempted fix:
As you can see... it didn't work. It also occurs to me, I think it would be broken if it had to move down, although that case shouldn't actually occur, because lakes only ever sink the landscape. Anyway, here is the source of my attempt:
//smoothing land nearby
int maxVariation = 2; //similar to the max height variation when the land is generated
//going right
for (int xPos = rightBound + 1, previousHeight = 0; ; ++xPos)
{
if (previousHeight == 0)
for (; previousHeight < size.y; ++previousHeight)
if (grid[0][rightBound][previousHeight] != BlockColor::DIRT && grid[0][rightBound][previousHeight] != BlockColor::GRASS)
{
--previousHeight;
break;
}
for (int y = 0; y < size.y; ++y)
if (grid[0][xPos][y] == BlockColor::WATER)
goto done_smoothing_right;
int height;
for (height = 0; height < size.y; ++height)
if (grid[0][xPos][height] != BlockColor::DIRT && grid[0][xPos][height] != BlockColor::GRASS)
{
--height;
break;
}
int difference = std::abs(height - previousHeight);
previousHeight = height;
if (difference > maxVariation)
{
for (int j = 0; j < size.y; ++j)
{
int toMove = difference;
while (j + toMove >= size.y)
--toMove;
grid[0][xPos][j] = grid[0][xPos][j + toMove];
}
}
else
goto done_smoothing_right;
}
done_smoothing_right:
int tomakegotowork;
Note that is only the right side, but left should be about the same. How can I do this correctly?
Thanks if you can help.
EDIT:
I never did solve this problem. Instead, I made a recursive function to measure air, (from a certain height), and if a pocket of air (formed by the land) had enough, to fill with water. This has the advantaged of the land looking smooth because it is not altered.
This is written in java so you will need to convert it to c++ but it should give you the basic idea. It will only work for smoothing upwards as well and I only did the right side of the lake but it is very easy to modify it for the left side of the lake. I tried to match what I think the functionality of your code is.
Hope it helps...
void smoothLakeRight(Lake lake){
int x = lake.rightBound+1;
if(getGrassHeight(x)-lake.height>WorldConstants.MAX_LAKESIDE_VARIATION){
//if the right bank is too high start smoothing
int y =lake.height+WorldConstants.MAX_LAKESIDE_VARIATION;
while(grid[0][x][y] == BlockColor.DIRT){
fixGrass(x++, y++);
}
}
}
private int getGrassHeight(int xPos){
int y = WorldConstants.LOWEST_GRASS;
while(grid[0][xPos][y++] != BlockColor.GRASS);
return y-1;
}
private void fixGrass(int xPos, int yPos){
grid[0][xPos][yPos] = BlockColor.GRASS;
aboveAir(xPos,yPos);
belowDirt(xPos, yPos);
}
private void aboveAir(int xPos, int yPos) {
while(grid[0][xPos][++yPos]!=BlockColor.AIR){
if(grid[0][xPos][yPos]==BlockColor.TREE){
upRootTree(xPos, yPos);
}else{
grid[0][xPos][yPos]=BlockColor.AIR;
}
}
}
private void upRootTree(int xPos, int yPos) {
while(grid[0][xPos][yPos]==BlockColor.TREE){//remove stump
grid[0][xPos][yPos++]=BlockColor.AIR;
}
//remove leaves
grid[0][xPos][yPos] = BlockColor.AIR;
grid[0][xPos+1][yPos] = BlockColor.AIR;
grid[0][xPos-1][yPos] = BlockColor.AIR;
grid[0][xPos+1][yPos-1] = BlockColor.AIR;
grid[0][xPos-1][yPos-1] = BlockColor.AIR;
}
private void belowDirt(int xPos, int yPos) {
while(grid[0][xPos][--yPos]!=BlockColor.DIRT){
grid[0][xPos][yPos] = BlockColor.DIRT;
}
}

2D Box Collisions - What am I doing wrong? (C++)

I'm trying to make a platformer game in C++ and I have made a vector of blocks,
and I simply loop through the vector and check for the collision individually:
//Pseudo code
class Block{
...int x
...int y
...int width
...int height
};
class Player{
int x
int y
int width
int height
int hsp //horizontal speed
int vsp //vertical speed
int facing //0 = no direction, -1 = left, 1 = right
...
void loop()
{
if(keyboard_pressed(key_left) { x-=hsp; facing = -1;}
if(keyboard_pressed(key_right) {x+=hsp; facing = 1;}
if(keyboard_pressed(key_up) {y-=vsp;}
if(keyboard_pressed(key_down) {y+=vsp;}
if(keyboard_released(key_left | key_right) {facing = 0;}
for(int i = 0; i < blocks.size(); i++)
{
Block b = blocks.at(i);
check_Collision(b);
}
}
};
As you can see, my player simply moves according to hsp and vsp. Simple enough.
The main portion of my question is in check_Collision(). First I check to see if the player
is on top of the block, and if he is, let him stay there.
Then I check if the player is at the sides of the block.
But for some reason there's a problem. For some reason when I go under the top of the block,
he stays at the top, but then he gets shifted to the left side.
I honestly don't know where to go with this.
The following code only checks for the top and the left side:
check_Collision(){
///////////////////////////////////
var myLeft, myRight, myTop, myBot;
var bLeft, bRight, bTop, bBot;
myLeft = x;
myRight = x + width;
myTop = y;
myBot = y + height;
/////////////////////
bLeft = b.x;
bRight = b.x + b.width;
bTop = b.y;
bBot = b.y + b.height;
//////////////////////////////////
//Check if we are at the top
if(myBot + vsp > bTop+1){
y = bTop - height;
}
//Check if we are at the sides
if(myBot > bTop+2){
if(myRight + hsp > bLeft)
{
x = bLeft - width;
}
}
}
If anyone can point me into some tutorial on 2D box collision that would be great.
The logic you're using doesn't make sense to me. It's not enough just to check that the player is under the block: don't you also need to make sure that the player is standing on it, ie, isn't too far to the right or left of the block? Similarly, in your second check, you've got to make sure that the player isn't jumping over the block (or standing on it). Your if partially checks this, but doesn't take into account the fact that the first check might have modified player position.
Can you assume that the player can't ever walk under the block? Can you assume that the player will never move fast enough to "tunnel" completely through the block (hsp > b.width, etc)?
If the answer to either of these is no, you will need significantly more sophisticated collision detection.