Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Strange pointer problem

$
0
0
Hi D.I.C.

I'm working on a quadtree that will handle collision detection between the player object and all other objects. I've fixed a bunch of other stuff wrong and narrowed it down to one area I believe. The if block checks to see if the main target, that is the player object, is within the node first. I used a switch because it seemed to make more sense because the value I'm checking for is an enum, also switch is faster than if/else. Here is the function I use for that:

bool CQuadTreeNode::GetMainTarget()
{
	// This function searches for the player and camera objects in a node
	for( it = CollideList.begin(); it != CollideList.end(); it++ )
	{
		switch( (*it)->GetType() )
		{
			case VisibleGameObject::PLAYER:
				ptr_player = std::addressof( it );
				break;

			default:
				break;
		}
	}

	if ( ptr_player != NULL )
	{
		return true;
	}

	else
	{
		return false;
	}
}



When I stepped through the compiler, it shows that I'm doing this part correctly as it points to the player object. However, once I stepped through the actual collision check, the player pointer (ptr_player) points to the same object the iterator is pointing at, even though it was already set to point at the player object. I'm quite confused as to why this is happening. Here's the collision check function.

if( GetMainTarget() == false )
		{
			cout << "ALERT!!!!! Main target isn't found!" << endl << endl;
			// If the camera isn't in this node then exit immediately.
			return;
		}

		else
		{
			cout << "Main Target found!!!" << endl << endl;

			// Update each object first
			for( it = CollideList.begin(); it != CollideList.end(); it++ )
			{
				(*it)->Update( elapsedTime );
				
				this->GetDimensions( &lrx, &lry, &w, &h );
				
				// Checking to see if object is inside the node bounds
				left1 = lrx - w;
				right1 = lrx;
				top1 = lry - h;
				bottom1 = lry;

				left2 = (*it)->GetPosition().x;
				right2 = (*it)->GetPosition().x + (*it)->GetWidth();
				top2 = (*it)->GetPosition().y + (*it)->GetHeight();
				bottom2 = (*it)->GetPosition().y;

				if( bottom2 > top1 || top2 < bottom1 || left2 < right1 || right2 > left1 )
				{
					cout << "Object " << (*it)->GetName() << " is INSIDE Node bounds!" << endl;
					// If the object is still within bounds, keep going
					
					if( (*it) != (**ptr_player) )
					{
						// If this is NOT the player 
						cout << "CHECKING COLLISION!" << endl << endl << endl<< endl<< endl<< endl;
						(**ptr_player)->CollisionResponse( (*it) );
					}					
				}

				else
				{
					cout << "ALERT!!!! OBJECT " << (*it)->GetName() << " HAS LEFT NODE BOUNDS!" << endl;

					// If not within the bounds then remove this object from the list
					CollideList.erase( it );

					// And THEN add it back to the main node so it can get put in the right place
					MainNode->AddObject( (*it) );
				} 

				(*it)->Draw( window );
			}
		}
	}
}



The overloaded comparison operator checks to see if the name of the objects are the same. At first, I had that part wrong, but I fixed that, but that should have nothing to do with why the ptr_player is getting changed.

Note: I removed the checking for the camera part if you are wondering. Sorry for the double post but I can't edit posts(?)

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>