Is there a simple equation for this?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Xero
Posts: 157
Joined: Sat Jun 23, 2018 2:22 pm

Re: Is there a simple equation for this?

Post by Xero » Tue Sep 27, 2022 2:55 pm

Are you still looking for the maths behind this?
I would suggest that you look at locating the start point and target point then working off the vector between the two that you can use to move along.

Code: Select all

put field "Speed" into tSpeed --where speed is in pixels per second--
repeat until tStartPoint = tEndPoint
put the location of --cell-- into tStartPoint
put the location of --target-- into tEndPoint
put 0.1 into tTime --this will redraw the screen every 1/10 sec or 10 times per second. 0.01 is 1/100th second, etc change as necessary--
put tSpeed*tTime into tDist
-- work out the angle between the two--
put atan((item 2 of tEndPoint-item2 of tStartPoint)/(item 1 of tEndPoint-item1 of tStartPoint)) into tAngle
-- move the cell to a point that is a distance along the vector
move --cell-- to round((item 1 of tStartpoint)+ tDist * cos(tAngle)),round((item 2 of tStartpoint)+ tDist * sin(tAngle))
wait for tTime seconds
end repeat
I haven't tested it, but if my maths is right, that should move it in whatever direction the target is.
You will just need a certain speed to move it (in a field on your card, and the right thing to call --cell-- (i.e. img "Cell" or grc "Cell") and --target--
Would also help if you had an out clause to make sure the function doesn't just run forever.
Have a play with it and see if that does what you want. If it doesn't I am sure it can be tweaked pretty easily. Like I said, I haven't tested it, so it could have some bugs, but theoretically it could work.
X

Post Reply