Jump to content

Always false, never true

- - - - -

  • Please log in to reply
11 replies to this topic

#1
Rei_NERV

Rei_NERV

    Newbie

  • Members
  • PipPip
  • 28 posts
Why won't this return true?

bool Bullet::bullethit(Image b)

{

	if((centerx > b.left && centerx < b.right) && (centery > b.top && centery < b.bottom))

		return true;

	else

		return false;

}



#2
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
If this is supposed to be a normal (x,y) coordinate type, then your inequalities are backwards. (centery > b.top) means that the center is ABOVE the top. (centery < b.bottom) means that it's BELOW the bottom. It's quite impossible for it to be both simultaneously. Just flip all of the signs around and you should be fine.

#3
Rei_NERV

Rei_NERV

    Newbie

  • Members
  • PipPip
  • 28 posts
The x,y coordinates are in allegro, and in allegro, the higher you go, the smaller the y gets.
Think of it as, x = 0; y = 0; are the coordinates of the top left corner of the screen.

#4
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Thats weird, the code looks right.
You should try to breakpoint the function and see the values.
My best guess is that you are passing a wrong image in to the function.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#5
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
may be you need equality in this conditions ,
(centerx >= b.left && centerx <= b.right)
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#6
Rei_NERV

Rei_NERV

    Newbie

  • Members
  • PipPip
  • 28 posts
Equality didn't do the trick.
And unfortunately, I am passing the right image to the function. I wish it was wrong :P

#7
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Check the values of centerx and centery during runtime. Perhaps they're not initializing correctly.

#8
Rei_NERV

Rei_NERV

    Newbie

  • Members
  • PipPip
  • 28 posts
They are updating correctly. Which probably means that they initialize correctly, right?

#9
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
well this is a weird code :), make sure that you didnt swap the centerx.left and right so the condition may not become valid.
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#10
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts

Rei_NERV said:

They are updating correctly. Which probably means that they initialize correctly, right?

Yep, that would probably mean that. Have you tried stepping into the if statement with a debugger to find out exactly what's going on here?

Alternatively, some more code would be helpful, something we can execute to test it.

#11
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Is Image custom type that you coded? If so, then did you check left, right, top and bottom fields as they may be calculated wrong?

#12
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
When I'm at a loss, I can sometimes easily debug my code by printing out the values of some variables. I have applied the concept to your code, it may help. Note that %d may have to be changed to %f or something else depending on your variable type.
bool Bullet::bullethit(Image b)
{
	printf("centerx=%d, b.left=%d, b.right=%d\n", centerx, b.left, b.right); 
	printf("centery=%d, b.top=%d, b.bottom=%d\n", centery, b.top, b.bottom);
	if((centerx > b.left && centerx < b.right) && (centery > b.top && centery < b.bottom))
		return true;
	else
		return false;
}[FONT=Tahoma]
[/FONT]
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users