[SOLVED] Converting a distance value to another one within a limited range

0 favourites
  • 5 posts
From the Asset Store
Electric Cage
$5.99 USD
Dodge electric balls and survive in an unpredictable spaceship within an electric cage!
  • Hi!

    I hope I explain myself well enough.

    I'm making a side-scrolling minigolf game. I used the catapult code as an example to make a little "puncher" that can only separate from the ball a certain distance, like the pig from the example does with the top of the catapult. Now I can easily make an impulse based on that distance, so I can control the limit by dividing it or whatever.

    But I'd like this maximum distance that goes from 0 to, let's say, 25,3 (in pixels) to show as 10 in a textbox. So no matter the real value it's working behind the scenes, I want the max distance to be represented as 10 (you now, something rounder, 0 to 10 in power). Or 100, or whatever the number I like.

    Some help with the formula? I'm terrible at math :(

  • Hey there, are you familiar with the lerp() and unlerp() expressions? They could help to solve your problem

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Say, the total distance is 25.3px, you want to represent it as "10".

    Add a variable f=10/23.5=0.4255

    So, for example, as the ball flies you can show the distance it has traveled as (RealDistanceInPixels*f)

  • Hey there, are you familiar with the lerp() and unlerp() expressions? They could help to solve your problem

    Yeah, kind of, though I use that mainly for camera movement or enemy behavior.

    Mechanically what I'm doing works flawlessly. It's just that I want to transform an "ugly" value into a rounder, more pleasant one. It's easier to understand power from 1 to 10 than 1 to 234.5

  • Solved it!

    (round(distance(PowerPuncher.X,PowerPuncher.Y,BallBox.X,BallBox.Y))/40)*10

    40 is the max px the puncher can travel from the ball, limiting the power it's gonna use to punch the ball off. Now It's represented as a 10 in a text box :)

    Thanks for the help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)