Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Convert distance to percentage
Started by dan.hunter Jan 28 2015 08:22 AM

- - - - -
7 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dan.hunter

[GLOBAL: userInfoPane.html]
dan.hunter
  • Observer

  • 20 posts
  • Corona SDK

Hello,

 

I need help converting the distance between two objects to a percentage.

 

I have two objects, one stationary (A), and another that moves ( B). When B is less than a maximum distance to A (in this case 200), something starts to happen to A. But what I also want is within that radius for a percentage to be calculated, with 0% being maximum distance and 100% being on top of object A. So the percentage of A increases the closer you get.

 

Any help would be greatly appreciated.

 

Thanks,

Dan



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,534 posts
  • Enterprise

local percent = 1 - ( currentDistance / maxDistance )

 

Rob



[TOPIC: post.html]
#3

dan.hunter

[GLOBAL: userInfoPane.html]
dan.hunter
  • Observer

  • 20 posts
  • Corona SDK

Perfect.

 

As always, thanks Rob.



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,534 posts
  • Enterprise

I should have been clear.  That will give you a value of 0 .. 1 where 1 is 100%.  If you want to display that as a percentage, multiply the number by 100 to convert it to a percentage. 



[TOPIC: post.html]
#5

dan.hunter

[GLOBAL: userInfoPane.html]
dan.hunter
  • Observer

  • 20 posts
  • Corona SDK

I actually wanted a value of 0 to 1. So that worked out great.

 

Thanks again.



[TOPIC: post.html]
#6

FearTec

[GLOBAL: userInfoPane.html]
FearTec
  • Contributor

  • 218 posts
  • Corona SDK

function GetPercentLeftInTravelPath( currentDistance, distanceToTravel)
   local tempnumber
   tempnumber = (1 - ( currentDistance / distanceToTravel)) * 100
   return tempnumber
end
print ("GetPercentLeftInTravelPath: " .. GetPercentLeftInTravelPath(67.2,100) .. "%")

 

function GetPercentTravelledAlongPath( currentDistance, distanceToTravel)
   local tempnumber
   tempnumber = 100 * ( currentDistance / distanceToTravel)
   return tempnumber
end
print ("GetPercentTravelledAlongPath: " .. GetPercentTravelledAlongPath(45.1,100) .. "%")



[TOPIC: post.html]
#7

FearTec

[GLOBAL: userInfoPane.html]
FearTec
  • Contributor

  • 218 posts
  • Corona SDK

And using percentages to get distances where the portion travelled and total to travel is known

 

function GetDistanceLeftToTravelPath(percentTravelled, totalPathDistance)
    local tempdist
    if (percentTravelled == 100) then
        tempdist = totalPathDistance
    else
        tempdist = totalPathDistance - ((percentTravelled / 100) * totalPathDistance)
    end
   return tempdist
end
print ("GetDistanceLeftToTravelPath: " .. GetDistanceLeftToTravelPath(33,1000))

function GetDistanceTravelledOnPath(percentTravelled, totalPathDistance)
    local tempdist
    if (percentTravelled == 100) then
        tempdist = totalPathDistance
    else
        tempdist = (percentTravelled / 100) * totalPathDistance
    end
   return tempdist
end
print ("GetDistanceTravelledOnPath: " .. GetDistanceTravelledOnPath(33,1000))



[TOPIC: post.html]
#8

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,534 posts
  • Enterprise

These are useful. You should bundle them up and add them to the community code.

 

Rob




[topic_controls]
[/topic_controls]