Problem with division

Talk about making games.

Problem with division

Postby zygoth » Thu Jan 31, 2008 2:46 am

I have a health bar with 56 animations and I want it to reflect the health of the player. Here's my code:

Draw Actor -->healthbar
animpos = abs(((health / maxhealth) * 55) - 55)

the absolute value is there because the health bar is going the opposite direction it needs to.
I tried writing (health / maxhealth) as a float variable and replacing it with the float variable "floaty" but it still didn't work

like this

Draw Actor -->healthbar

float floaty
floaty = health / maxhealth
can anybody help me? It seems like this code should work.
Nova: 100% Vertigo/Mazeman: 100%
Visit my website to download them both! http://www.ketonegames.com
User avatar
zygoth
 
Posts: 140
Joined: Mon Jun 11, 2007 6:37 pm
Score: 5 Give a positive score

Re: Problem with division

Postby stevenp » Thu Jan 31, 2008 3:03 am

see post blelow
Last edited by stevenp on Fri Feb 01, 2008 12:59 am, edited 1 time in total.
User avatar
stevenp
 
Posts: 403
Joined: Sat Dec 22, 2007 12:49 pm
Location: canada, ontario
Score: 16 Give a positive score

Re: Problem with division

Postby Fuzzy » Thu Jan 31, 2008 6:26 am

floaty is a terrible name for a variable. Its too close to float.

I can see that you would get a division by zero error with that, possibly. Is that the problem?

Multiplication can take the place of division thus avoiding a division by zero error. if you wanted to divide by 4, for instance, you can multiply by 0.25.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Problem with division

Postby DilloDude » Thu Jan 31, 2008 8:52 am

If health is an integer, then it will do an integer division, so the result is cast to an integer. If you use (double)health, health will be cast to a real number first, fixing the problem:
Code: Select all
animpos = abs((((double)health / maxhealth) * 55) - 55);
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Problem with division

Postby pyrometal » Thu Jan 31, 2008 10:22 pm

I think the problem is that you are trying to assing a real value to animpos (which is an integer). To fix this, try add either floor() or ceil() to your function to truncate your vale to the nearest integer.

Code: Select all
animpos = floor(abs(((health / maxhealth) * 55) - 55));


I think you should also consider what dillodude has mention as well, although I do beleive it is possible without the pointer specifier.

That's all!

-- Pyro
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Problem with division

Postby zygoth » Thu Jan 31, 2008 11:00 pm

Thanks a lot, guys, it worked... I was getting really frustrated with it, but i put in your code and it worked fine.
Nova: 100% Vertigo/Mazeman: 100%
Visit my website to download them both! http://www.ketonegames.com
User avatar
zygoth
 
Posts: 140
Joined: Mon Jun 11, 2007 6:37 pm
Score: 5 Give a positive score

Re: Problem with division

Postby pyrometal » Fri Feb 01, 2008 1:08 am

I'm curious, did flooring your value help? I had that problem in the past as well and that's how I fixed it.
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron