4

So I am trying to make a game with a health bar for the player but whenever I use cross products or any other method of math that should work the percentage result is 0.0... Here is the .java file that handles this:

package com.game.graphics;

import com.game.entity.mob.Mob;

public class HUD {
    Mob target;
    Sprite healthbar;
    Sprite bgBar;

    double percent_normal;

    public HUD(Mob target) {
        this.target = target;
        bgBar = new Sprite(0xff777777, 104, 8);
        healthbar = new Sprite(0xffFF0000, 100, 6);
    }

    public void update() {
        percent_normal = target.getHealth() / 100;
        percent_normal *= target.getMaxHealth();
    }

    public void printData() {
        System.out.println("Normal(" + percent_normal + ")");
    // if the targets's health is any lower than the max health then this seems to always return 0
    }

    public void render(Screen s) {
        s.renderSprite(5, 5, bgBar, false);
        s.renderSprite(7, 6, new Sprite(0xffFF0000, (int) percent_normal, 6), false);
    }
}

I'm not sure if my Math is completely off or if I'm going about this horibly wrong. I've tried looking up how to calculate it to no avail so that's why I'm putting this question here.

Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
draco miner
  • 43
  • 1
  • 1
  • 4
  • 1
    How do you calculate percentages in _math_? Do you divide by 100, or multiply? O.o – xrisk Aug 17 '15 at 12:33
  • 6
    Is `target.getHealth()` an integer? `/100` would be integer division, and round down (most likely to zero). – Teepeemm Aug 17 '15 at 12:36
  • 1
    possible duplicate of [Why does dividing a float by an integer return 0.0?](http://stackoverflow.com/questions/3779604/why-does-dividing-a-float-by-an-integer-return-0-0) – MarsAtomic Aug 17 '15 at 20:30

2 Answers2

8

Shouldn't it be this? :

public void update() {
    percent_normal = target.getHealth() / target.getMaxHealth();
    percent_normal *= 100;
}

E.g. : Player has 120 health and maximum health is 180. This is, 120 / 180 = 0.66666666666 which gives in percentage 0.66666666666 * 100 = 66,66...% instead of 120 / 100 = 1.2 and multiplying this by target.getMaxHealth() which is 180, results in (1.2 * 180) > 100.

Kevin
  • 2,813
  • 3
  • 20
  • 30
  • 1
    @sircapsalot It's a perfectly real operator and not an issue of faith. The first division requires a cast to double for either health or maxHealth (since they're probably integers) to avoid integer division and 0 result. – Kayaman Aug 17 '15 at 12:37
  • @sircapsalot It is not a matter of belief. You just need to open the JLS and check. – biziclop Aug 17 '15 at 12:37
  • sorry, i was looking at [this page](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html) which did not include `*=`. [this one](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) does :) – ddavison Aug 17 '15 at 12:38
  • @RishavKundu in sircapsalot's defence, it is not because you have an extensive experience in Java that you commonly us the *= operator. – ThomasS Aug 17 '15 at 12:45
  • 1
    @sircapsalot yes sorry that was a stupid remark to make :/ i apologise – xrisk Aug 17 '15 at 12:49
8

Your way of calculating the percentage seems to be off. To calculate the percentage of X compared to Y you have to do:

double result = X/Y;
result = result * 100;

So in this case your code should look like

public void update() {
    percent_normal = target.getHealth() / target.getMaxHealth();
    percent_normal *= 100;
}

The reason you are always getting 0.0 although is because of the int 100. I believe the target.getHealth()/100 will return a value that is in the form of 0.x and will be turned into int 0 before being cast to double.

ThomasS
  • 705
  • 1
  • 11
  • 30
  • I tried this. (Thank you so much for explaining why it works that way!) Here's what I have now and it works wonderfully: [link](http://pastebin.com/DSe85Svw) – draco miner Aug 17 '15 at 16:46
  • I'm glad your problem has been fixed! Could you accept one of the answers to help the people who might have the same problem? – ThomasS Aug 17 '15 at 17:56