For Loop within Boolean Function

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

For Loop within Boolean Function

Post 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!
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: For Loop within Boolean Function

Post 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..
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: For Loop within Boolean Function

Post 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.
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: For Loop within Boolean Function

Post 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.
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: For Loop within Boolean Function

Post 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.
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: For Loop within Boolean Function

Post 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. 8-)
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: For Loop within Boolean Function

Post 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)
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
Khearts
ES Beta Backer
ES Beta Backer
Posts: 50
Joined: Sun Oct 10, 2010 5:07 pm
Current Project: Super Mario Bros Clone and 3D Engine
Favorite Gaming Platforms: Dreamcast
Programming Language of Choice: C/++

Re: For Loop within Boolean Function

Post by Khearts »

Not sure. I'll deal with that when I implement enemies. :mrgreen:
User avatar
JGorard159
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 44
Joined: Sun May 09, 2010 3:05 pm
Current Project: Azareal Online: MMO
Favorite Gaming Platforms: Virtual Boy. LOL WUT!?
Programming Language of Choice: Javascript
Contact:

Re: For Loop within Boolean Function

Post by JGorard159 »

Khearts wrote:Not sure. I'll deal with that when I implement enemies. :mrgreen:
The programmer's creed: making it up as we go along :lol:
Savannah Cart of Ollege and Design

Current Project: Azareal Online. Visit us at: http://azareal-online.comuv.com
Post Reply