Simple bugs – totally wrong behavior

By , last updated May 17, 2015

This goes for most programming languages I guess. But especially for C++ when you are making a game.

I was working on a code that places blocks in a 3d space ship. The block was supposed to be placed one after each other. But instead, the blocks were placed partly within each other.

Block in a 3d space ship in Burnt Islands

The reason is this code:

bool SpaceShip::checkPosition(double objectInFocus, double &pos)
		if (objectInFocus > pos)
			double rounded = round(pos);
			double pos = rounded - 1.0;

			//if 1 too much
			if ((pos - rounded) > 1.0)
				pos = rounded; 

			return true;

		return false;

The result was sometimes updated and sometimes don’t. Why?

The answer is a c++ topic named “variable scope“. Look at line 6 of the code. What did I do wrong?


Be the first to comment.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>