Page 1 of 1
For Loop within Boolean Function
Posted: Sun Oct 24, 2010 3:52 am
by Khearts
I have a boolean function:
Code: Select all
bool Level::checkcoll(Character character)
{
for(int i = 0; i < Objects.size(); i++)
{
if(character.ypos + character.CSprite.rect.h <= Objects[i].hObjectSprite.rect.y) return false;
if(character.ypos >= Objects[i].hObjectSprite.rect.y + Objects[i].hObjectSprite.rect.h) return false;
if(character.xpos + character.CSprite.rect.w <= Objects[i].hObjectSprite.rect.x) return false;
if(character.xpos >= Objects[i].hObjectSprite.rect.x + Objects[i].hObjectSprite.rect.w) return false;
return true;
}
return true;
}
that contains a for loop. However, when I call this function for example:
Code: Select all
if(LevelOne.checkcoll(Mario)==true){/*Doing stuff here*/}
it will only perform "stuff" only for the first element (Objects[0]) and the rest will be ignored. I want to be able to have ALL of my objects being checked for collision instead of just one. My best guess is since the boolean function automatically returns a false or true upon the first element, the rest will be essentially ignored. I need to be able to have all of my elements checked.
Thanks!
Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 4:48 am
by adikid89
Khearts wrote:
Code: Select all
bool Level::checkcoll(Character character)
{
for(int i = 0; i < Objects.size(); i++)
{
if(character.ypos + character.CSprite.rect.h <= Objects[i].hObjectSprite.rect.y) return false;
if(character.ypos >= Objects[i].hObjectSprite.rect.y + Objects[i].hObjectSprite.rect.h) return false;
if(character.xpos + character.CSprite.rect.w <= Objects[i].hObjectSprite.rect.x) return false;
if(character.xpos >= Objects[i].hObjectSprite.rect.x + Objects[i].hObjectSprite.rect.w) return false;
return true; //loops stops here.. and function return true if it didn't return false before...
}
return true;
}
So yeah.. you have an extra return there..
Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 4:50 am
by Khearts
Right. I am aware of that. If I take either of them out, I will still have the same problem. I was just messing around with that earlier today and forgot to remove it.
I also left both in there because:
The one inside the for loop is needed for the collision to actually return true.
The one outside needs to have some value return or else I get a warning. True or false doesn't seem to make a difference.
Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 5:10 am
by adikid89
If you want to check collision with all objects, you can do something like this:
Code: Select all
void CheckCollisions(const Character& player, vector<Object*>& collisions)
{
for(int i=0; i<Objects.size(); i++) {
if(Collision(player.rect(), Objects[i].rect());
collisions.push_back(Objects[i]);
}
//this will fill a vector that holds the object with which the player has collided if any
}
It's also better to send the character by reference... more efficient.
Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 5:26 am
by Khearts
A few things about that:
1. If I'm calling your proposed function each frame, wouldn't I be adding the same object that I am on top of continuously to the vector?
2. What would I be doing with the items stored in that vector?
3. If there's something I can do with my existing for-loop embedded within my boolean, it'll be nice just to know for future implementations of other things I may need to cycle through later.
Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 6:45 am
by Khearts
It's nearly 5AM in the morning and after 4 cans of Brisk Iced Tea, I finally gotten around to working this damn thing. I went ahead and dropped the boolean check and implemented somewhat of what adikid89 suggested. It may be a lot easier this way, too.
Code: Select all
void Level::Coll(Character &character)
{
for(int i = 0; i < Objects.size(); i++)
{
if(!(character.ypos + character.CSprite.rect.h <= Objects[i].hObjectSprite.rect.y) &&
!(character.ypos >= Objects[i].hObjectSprite.rect.y + Objects[i].hObjectSprite.rect.h) &&
!(character.xpos + character.CSprite.rect.w <= Objects[i].hObjectSprite.rect.x) &&
!(character.xpos >= Objects[i].hObjectSprite.rect.x + Objects[i].hObjectSprite.rect.w))
{
//do shit upon collision
}
}
}
May look a tad messy but gets the job done.
Now, to reward myself with sleep.

Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 8:04 am
by adikid89
Khearts wrote:A few things about that:
1. If I'm calling your proposed function each frame, wouldn't I be adding the same object that I am on top of continuously to the vector?
2. What would I be doing with the items stored in that vector?
3. If there's something I can do with my existing for-loop embedded within my boolean, it'll be nice just to know for future implementations of other things I may need to cycle through later.
1. Yes you would.
2. Handle the collision ? Maybe inside your character somewhere ?
I used to handle collisions like this:
inside main loop:
Code: Select all
EntityManager::GetInstance()->Update();
inside EntityManager::Update() :
Code: Select all
vector<Entity*> collision;
for(int i=0; i<entities.size(); i++) {
for(int j0; j<entities.size(); j++) {
if(collision(entities[i]->rect(), entities[j]->rect()) {
//entity i has collided with j
collision.push_back(entities[j]);
}
//done with entity i.. send the vector with all the colliding entities to handle them
entities[i]->HandleCollision(collision);
}
Granted... it's probably not the best way to handle collision... but it is A way.

. Also... it's a n^2 "algorithm".
inside HandleCollision():
Code: Select all
void Player::UpdateCollision(vector<Entity*>& collision)
{
for(int i=0; i<collision.size(); i++) {
if(collision[i]->GetType() == TYPE_BULLET) {
if(((Bullet*)collision[i])->owner() != this) {
this->Die();
}
}
}
}
3. You fixed that yourself

. But still... how will you handle collision when you add enemies ?
(P.S. I had some annoying problems solving collisions like this so I don't recommended)
Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 1:47 pm
by Khearts
Not sure. I'll deal with that when I implement enemies.

Re: For Loop within Boolean Function
Posted: Sun Oct 24, 2010 1:51 pm
by JGorard159
Khearts wrote:Not sure. I'll deal with that when I implement enemies.

The programmer's creed: making it up as we go along
