EASY: Strange Formula Issue

Started by
11 comments, last by d h k 18 years, 10 months ago
(player.health/100)*32 is not the same as (100*32)/player.health!
player.health*32/100 would be the correct formula
(or with with floats and casts: (unsigned char)(player.health*0.32f) )
remember that an integer division always cuts off the decimal places (no rounding!)

Advertisement
Quote:Original post by d h k
Thanks for your reply, Zahlmann. I suppose your way is better; when you don't disable warnings it just doesn't have this hacking feel to it.

I do it your way now and it does not create any warnings or anything, but it gives wierd results. With player.health equals 100 it's fine, but once I put it to 99 or anything lower it seems to expand further to the right instead of becoming smaller... Although with my calculator the formula works fine...

Here's the code:

DrawLine ( globals.screen, player.position_x, player.position_y - 6, player.position_x + ( ( 100 * 32 ) / player.health ), player.position_y - 6 );


Er. You still want the health to be in the numerator and 100 to be in the denominator. It should be like:

DrawLine ( globals.screen, player.position_x, player.position_y - 6, player.position_x + ( ( player.health * 32 ) / 100 ), player.position_y - 6 );


Although, this really makes more sense as a method on the Player:

player.drawHealth(globals.screen);// in the Player's classvoid Player::drawHealth(Screen target) {  // first calculate width and y-position of the bar so that it isn't all one  // huge expression  int health_y = position_y - 6;  int health_w = position_x + ( ( health * 32 ) / 100 );  DrawLine(target, position_x, health_y, position_x + health_w, health_y);}


See how it eliminates all the "player." spam? :)
Allright, I finally got it. DOn't know why I messed up that formula, I guess I tried the correct formula in the calculator but then forgot to change it in the code or something stupid as that...

Thanks everybody.

Oh, and I will make it a method in player once I can find some time, right now I have lots of things in my code that can be better grouped together in functions/structures/methods...

This topic is closed to new replies.

Advertisement