- Home /
Math - distance to percentage
Im trying to convert the percentage of a level and its completion based on distance to the end object So in each level there is a spawn point and an end point, ive got the distance between the player and the end point but Im struggling to work out a percentage?
Logically Im guessing itd be to get the distance between spawn and end as well as player and end then im stuck, would it be something like distance between spawn and end multiplied by 100 divided by distance berween player and end point then round it to the nearest int?
Any help appreciated, thanks
Answer by poncho · Jan 31, 2011 at 04:56 PM
well, you can have the distance from the spawn point and the object, then the distance from the spawn to the end lets call distancespawnobject and distancespawnend divide distancespawnobject / distancespawnend and the result multiplied by 100 or the 10 factor to make the result a number between 0-100
(spawnToPlayer / spawnToEnd) * 100 will be a percent from 0.0 to 100.0
yeah but i had a problem like this when in ipad the values for some reason were scaled ins$$anonymous$$d of 1 was .001 an i had to add a 1000 factor to clean that mess XD thats why write "or the 10 factor to make the result a number between 0-100"
is it possible to make paths and do it like that ins$$anonymous$$d? it seems to be going higher than 100% which i understand why, because youve gone passed the marker, so would it be possible to make a path and work out a percentage based on where you are on the path?
What you might want to use the distance from the end point to the player rather than the spawnpoint to the player. This might result in negative numbers if they go in the opposite direction at the start, so just don't let it go below zero. Essentially, you'd be defining a circle around the end point with max radius the distance to the spawn point, anything more is 'out of bounds' and clamped to zero
if you are doing a 2d game, you can work with the x on perspective at your camera, y|x then you need to make a pathBeginX and pathEndX like pathBeginX |o_$$anonymous$$ pathEndX 0|object at 4 (7.5%) ---------------------|40 this way even if you are on x 4 but on y like 50 or something the "path" would say that you are on 7.5% of your path if this not help, please explain more
Answer by yoyo · Jan 31, 2011 at 06:07 PM
If you don't want the complexity of paths, and you don't want the distance to go over 100% then use (100 * spawnToPlayer)/(spawnToPlayer + playerToEnd).
basically, i have a game where you run around a circuit so i dont want it to be a straight line id actually need a path, ive thought about it a bit more and i think ill have to look into how paths work but thanks for everyones help
you could create an array of points scattered along the your level that trace out the path, then find which point the player is closest to and use that to deter$$anonymous$$e distance estimate