Sign in to follow this  
Followers 0
jvanegmond

Applied Maths

7 posts in this topic

I'm trying to get a label move to my mouse when i click in a -smooth- and orderly fashion. I also want it to not move more then 300 pixels at a time.

I have commented the code a lot so please look at this in SciTe. (Where the most comment is, is where the problems are.)

Opt("GUIOnEventMode", 1)

#include <GUIConstants.au3>

Global $AppTitle = StringTrimRight(@ScriptName,4), $Item[3]

$GUI = GUICreate($AppTitle, 800,640,-1,-1,$WS_POPUP)
GUISetBkColor(0)
GUISetOnEvent(-3, "Stop")

;Create the moving part
$Item[1] = 0;X
$Item[2] = 0;Y
$Item[0] = GUICtrlCreateLabel("Me", $Item[1], $Item[2], 20,20); ID naturallement
GUICtrlSetBkColor(-1,0xFFFFFF)

GUISetState()

While 1
    $Mouse = GUIGetCursorInfo()
    
    If $Mouse[2] Then
        #cs
        $Mouse[0];x mouse
        $Mouse[1];y mouse
        $Item[1];x moving part
        $Item[2];y moving part
        #ce
        
    ;Pythagoras makes a visit to check if there has not been traveling more then 200 pixels
        If Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200 Then
        ;Shorter down the movement
            
        ;( I think this is where it goes wrong )
            
            While Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200
            ; Thank AMD for fast PC's
                $Mouse[0] *= 0.9
                $Mouse[1] *= 0.9
            WEnd
        Else
        ;Just go on !
        EndIf
        
    ;Number of steps is only so high because it will look smooth
        $Steps = 20
    ; This is to stop the "Are we there yet" if-statement
        $CurrentSteps = 0
        
    ;Ofcourse we have to calculate where we're going in the form of 'step size' (and direction)
        $StepSizeX = (($Mouse[0] - $Item[1]) / $Steps)
        $StepSizeY = (($Mouse[1] - $Item[2]) / $Steps)
        
        While $CurrentSteps <> $Steps
        ;Visualize.
            $Item[1] += $StepSizeX
            $Item[2] += $StepSizeY
            GUICtrlSetPos($Item[0], $Item[1], $Item[2])
            Sleep(10)
            $CurrentSteps += 1
        WEnd
    EndIf
WEnd

Func Stop()
    Exit
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Your right about the problem area. You compare algorithm > 200 but decrease the values on each itteration. So you will never get out of the loop.

;Pythagoras makes a visit to check if there has not been traveling more then 200 pixels
        If Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200 Then
        ;Shorter down the movement
            
        ;( I think this is where it goes wrong )
            
            ;CHANGE: While Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) > 200
            While Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2) < 200
            ; Thank AMD for fast PC's
                $Mouse[0] *= 0.9
                $Mouse[1] *= 0.9
            WEnd
        Else
        ;Just go on !
        EndIf
Edited by Uten

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

[Edit] I first thought it made sence what you said. But just look at this:

If it's bigger then 50 Do:

While it's smaller then 50

make value lower and lower

end

EndIf

If you would fill in a value of 40, it skips the while and it's ok. If you get a value bigger then 50, say 60, it goes towards the while loop but it never runs it even once because the statement is false.. ??

Edited by Manadar

Share this post


Link to post
Share on other sites

[Edit] I first thought it made sence what you said. But just look at this:

If it's bigger then 50 Do:

While it's smaller then 50

make value lower and lower

end

EndIf

If you would fill in a value of 40, it skips the while and it's ok. If you get a value bigger then 50, say 60, it goes towards the while loop but it never runs it even once because the statement is false.. ??

hmm, looks like your right. Not my best day. Should have tested more, but then a gain that's not my job it's yours :D. Good catch though!

The loop should have been something like this:

Local $foo
            Do
            ; Thank AMD for fast PC's
                $Mouse[0] *= 0.9
                $Mouse[1] *= 0.9
        $foo = Round(Sqrt(($Mouse[0] - $Item[1])^2 + ($Mouse[1] - $Item[2])^2),0) + 1
        ConsoleWrite("Calculate: $foo:=" & $foo & ", $Mouse[0]:=" & $Mouse[0] & ", $Mouse[1]:=" & $Mouse[1] & @LF)
        if Round($Mouse[0],0) = 0 OR Round( $Mouse[1],0) = 0 then ExitLoop ;Escaping 
            Until  $foo < 200

As you can see the algorithm still is not perfect in the sense that it will fail if your label is fare down on the GUI and you do your click in the upper part of the GUI.

Maybe you could post the solution when you get it?

Share this post


Link to post
Share on other sites

Maybe you could post the solution when you get it?

Sure, i will do that. I still find it complicated though. The first time i tried this i wanted to do it with 4 labels at a time, but it was a pain in the ass trying to get those all to move smoothly at the same time, hehe.

Share this post


Link to post
Share on other sites

I have commented the code a lot so please look at this in SciTe. (Where the most comment is, is where the problems are.)

I don't know if you know this, but now you can use syntax coloring on the forum. Instead of [ code][ /code] you would use [ autoit][ /autoit] (without spaces of course) or if you are in full reply/post mode you could use the button that big_daddy came up with [A3] next to the code tags :D ... Just a FYI is all.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I don't know if you know this, but now you can use syntax coloring on the forum. Instead of [ code][ /code] you would use [ autoit][ /autoit] (without spaces of course) or if you are in full reply/post mode you could use the button that big_daddy came up with [A3] next to the code tags :D ... Just a FYI is all.

Wasn't there a problem when you tried to copy everything in that box into SciTe? I didn't use it for that reason.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0