Limit one basic function with another basic function

cvbattum

New member
Joined
Mar 10, 2019
Messages
3
For a game I'm making I'm trying to design a button that is repelled by the cursor like a magnet while being tethered to a virtual spring. The tether point is the center of the button (imagine a basic square). The force of this tether (or the force of the magnetic effect, depends on how you want to see it) defines how hard it feels to click the button. I've called this parameter power. For this function I've decided to simplify the entire square to a single point, which is again the tether. Now that I've explained the context, let's get into the math. I've figured out how to move this button in the opposite direction of the cursor-to-tether line so the only thing I'm calculating is the strength with which this happens (basically, how far away to move it in that direction). That's the result of the function I'm trying to create.

The simple, initial approach I used was this function: [MATH]moveAmount = \frac{1}{\sqrt{mouseDistance}}[/MATH]. This function moves the object relatively further away as the cursor gets closer to the tether point. However, this does not take into effect the spring effect. Right now, you could in theory move the object into infinity if computers would be precise enough. I want to add a spring effect that pulls the object back to the tether point as it gets pushed further away. That could look like this: [MATH]springPull = moveAmount^2[/MATH]. Now, as moveAmount approaches infinity, springPull will also approach infinity at a quadratic rate. However, I need to combine this function with the first one, because springPull should limit the total distance the object should be moved with.

My first question is how, in general, this effect of one function limiting the other can be achieved. My second question is how to add these two functions together without creating a circular dependency. How can you calculate moveAmount if the value of moveAmount itself is required for calculating the result?

Keep in mind that things can be simplified and don't need to be as physically accurate as possible. In the end, it just needs to feel good to the user. I would love a detailed explanation though about the most accurate way, so I can do the simplification myself. I want to understand this. For reference, I've passed calculus in uni but I was never very good at it. That was also over two years ago and I haven't used it much since. I have however not passed linear algebra.

I'm not entirely sure in which category of algebra this fits; it might be moving into calculus areas but at the same time I feel like this should be so simple.
 
If we try for a physically accurate model, we might suppose that the magnetic repulsion force is proportional to [MATH]\frac{1}{(mouseDistance + moveDistance)^2}[/MATH], using an inverse-square law and taking [MATH]mouseDistance[/MATH] to be the distance from the "tether point". The spring force would be proportional to [MATH]moveDistance[/MATH]. Equating the forces for equilibrium, we'd have [MATH]moveDistance = \frac{k}{(mouseDistance + moveDistance)^2}[/MATH]. This turns into a cubic equation in [MATH]moveDistance[/MATH], which would be beyond what you'd want to solve.

You might experiment with other laws to make it simpler, such as [MATH]moveDistance = \frac{k}{(mouseDistance + moveDistance)}[/MATH]. I suspect that would not work out, but something else might.

Note that this puts your question within algebra; that is the typical tool when, as you say, "the value of moveAmount itself is required for calculating the result ".
 
If I were going to program such a model, I think I would use an inverse square law for the "magnetic repulsion" and Hooke's Law for the restorative spring force (this is linear rather than quadratic, but you could choose whatever model you want). Thus, the net force on the button would be the sum of the two forces resolved into x and y components. And Newton's 2nd law of motion would form the basis for determining acceleration \(a\), that is:

[MATH]a=\frac{F}{m}[/MATH]
You would likely have to experiment with the mass of the button to get the results you want.

For both components, I would use the approximations:

[MATH]\Delta v\approx a\Delta t[/MATH]
[MATH]\Delta s\approx v\Delta t[/MATH]
You would essentially be using Euler's method for approximating the solutions to the interrelated initial value problems whose solutions would determine the velocity and position of the button.
 
Thanks for the replies! I also noticed that I forgot to give my post a sensible title with my tired head but that's fixed now!

Equating the forces for equilibrium, we'd have [MATH]moveDistance = \frac{k}{(mouseDistance + moveDistance)^2}[/MATH]. This turns into a cubic equation in [MATH]moveDistance[/MATH], which would be beyond what you'd want to solve.

I'm still having trouble understanding how this formula (or the simplified version [MATH]moveDistance = \frac{k}{(mouseDistance + moveDistance)}[/MATH]) should be calculated, when [MATH]moveDistance[/MATH] is a parameter to its own formula.

As for the physical simulation with Newton's law, I might want to try that but I was specifically aiming for a more simplified mathematical approach (mostly because I want to be able to make something like that, but also because in theory it earns some performance benefit by using less overhead)
 
Last edited:
Do you know algebra? My suggestion (which is indeed meant as a huge simplification over actual simulation) is just what one does in algeba: write multiple equations that represent different relationships, and then solve them. In this case, you need to solve for moveDistance as a function of mouseDistance. I'm leaving that for you to do, because you will have to experiment with various possible models, and can't be dependent on others to do the work.
 
There's a reason I dropped out of uni... Also, my brain is just really failing me on algebra right now. It's really the reason why I want to do this the "hard" way, as I haven't touched much math in two years now. I can use the refresher.

In this case, you need to solve for moveDistance as a function of mouseDistance

This bit will help, I'll try to do it as soon as I get home.
 
I'll be waiting ...

Note that if you read our submission guidelines, we ask you to tell us your background and where you are having trouble, so that we will know things like how much help you'll need with algebra. We'll be happy to help there, as long as we can see what you need (and you're showing effort).
 
Top