Mathematics
- If the mouse is at the blue ring, the object is at the black ring.
- If the mouse is at the red ring, the object is at the black ring.
- If the mouse is half way between the blue and black ring, the object is half way between the red and black ring.
- If the mouse is half way between the red and black ring, the object is half way between the blue and black ring.
Programming
- If the mouse is at the black ring, the object is at the red or blue ring depending on the direction the mouse was previously.
- If the mouse was at the red ring, but is now half way between the blue ring and the black ring, the object is a quarter the distance from the blue ring as the distance between the blue ring and the black ring, in the direction from the blue ring from the black ring.
The mathematics is the part im no good at. however if i have the formula i should be able to workout the programming part. does anyone know the formula its trigonometry if i recall.
ok perhaps its not everywhere, but assuming the object and the mouse starts in the centre, if the mouse is moved to 4,4 what is the formula to make the object move to 2,2, and when the mouse is at 5,5 the object is at 3,3.
That's not enough points to determine the formula you want, necessarily. But assuming you want somewhat linear scaling, then it sounds like you want the object coordinates to be half the mouse coordinates, maybe with rounding (I wouldn't round, it will cause jerkiness).
So, you're solving for a line that goes through 0,0, 4,2, and 5,3. There is no such line, but a line with slope .5 is a pretty good fit.
What that means is that the object moves at half the speed of the mouse.
If you don't want linear scaling, then you can arbitrarily add higher polynomial terms and more points to your equation.
It should be possible to fit a parabola to your points exactly, so let's try that. I put in "equation of a parabola through (0,0), (4,2), (5,3)" in wolfram alpha and got
y = (x^2)/10 + x/10.
Checking that, 4 for x yields 1.6 + .4 = 2.
and 5 for x yields 2.5 + .5 = 3
So yeah, that works.
Your function for the object point given the mouse point is:
Code:
function updateObjectLocation(e:MouseEvent):void{
object.x = object.y = e.localX*e.localX/10 + e.localX/10;
}
Assuming you only care about the x coordinate of the mouse.
I don't think this is really what you want, but it's what you asked for.
what comes to mind is the old black and white game, when you find fish at the shore, when you touch the water, the fish move away from that point, but they always stay in the same general location.
so think of like a magnet that only repels the grains of metal around it, however if the grains of metal get to close the boarder of the location, they try to find a way past the magnet to return to the center.
i'll try to find an actual example of this happening.
Okay, I get it.
You are not looking for a formula which gives a location based on the mouse location. Instead, you are trying to implement a very simple physical system with multiple forces.
Each item will have an optional mass (if not specified, it's 1 for the math), it will experience a repulsive force from the mouse location which varies with the inverse square of the distance to the mouse, and it will experience an attractive force to its "home" location. The attractive force can be constant, linear, or square with distance, depending on the desired effect.
thats a good theory, i may have to consult some physics students about this. if additional variables such as mass were taken into account it will definately be more to work with. Lastly however, can this be implemented as a single equation or will a fair amount programming based logic be needed as well?
EDIT: there may also be some physics frameworks that do this for me. do you know any lightweight MIT or GPL projects like this?
So here's a basic implementation of the system I was talking about. This generates 100 randomly spaced objects which will react to the mouse position and their original positions. You can play with the tuning parameters to adjust the size of the forces.