Moving an object on a line

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Moving an object on a line

Post 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.
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post 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
tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post 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
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post 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
tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post 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
malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post 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
malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post 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
tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post 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
bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4219
Joined: Sun Jan 07, 2007 9:12 pm

Post 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
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10507
Joined: Wed May 06, 2009 2:28 pm

Post 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
Last edited by dunbarx on Thu Nov 05, 2009 4:58 pm, edited 9 times in total.
malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post 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
Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Post 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â€
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10507
Joined: Wed May 06, 2009 2:28 pm

Post 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.
tal
Posts: 84
Joined: Thu Jul 02, 2009 3:27 pm

Post 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
malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post 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
Post Reply