Image rotation with MouseStillDown not very smooth

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

Post Reply
macthorman
Posts: 7
Joined: Tue Nov 20, 2012 9:30 pm

Image rotation with MouseStillDown not very smooth

Post by macthorman » Thu Dec 20, 2012 3:21 pm

Hey guys,

I am trying to rotate an image "CannonBarrel.png" according to the position of the finger on the screen. I am therefore using the "on MouseStillDown" still down handler in combination with "mouseLoc". Everything works fine and the image is adjusting accordingly to the position of the finger. The big problem is that the movement is not very smooth. You can actually see the image "jumping" and not turning smoothly. If you move very fast, the on the screen it is even possible the the image jumps by more than 20 degrees.

I guess this is related to the "update rate" of the "on MouseStillDown" handler but I am not sure. Does anybody know how I can increase the smoothness of the image rotation?

This is the code I am using:

Code: Select all

on MouseStillDown
   put item 1 of the mouseLoc into lMouseUpX
   put item 2 of the mouseLoc into lMouseUpY
      
   put item 1 of the the location of the image "CannonBarrel.png" into lBowLocX
   put item 2 of the the location of the image "CannonBarrel.png" into lBowLocY

   
   put (lMouseUpX - lBowLocX) *-1 into lDiffX
   put (lMouseUpY - lBowLocY)  into lDiffY
   

   if lMouseUpX < lBowLocX then 
   
      put atan (lDiffY / lDiffX) *180 / pi into lAngle
      set the angle of image "CannonBarrel.png" to lAngle

   end if
   
end MouseStillDown



Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Image rotation with MouseStillDown not very smooth

Post by Klaus » Thu Dec 20, 2012 4:05 pm

Hi Mac,

1. welcome to the forum :D

2. "mousestilldown" is in fact not very effective, too much system "mousechecking" overhead!
The trick is to use "mousemove" with a "flag", see script.

Code: Select all

local mayRotate

on MouseDown
  put true into mayRotate     
end MouseDown

on mouseup
  put false into mayRotate
end mouseup

on mouserelease
  mouseup
end mouserelease

on mousemove x,y
  if mayRotate = false then
    exit mousemove
  end if
  do_rotate x,y
end mousemove

command do_rotate lMouseUpX,lMouseUpY       
     put item 1 of the loc of image 1 into lBowLocX
     put item 2 of the loc of image 1 into lBowLocY    
     put (lMouseUpX - lBowLocX) *-1 into lDiffX
     put (lMouseUpY - lBowLocY)  into lDiffY
     if lMouseUpX < lBowLocX then      
          put atan (lDiffY / lDiffX) *180 / pi into lAngle
          set the angle of image 1 to lAngle
     end if
end do_rotate
Hint: Do not use "THE" when addressing controls:
"img 1..." instead of "THE img 1..."

"THE" is for addressing custom properties.


Best

Klaus

macthorman
Posts: 7
Joined: Tue Nov 20, 2012 9:30 pm

Re: Image rotation with MouseStillDown not very smooth

Post by macthorman » Thu Dec 20, 2012 4:34 pm

Hey Klaus,

thank you very much for the hints, it rotates perfectly now and I removed the "THE". :D

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Image rotation with MouseStillDown not very smooth

Post by Klaus » Thu Dec 20, 2012 5:03 pm

Cool :D

Post Reply