Volume control turn Knob

G

gmproduct

Guest
Does anyone know if anyone has created a code or way of making a volume control turn knob as apposed to a volume control slider?
Cheers
Graeme
 

samspade

Member
Does anyone know if anyone has created a code or way of making a volume control turn knob as apposed to a volume control slider?
Cheers
Graeme
How would you want to rotate it? In other words, what is the specific action a player would take to interact and rotate the knob?
 
G

gmproduct

Guest
hi samspade,
I need to rotate it by grabbing the volume knob sprite using mouse left press button then turning it from left to right in order to increase the volume, reversing would decrease the sound, much like you would do with a record player or radio volume control
have you any idea how I would go about this?
thanks for your reply
 

samspade

Member
hi samspade,
I need to rotate it by grabbing the volume knob sprite using mouse left press button then turning it from left to right in order to increase the volume, reversing would decrease the sound, much like you would do with a record player or radio volume control
have you any idea how I would go about this?
thanks for your reply
I haven't done this before, so no guarantees, but my first attempt would be to lock the angle difference between the mouse position and the object when the left button is pressed, and then update the button's angle based upon the updated angle from button to mouse as the mouse moves so that the difference remains the same.

The volume itself would then just be mapped from the max and min volume angles (0 - 360 assuming a full rotation, 180 - 0 assuming a half, etc) the same as you would a slider (so rotation angle, rather than position along a line).
 

SoapSud39

Member
You can try this code. I haven't tried it myself, but it should hopefully work.
GML:
//create
mouse_x_prev = 0;
mouse_click = false
volume = 50; //out of 100
    //note: a non-1 (eg 50/100) value at the start of the game will cause a pop
    //for this code I'm using 50, but you should use 100
image_angle = 0; //no need for this line, really, if 0
    //change depending on how your sprite is drawn or how you want it to point

//step
if point_collision(mouse_x, mouse_y, id, true, false) { //id should work, it would be the instance of the knob
    if (mouse_check_button_pressed(mb_left)) mouse_click = true;
}
if mouse_click {
    volume += (mouse_x - mouse_x_prev) / 20; //the smaller this number, the faster volume changes
    volume = clamp(volume, 0, 100); //you can also set a max volume
}
if (mouse_check_button_released(mb_left)) mouse_click = false;
mouse_x_prev = mouse_x;

audio_master_gain(volume / 100); //or out of max volume
image_angle = (50 - volume) * 2; //put a multiplier depending on how you want it to turn
        //with a times-2 multiplier, your angle will be between 100 and -100 degrees
    //since 50 is halfway, change as needed
    //recommended to set a variable for this value
For now, this will rotate the knob between -50 and 50 degrees if you draw your sprite pointing upward. You can change this by putting a multiplier on the "image_angle =" line. As I put in comment lines, you'll probably want to start your gain at 1 instead of 0.5 (50 / 100), but mess around with this code first and see how it suits your project.

edit: some code
edit: the "image_angle = volume - 50" will make it turn in an opposite direction, so that line will be your problem. I'll try to fix it soon but I'm not good at math.
edit: fixed it, hopefully
 
Last edited:
G

gmproduct

Guest
Hi Samspade and SoapSud39
Thanks for your help, I'll have a go with the code you suggested SoapSud39... I was looking at this Youtube video from GravityShift games:
GameMaker Tutorial - Sound and Sliders
I have used the code he teaches in this video before and it worked OK, I was thinking about using the slider and slider-knob within my project but making these items invisible and possibly controlling the slider-knob movement with the circular volume control knob that I want to control with a left/ right mouse pressed function... is this possible and have you or anyone any ideas on how I would go about doing this as I imagine there would have to be some code that would have to be changed and others that would have to be created, or would this make things more complicated?
 

samspade

Member
Hi Samspade and SoapSud39
Thanks for your help, I'll have a go with the code you suggested SoapSud39... I was looking at this Youtube video from GravityShift games:
GameMaker Tutorial - Sound and Sliders
I have used the code he teaches in this video before and it worked OK, I was thinking about using the slider and slider-knob within my project but making these items invisible and possibly controlling the slider-knob movement with the circular volume control knob that I want to control with a left/ right mouse pressed function... is this possible and have you or anyone any ideas on how I would go about doing this as I imagine there would have to be some code that would have to be changed and others that would have to be created, or would this make things more complicated?
Yes, I would use the method I suggested in my first post for this - except I wouldn't have an invisible slider, I would do it all in the circular control knob and that if you wanted to do it with the mouse buttons, rather than moving the mouse, you would adjust the angle with those instead of calculating the angle of movement.
 
G

gmproduct

Guest
Hi SamSpade
I tried your method but had to change a couple of lines in the code to get it to work:
//step
if collision_point(mouse_x, mouse_y, obj_volumeknob, true, false) { //id should work, it would be the instance of the knob
changed (point_collision to collision_point) first code wouldn't work possibly because I have Gamemaker Studio 2.3 and a lot of the way code is written has been changed. also had to use (obj_volumeknob) as (id) wouldn't work.
volume += (mouse_x - mouse_x_prev) / 2; //the smaller this number, the faster volume changes..... had to lower number to 2 as the higher number was just too high to hear any volume change.
this method seems to work but it is difficult and awkward to turn the obj_volumeknob... (in order to move the volumeknob you have to left press on the knob and move you're mouse horizontally left to decrease volume and horizontally right to increase it) I tried it before using (mouse event: left_down, right_down) instead of (mouse_check_button_pressed) and the control knob rotated quite fast and smoothly but that was when I used this in order to get the volume control knob to move the horizontal (sliderknob) the code I had used for that was:
Mouse Left Down Event - this was for rotating the obj_volumesliderknob one way had to use Mouse Right Down Event to reverse the rotation
{
image_angle += degrees;
}
with (obj_sliderknob)
{
x -=1;
}
I can't seem to find any code in order to use the (mouse_down) event , have you any ideas or is there a better way to make the rotation of the obj_volumeknob a bit more smoother and realistic
thanks again anyway for this help as it is making me get nearer to the goal of having a volume turn knob as apposed to the horizontal slider volume control one... much appreciated
 
Top