Moving an object on a line
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Moving an object on a line
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.
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.
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:
Hope that helps.
Best
Klaus
"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
Best
Klaus
yes it is that, i found this script :
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
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
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
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
regards
Bernd
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
Bernd
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.
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

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