Page 1 of 2

Moving an object on a line

Posted: Tue Nov 03, 2009 5:08 pm
by tal
Hello,

I have to move an object (when I click on it) on a line, the object must only follow the points of the line (like the instruction "move the grc"A" to the points of grc "B" but not automatically...)

Thank you for your help.

Posted: Tue Nov 03, 2009 6:54 pm
by Klaus
Hi tal,

"line" graphics do not have the necessary "points" property for moving objects along it.
But you could use "the toppleft" and the "bottomright" of that graphic:

Code: Select all

on mouseup
  move btn "x, or whatever you need to move..." from the topleft of grc "the line" to the bottomright of grc "the line" in 2 secs
end mouseup
Hope that helps.


Best

Klaus

Posted: Wed Nov 04, 2009 11:32 am
by tal
Hi klaus,

Thanks but the object still move alone, i want to control his move with my mousedown and move it only on the line

Best regards

Posted: Wed Nov 04, 2009 12:16 pm
by Klaus
Hi tal,

oh, sorry, looks like I misunderstood what you want.
You want to move the object with the mouse but constrain the movement to the "line path"?

Hm, that is not so trivial :), will think it over.


Best

Klaus

Posted: Wed Nov 04, 2009 1:16 pm
by tal
yes it is that, i found this script :

Code: Select all

on mouseDown 
  put item 2 of the loc of me into myY 
  repeat until the mouse is up with messages 
    put the mouseH into myX 
    if myX > 9 and myX < 151 then 
      set the loc of me to the myX,myY 
    end if 
    wait 0 millisecs with messages 
  end repeat 
end mouseDown


but it only works with an horizontal line, my line have 4 segments which can be horizontal or vertical, in fact it is not so trivial i am trying....

Thanks

Posted: Wed Nov 04, 2009 3:32 pm
by malte
Do not like to blow my own horn too much, but TOOT TOOT

Have you looked at animationEngine yet?

AE has many different constrain properties that will let you do that. You could do:

set the constrainLinear of button "myButton" to 100,100,170,270 for example.

All the best,

Malte

Posted: Wed Nov 04, 2009 4:17 pm
by malte
Quick note to self (please ignore): First implementation for a polygon using AE

Code: Select all

on constrainLinearCallBack 
   local tDistToOriginal,tDistNext,tWhichPoint
   if item 3 to -1 of the constrainLinear of me=the loc of me then
      put lineOffset(item 3 to -1 of the constrainLinear of me,the points of grc 1) into tWhichPoint
      if tWhichPoint >= the number of lines of the points of grc 1 then exit constrainLinearCallBack
      put distance(closestPointOnLine(the constrainLinear of me,the mouseLoc),the mouseLoc) into tDistToOriginal
      put distance(closestPointOnLine(item 3 to -1 of the constrainLinear of me,line tWhichpoint +1 of the points of grc 1, the mouseLoc),the mouseLoc) into tDistNext
      put tDistToOriginal,tDistNext
      if tDistNext < tDistToOriginal then
         set the constrainLinear of me to item 3 to -1 of the constrainLinear of me,line tWhichPoint + 1 of the points of grc 1
      end if
   end if
      if item 1 to 2 of the constrainLinear of me=the loc of me then
      put lineOffset(item 1 to 2 of the constrainLinear of me,the points of grc 1) into tWhichPoint
      if tWhichPoint = 1 then exit constrainLinearCallBack
      put distance(closestPointOnLine(the constrainLinear of me,the mouseLoc),the mouseLoc) into tDistToOriginal
      put distance(closestPointOnLine(item 1 to 2 of the constrainLinear of me,line tWhichpoint -1 of the points of grc 1, the mouseLoc),the mouseLoc) into tDistNext
      put tDistToOriginal,tDistNext
      if tDistNext < tDistToOriginal then
         set the constrainLinear of me to line tWhichPoint - 1 of the points of grc 1,item 1 to 2 of the constrainLinear of me
      end if
   end if
end constrainLinearCallBack

Posted: Wed Nov 04, 2009 4:36 pm
by tal
Thank you, so I guess I have to download the trivial version of "Animation Engine" to test it... I will check you when it is done.

Thanks

Posted: Wed Nov 04, 2009 10:13 pm
by bn
Dunbarx,
thank you very much for this piece of code. I was looking for something like this every once so often and limped along with iterations. This is a lot more elegant. A little math can do wonders...

May I suggest the following variation

Code: Select all

local x1, y1, x2, y2, slope
on mouseDown
    put item 1 of line 1 of the points of grc 1 into x1 
      put item 2 of line 1 of the points of grc 1 into y1 
      put item 1 of line 2 of the points of grc 1 into x2 
      put item 2 of line 2 of the points of grc 1 into y2 
      
      put y2 - y1  into yOffset 
      put x2 - x1  into xOffset 
      put yoffset / xOffset into slope 
end mouseDown

on mouseMove 
   if the mouse is up then exit mouseMove
   if  the mouseH >= x1 and the mouseH <= x2  then set the loc of me to the mouseH & "," & (the mouseH - x1) * slope + y1 
end mouseMove
regards
Bernd

Posted: Wed Nov 04, 2009 10:24 pm
by dunbarx
This is better, It handles the direction you might draw the line, since the points depends on that.

Code: Select all

on mouseMove
   if the mouse is down then 
      put item 1 of line 1 of the points of grc 1 into x1
      put item 2 of line 1 of the points of grc 1 into y1
      put item 1 of line 2 of the points of grc 1 into x2
      put item 2 of line 2 of the points of grc 1 into y2
      
      put y2 - y1  into yDiff
      put x2 - x1  into xDiff
      if xDiff <> 0 then put yDiff / xDiff into slope
      
      switch 
         case x1 > x2 
            if  the mouseH < x1 and the mouseH > x2  then set the loc of me to the mouseH & "," & (the mouseH - x1) * slope + y1
            break
         case x1 < x2
            if  the mouseH > x1 and the mouseH < x2  then set the loc of me to the mouseH & "," & (the mouseH - x1) * slope + y1
            break
         case x1 = x2
            if  the mouseV > item 2 of the topleft of grc 1 and the mouseV < item 2 of the botRight  of grc 1
            then set the loc of me to x1 & "," & the mouseV
            break
      end switch
   end if
end mouseMove

Posted: Wed Nov 04, 2009 11:06 pm
by malte
Remeber to check for divide by zero when you calculate the slope. :)

2 sidenotes: xOffset and yOffset are reserved keywords in rev, so you might want to considder other var names. Also you might want to set the loc to the firs / last point of the grc, otherwise if you move the mouse real fast it will not reach its destination, but halt half way through.

2 cents worth,

Malte

Posted: Thu Nov 05, 2009 4:36 am
by Regulae
Just a thought:
An alternative to testing whether the mouse is down/up in the mouseMove would be have a variable, say moveOnLine, set to “yesâ€

Posted: Thu Nov 05, 2009 4:55 pm
by dunbarx
Good suggestions all. I modified the code above to lose reserved words and deal with vertical lines. The suggestion of Regulae is good, too.

Posted: Mon Nov 09, 2009 10:34 am
by tal
Hi every one,

Thank you for your suggestions, but the example work only for an horizontal line, if i have a line with several segments (vertical and horizontal) it doesn't work, if you have a clue to do that...

Thanks

Posted: Mon Nov 09, 2009 12:09 pm
by malte
Hey Tal,

I quickly made a revlet from the code using animationEngine I posted earlier:

http://derbrill.on-rev.com/ae3/testline.html

It still is a bit rough, however I am posting it anyway. Is something like this what you are after?

Cheers,

Malte