creating combinations of obstacles in a 2D side scroller game - c++

My first game in ALLEGRO 5 with c++.
It has a player that keeps moving continuously in right direction. From the right edge of the screen player faces obstacles like triangles and squares. These obstacles come alive at right edge of screen and die at left edge of screen.
Suppose X is triangle and O is square.
i want to create them in few combinations like
..{x} {xo} {xoox} {oxxo} {oxx}... And some variations(random maybe)
And after that I will randomize the occurence of these pattern.
SO I found a method to implement what i wanted. I used a switch() to randomly select various cases of combination. It works pretty well. But once in a while it overlaps even after I introduced a minimum gap xoff
Here is the code:
`//I have used srand(time(NULL)); once in main function too.
if(state == PLAYING)
{
ball->Moveball();
ball->Jumpball();
//Camera-----------
ball->Cameraupdate();
al_identity_transform(&camera);
al_translate_transform(&camera,-Cameraposition[0],-Cameraposition[1]);
al_use_transform(&camera);
if(rand() % 200==0)
{
xoff+=200;// to introduce a minimum gap between them but this also fail once in a while.
int ch;
ch=rand()%4;
switch(ch)
{
case 0:
//T
triangle = new Triangle(850 + Cameraposition[0]+xoff,319);
objects.push_back(triangle);
triangle->SetAlive(true);
break;
case 1:
//TT
triangle = new Triangle(850 + Cameraposition[0]+xoff,319);
objects.push_back(triangle);
triangle->SetAlive(true);
triangle = new Triangle(850 +30+ Cameraposition[0]+xoff,319);
objects.push_back(triangle);
triangle->SetAlive(true);
break;
case 2:
//S
square = new Square(850 + Cameraposition[0]+xoff,310);
objects.push_back(square);
square->SetAlive(true);
break;
case 3:
//SS
square = new Square(850 + Cameraposition[0]+xoff,310);
objects.push_back(square);
square->SetAlive(true);
square = new Square(850 +82+ Cameraposition[0]+xoff,310);
objects.push_back(square);
square->SetAlive(true);
break;
}
}
`
Problem solved :(For those who experienced similar doubt)The above code solves the problem of creating random combination of obstacles and generating those combinations randomly. But do check for collisions if you don't want to get overlapping combinations.

Just add another for loop:
void createObstacles(int numObstacles)
{
for(int i = 0; i < numObstacles; i++)
{
if(rand() % 2 == 0)
{
// Create a triangle.
}
else
{
// Create a square.
}
}
}
Call this every time you want to create obstacles, controlling the number of objects released at one time by changing the parameter numObstacles.
You can now do:
if(rand() % 200 == 0)
{
// Create anywhere from 1 to maxToCreate (which is 4) obstacles at a time.
const int maxToCreate = 4;
int numToCreate = (rand() % maxToCreate) + 1;
// Call the function to make some obstacles.
createObstacles(numToCreate);
}
Since your shapes are overlapping, either fix your initialization code to initialize them the way

Related

SFML Collision with tilemaps

My current method of doing it is to check against my array and make cases for each different value I get. It works for small maps with only 1 or 2 collidable tiles but if I have over 100 I'd have to create over 100 cases and if the player enters another map I'd have to create more cases which is very inefficient. Is there a better way I can do tilemap collision with the player?
For example
int x = player.getPlayerPositionX() / map.getTileWidth();
int y = player.getPlayerPositionY() / map.getTileHeight();
switch (player.getDirection())
{
case 3:
if (map.data[x + y * map.getMapWidth()] == 1)
{
player.moveRect(1.0f, 0);
}
break;
case 4:
x = (player.getPlayerPositionX() + 20) / map.getTileWidth();
if (map.data[x + y * map.getMapWidth()] == 1)
{
player.moveRect(-1.0f, 0);
}
break;
}
Typically a tile in a game should be flagged as passable or impassable. There is no need to check for every possible case.
struct Tile {
bool passable;
};
bool collision(tileMap, playerCharacter) {
if(tileMap[playerCharacter.x][playerCharacter.y].Tile != passable) {
//some collision logic here
}
}
I think your question is about space partitionning. In your case, if you have tiles that have a known size, and are stored in a grid (2d array); you can use integer divisions (of your player coordinate by the tile width) to determine the index in the grid to check your collisions. For "conservativeness" you check the neighbors too, so that you are sure to span the size of the player and check activated tiles. I implemtented mutiple versions of that in extreme carnage, you can refer to the source in the zip https://sourceforge.net/projects/extremecarnage/ there is a binary tree version for landscape construction, and a grid/tile version for enemy storage partitionning

I don't get the expected feedback of a basic rectangle collision implementation in C++

Hi I am building a basic game in wich I have to detect collisions between an array of sprites. For now the sprites just move forward and change direction randomly or when reached a screen edge. The game is made in C++ with my college graphich library.
I implemented a basic algorithm for detecting collisions into this function:
bool DetectarColisionTanques(strTanque tanque1, strTanque tanque2) {
if(
(tanque1.px + tanque1.width >= tanque2.px) &&
(tanque1.px <= tanque2.px + tanque2.width) &&
(tanque1.py + tanque1.height >= tanque2.py) &&
(tanque1.py <= tanque2.py + tanque2.height)
) {
return true;
} else {
return false;
}
}
Sorry that code is in spanish, tanque1 and tanque2 are 2 structs containing each sprite coordinates wich I want to see if collide, px and py are the sprite origin at the upper left edge of the image.
But when I try to run the function i don't get the expected feedback:
void MoverEnemigos(){
for (int i=0;i<nEnemigos;i++) {
switch (enemigos[i].dir) {
case 0: enemigos[i].py-=enemigos[i].vel;break;
case 1: enemigos[i].px+=enemigos[i].vel;break;
case 2: enemigos[i].py+=enemigos[i].vel;break;
case 3: enemigos[i].px-=enemigos[i].vel;break;
for (int k= i+1; k<nEnemigos; k++) {
if(DetectarColisionTanques(enemigos[i],enemigos[k])) {
printf("Collision detected");
printf("\n");
}
}
}
enemigos[i].width= esat::SpriteWidth(spriteTanque[enemigos[i].dir]);
enemigos[i].height= esat::SpriteHeight(spriteTanque[enemigos[i].dir]);
}
}
In this function called MoverEnemigos() I move all the sprites px and py depending on their direction and then I try to call the function DetectarColisionTanques() to see if I get the printf at the same time I see in my graphic window that the sprites are obviously colliding. But I never ever get the printf.
PD: All the functions are running in a while at 60fps.

Slowdown when loading the same SpriteFrame used in an animation in Cocos2dx

I am currently experiencing some heavy slowdowns with my game. I have narrowed it down to something related with texture animations.
In my game there are characters that walk in 1 of 4 possible directions, they will walk up to a point, then change direction and continue walking (sort of like a tower defense game).
First i am loading the sprite frame cache like this
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("characters.plist");
This code is only run once during the life time of my application.
When the characters get loaded to the screen their animation is being set using the following code:
int direction = 0;
int number = 0;
if (this->to_x < 0) // Left
{
direction = 1;
number = 1;
}
else if(this->to_x > 0) // Right
{
direction = 2;
number = 1;
}
if (this->to_y < 0) // Down
{
direction = 0;
number = 0;
}
else if(this->to_y > 0) // Up
{
direction = 3;
number = 2;
}
int s = 0; //skin
// Set the animation
Animation *animation = Animation::create();
for (int i = 0; i < INT16_MAX; i++)
{
string frame_sprite_name = StringUtils::format("%s_%d_%d_%d.png",parameters[name].image_name.c_str(),s,number,i);
auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frame_sprite_name);
if (frame) {
animation->addSpriteFrame(frame);
} else {
break;
}
}
// Invert the sprite when they go right
if (direction == 2) {
setFlippedX(true);
}else{
setFlippedX(false);
}
// Set the pace of the animation based on the type
if (name=="runner") {
animation->setDelayPerUnit(0.15f);
} else{
animation->setDelayPerUnit(0.3f);
}
Animate *animate = Animate::create(animation);
this->stopAllActions();
this->runAction(RepeatForever::create(animate));
What this code does is:
Check the direction
Get the sprite frame from the cache based on the direction
Run the action with repeat forever.
However this code is ran every time they change direction to set the new animation of the active characters. Also, at one time I can have around 40-50 of these characters going around.
I've noticed that after a few minutes in the game the slowdown starts to happen as soon as a new "character" is created, (since they are created in rapid succession in waves). And the slowdown also happens when the characters change in direction. So this makes me believe I am using the textures wrong.
If anyone knows how to fix this please let me know.
PD: I was thinking about the possibility of pre-loading all the animations and then just having each of the sprites that represent the characters run the corresponding animation.
You should definitely cache the animation in the AnimationCache with addAnimation and getAnimation methods.

SDL 2 bullet collision not working?

I'm new to SDL 2 and I've been stuck on a collision detection between a bullet and a spaceship for some time.
I understand what has to happen, but I'm getting confused with what collides with what, and which way around to put the code. The collision detection doesn't work, and some other attempts I've made results in the bullet disappearing at the spaceships y coordinate, even if it is on the opposite side of the screen! I've based it around a lazy foo tutorial.
My whole code is here: http://codepad.org/YaCavBvm
My collision detection based around LazyFoo:
//sides of sprite rectangles
int leftBullet, leftEnemy;
int rightBullet, rightEnemy;
int topBullet, topEnemy;
int bottomBullet, bottomEnemy;
//Calculate the sides of bullet
leftBullet = posBullet.x;
rightBullet = posBullet.x + BULLET_WIDTH;
topBullet = posBullet.y;
bottomBullet = posBullet.y + BULLET_HEIGHT;
//Calculate the sides of enemy
leftEnemy = posEnemy.x;
rightEnemy = posEnemy.x + SPRITE_WIDTH;
topEnemy = posEnemy.y;
bottomEnemy = posEnemy.y + SPRITE_HEIGHT;
for (int i=0; i<MAX_BULLETS; i++)
{
if (topBullet == bottomEnemy)
{
arrayofBullets[i].isActive = false;
}
}
In theory, a bullet grazing the bottom of your foot is a hit but you could regard that as a hit or a miss. Try the following
if (topBullet >= topEnemy &&
bottomBullet <= bottomEnemy &&
leftBullet >= leftEnemy &&
rightBullet <= rightEnemy)
// What happens when the bullet hits
This will take care of the partial hits. The one in lazyfoo is for one particular case which is correct in the context of the example.

OpenGL L-System Rendering

I have gotten my basic L-System working and I decided to try and optimize the rendering of the application. Previously I was looping through the whole string of the L-System with a switch case and drawing. Better yet I will show you what I was doing:
for(unsigned int stringLoop = 0; stringLoop < _buildString.length(); stringLoop++)
{
switch(_buildString.at(stringLoop))
{
case'X':
//Do Nothing
//X is there just to facilitate the Curve of the plant
break;
case'F':
_prevState = _currState;
_currState.position += _currState.direction * stdBranchLength;
//Set the previous state to the current state
_graphics.SetColour3f(0.0f, 1.0f, 0.0f);
_graphics.Begin(OGLFlags::LINE_STRIP);
_graphics.Vertex3f(_prevState.position.X(), _prevState.position.Y(), _prevState.position.Z());
_graphics.Vertex3f(_currState.position.X(), _currState.position.Y(), _currState.position.Z());
_graphics.End();
break;
case'[':
_prevStack.push(_currState);
break;
case']':
_prevState = _currState;
_currState = _prevStack.top();
_prevStack.pop();
break;
case'-':
_currState.direction = _currState.direction.RotatedAboutZ(-(ROTATION) * Math::DegreesToRadians);
break;
case'+':
_currState.direction = _currState.direction.RotatedAboutZ(ROTATION * Math::DegreesToRadians);
break;
};
}
I removed all of this because I was literally resolving the tree every single frame, I changed this loop so that it would save all of the verticies in a std vector.
for(unsigned int stringLoop = 0; stringLoop < _buildString.length(); stringLoop++)
{
switch(_buildString.at(stringLoop))
{
case'X':
break;
case'F':
//_prevState = _currState;
_currState.position += _currState.direction * stdBranchLength;
_vertexVector.push_back(_currState.position);
break;
case'[':
_prevStack.push(_currState);
break;
case']':
_currState = _prevStack.top();
_prevStack.pop();
break;
case'-':
_currState.direction = _currState.direction.RotatedAboutZ(-(ROTATION) * Math::DegreesToRadians);
break;
case'+':
_currState.direction = _currState.direction.RotatedAboutZ(ROTATION * Math::DegreesToRadians);
break;
};
}
Now I changed my render loop so I just read straight from the vector array.
DesignPatterns::Facades::OpenGLFacade _graphics = DesignPatterns::Facades::openGLFacade::Instance();
_graphics.Begin(OGLFlags::LINE_STRIP);
for(unsigned int i = 0; i < _vertexVector.size(); i++)
{
_graphics.Vertex3f(_vertexVector.at(i).X(), _vertexVector.at(i).Y(), _vertexVector.at(i).Z());
}
_graphics.End();
Now my problem is that when I am using a vector array and I use Line Stip, I get extra artifact.
The first image is the original render that is the unoptimized one, then the second is the newer render which runs faster, and the thirds is a render of a dragon curve which uses no push and pops like the first two are using (I am pretty sure the push and pop is where the problems are coming in).
Is the problem with my logic here of storing verticies, or is it because I am using a line strip? I would just use lines, but it doesn't really work at all, it ends up looking more of a line_stipple.
Also sorry about the length of this post.
alt text http://img197.imageshack.us/img197/8030/bettera.jpg
alt text http://img23.imageshack.us/img23/3924/buggyrender.jpg
alt text http://img8.imageshack.us/img8/627/dragoncurve.jpg
You are getting those extra lines because you are using a LINE_STRIP.
In your 'F' case, push both end points of your line into the vector (like you were doing originally).
_vertexVector.push_back(_prevState.position);
_vertexVector.push_back(_currState.position);
And when you draw, use LINE_LIST instead.