This site requires JavaScript, please enable it in your browser!
Greenfoot back
Chip
Chip wrote ...

2016/11/4

Help with Math.Pow and doubles.

Chip Chip

2016/11/4

#
So in the game I am creating I'm trying to create a mechanic where the player accelerates faster the lower his health is. The basic maths for the line I'm trying to make is:
1
velocity = velocity*1.2*(101-health)^0.1;
I'm then going to move the player with:
1
setLocation(getX()+velocity, getY());
To do the power in the code I tried to use this code:
1
velocityCoefficient=Math.pow((healthCoefficient),x)
when
1
healthCoefficient=101-health;
and
1
private float x = 0.1f;
The problem is that for velocity to work in the setLocation line it needs to be an int, but 0.1 needs to be a float/double and Math.pow only returns doubles. Any way around this?
Super_Hippo Super_Hippo

2016/11/4

#
You could import the SmoothMover class and let your class extend it. Then you can move in doubles instead of only ints. I never used floats, so I don't know if there is any difference between the use of them and of doubles.
danpost danpost

2016/11/4

#
Super_Hippo wrote...
I don't know if there is any difference between the use of them (floats) and of doubles.
The only difference is in the precision. Doubles are more precise, as they use 8 bytes for storing them while floats only use 4. To explain the issue more in detail. You could cast the aruments of the 'setLocation' method to '(int)'; however, you lose precision when you use 'getX()' and 'getY()' to retrieve its current location. Hence, maintaining the current coordinates of the location of the actor as a double or float, as opposed to using an int, in fields will allow the precision to be maintained. This is precisely what a class like the SmoothMover class does, as well as providing methods to work with those fields.
Chip Chip

2016/11/6

#
Cheers for your help guys , sorry I didn't replay sooner, I was away for the weekend :)
You need to login to post a reply.