Volume knob - how to constrain rotation?
sawkastee
Member Posts: 184
I am trying to constrain the rotation of this volume knob so that it does not spin around in a complete circle. Does anyone know how to accomplish this? I tried a constrain self.rotation to min(90,max(1, self.Rotation )) but that didn't work. I have attached a file of the volume knob.
https://www.dropbox.com/s/lrv492kbhfqf4ad/volume.zip
Thanks
https://www.dropbox.com/s/lrv492kbhfqf4ad/volume.zip
Thanks
Comments
Rule is when touch is pressed behavior "rotate to angle"
vectorToAngle( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y )
Works well but 2 issues:
1. the volume knob spins all the way around in a circle, how do I constrain it to a min/max?
2. when touch is pressed the volume knob jerks into position. i do i rid the jerky movement?
For starters, you should make your graphic point to 0 degrees. In GameSalad, that means pointing it directly to the right ------>. (I think the indicator dot on your graphic is pointing to about 220 degrees or so.)
Next you need to keep track of three attributes. For kicks lets call them oldAngle, newAngle, and changeInAngle. They all deal with the angle of the mouse from the center of the volume knob.
The changInAngle attribute is the critical thing. It is updated continuously and is the difference between the oldAngle and the newAngle.
Basically you gotta compare the old angle against the new angle and update the volume knob's rotation with this slight change in angles. Its looks smooth when using constrains.
The big glitch is what to do with the boundary between 0 and 360. As you move the mouse around, eventually you go full circle and come to the boundary between 0 and 360. The actor's rotation snaps back and forth if it isn't dealt with. Fortunately there is a quick math formula that fixes it.
Here is what the behaviors look like to get a volume knob to be limited between 0 and 90 degrees.
When touch
-- Change Attribute: self.oldAngle To: vectorToAngle(game.Touches.Touch 1.X-self.Position.X,game.Touches.Touch 1.Y-self.Position.Y)
-- Constrain Attribute: self.newAngle To: vectorToAngle(game.Touches.Touch 1.X-self.Position.X,game.Touches.Touch 1.Y-self.Position.Y)
-- Constrain Attribute: self.changInAngles To: ((( self.newAngle - self.oldAngle )+180)%360)-180
-- Constrain Attribute: self.Rotation To: max(0,min(90,self.Rotation+self.changeInAngles ))
-- Constrain Attribute: self.oldAngle To: self.newAngle
Edit: Here is an example file. It shows both 360 degree rotation and limited (0-90) rotation.
http://www.mediafire.com/download/8xme08ihsbew0j8/knobRotation.zip