A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [RESOLVED] Need an algorithm

  1. #1
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352

    resolved [RESOLVED] Need an algorithm

    This is something we've seen a lot:



    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.

    thank you

    maricudus
    Last edited by flosculus; 02-16-2011 at 04:54 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    This is NOT something I've seen a lot.

    I'm having trouble seeing the pattern in your descriptions. Could you make an animation showing it?

  3. #3
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    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.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  5. #5
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    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.

    EDIT:

    ok its happening here:
    http://www.templatemonster.com/flash...tes/29871.html
    and here
    http://www.templatemonster.com/flash...from=19&type=9

    they are much more modified versions with several restraints but the concept should be the same.
    Last edited by flosculus; 02-15-2011 at 07:46 AM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  7. #7
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    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?
    Last edited by flosculus; 02-16-2011 at 12:05 PM.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could set up Box2d to do it, but that's complete overkill.

    It shouldn't be hard to implement it yourself. The relevant equations are:
    f=ma
    where f is force, m is mass, and a is acceleration. Both f and a are vectors (have x and y components).

    f is the composite force which is made from both the repulsive force from the mouse and the attractive force to the home location.

    I'm going to try to create a very simple implementation for you. Give me a few minutes.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.
    Attached Files Attached Files

  10. #10
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    thats exactly what i wanted, thank you very much
    even the easing looks nice.

    i'll have to study the code to get a good idea of the maths.

    thanks again,

    flos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center