Need help with an algebraic glitch in my programming

0 favourites
  • 5 posts
From the Asset Store
Tweakable and easy to use effects for your projects.
  • So for my start menu, I'm trying to create a button that vibrates more as the mouse gets closer to the center. My current programming for this is as follows:

    X=124+random(-(Mouse.X-160), (Mouse.X-160))

    Y=78+random(-(Mouse.Y-90), (Mouse.Y-90))

    If you can't make out what all this gibberish means, its purpose is to

    Set X to (the button's position) +/- a random number that gets exponentially bigger as the mouse gets closer

    Set Y to (the button's position) +/- a random number that gets exponentially bigger as the mouse gets closer

    This programming works fine, except the effect I want is inverted. Currently, the button vibrates more as the mouse gets further away, and less as it gets closer, which is the opposite of what I want.

    ___________________________

    UPDATE:

    I've sort of fixed the problem by getting the reciprocal of the increments, giving

    X = 124+1/(random(-(Mouse.X-160), (Mouse.X-160)))

    Y = 78+1/(random(-(Mouse.Y-90), (Mouse.Y-90)))

    This does however create a weird glitch where the button moves to really far positions along the axes, so I now need help in fixing that.

  • There is a range in play here.

    If we normalise the range to '0 - 1', things get easier. To do this we use a scale factor.

    ScaleFactor = 1 / (max - min)

    Since min is zero in your case, ScaleFactor = 1 / max

    Max (in your case) = Sprite.LineOfSight.Range

    Or .. ScaleFactor = (1 / Sprite.LineOfSight.Range)

    Any distance normalised is now DistanceNormalised = (1 / Sprite.LineOfSight.Range) * Distance

    Say the Range is = 300 And we are 200 away:

    (1 / 300 ) * 200 = 0.666

    Say the Range is = 300 And we are 75 away:

    (1 / 300 ) * 75 = 0.25

    So we clearly now have a value between zero and 1, representing the range (normalised), and it is a small number when closer and bigger when further away. So lets reverse that.

    ScaleFactor = (1 - (1 / Sprite.LineOfSight.Range))

    or DistanceNormalised = (1 - (1 / Sprite.LineOfSight.Range)) * Distance

    Only problem now is that things go negative when out of range.

    We solve that by ... DistanceNormalised = max((1 - (1 / Sprite.LineOfSight.Range)),0) * (Distance)

    Distance = distance(button.x,button.y,mouse.x,mouse.y)

    So we gonna add a small random to the button.X and button.y, that is proportional to DistanceNormalised

    random(DistanceNormalised , 0 )

    Set button position to ...

    x = Button.RestPositionX + (ScalarX * ((random( (max((1 - (1 / Sprite.LineOfSight.Range)),0) * (distance(button.x,button.y,mouse.x,mouse.y))) , 0 ))))

    y = Button.RestPositionY + (ScalarY * ((random( (max((1 - (1 / Sprite.LineOfSight.Range)),0) * (distance(button.x,button.y,mouse.x,mouse.y))) , 0 ))))

    Choose a value for ScalarX and ScalarY to your liking.

    Button.RestPositionX is the position for the button in rest, (else he gonna vibrate off screen) stored in an instance variable 'RestPositionX'.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for your reply, but using the programming that you've told me to use the button only vibrates towards the bottom right of the screen. I want it to vibrate moving around the center, not in an increasing direction. If you could tell me how to fix it based on my original equation, it would be greatly appreciated.

  • UPDATE: sort of fixed the problem by getting the reciprocal of the increments, giving

    X = 124+1/(random(-(Mouse.X-160), (Mouse.X-160)))

    Y = 78+1/(random(-(Mouse.Y-90), (Mouse.Y-90)))

    This does however create a weird glitch where the button moves to really far positions along the axes, so I now need help in fixing that.

  • Ohh you are right.

    x = Button.RestPositionX + (choose(1,-1) * (ScalarX * ((random( (max((1 - (1 / Button.LineOfSight.Range)),0) * (distance(button.x,button.y,mouse.x,mouse.y))) , 0 )))))

    y = Button.RestPositionY + (choose(1,-1) * (ScalarY * ((random( (max((1 - (1 / Button.LineOfSight.Range)),0) * (distance(button.x,button.y,mouse.x,mouse.y))) , 0 )))))

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