Jump to content


Photo

Zoom view to mouse position

gm:studio

Best Answer flyingsaucerinvasion, 28 March 2016 - 07:39 PM

That seems convoluted to me.

 

Try this and let me know if it works for you

if (mouse_wheel_up() or mouse_wheel_down())
{
    //note zoom is not linear nor clamped in this example
    zoom *= (mouse_wheel_up() * 0.8 + mouse_wheel_down() * 1.25);
    
    view_wview[0] = view_wport[0] * zoom;
    view_hview[0] = view_hport[0] * zoom;

    //this asumes the view port takes up the whole window.    
    view_xview[0] = mouse_x - view_wview[0] * window_mouse_get_x() / window_get_width();
    view_yview[0] = mouse_y - view_hview[0] * window_mouse_get_y() / window_get_height();
}
Go to the full post


  • Please log in to reply
4 replies to this topic

#1 kingsushi001

kingsushi001

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM:Studio

Posted 28 March 2016 - 11:48 AM

Hello everyone

 

For some reason I just can't get my head around this. I'm trying to zoom

a view to the mouse position. I use some view panning code I got from HERE.

 

So here's my obj_camera code

 

Create

//pan vars
mx_previous=window_mouse_get_x();
my_previous=window_mouse_get_y();

//zoom vars
zoom=2;
view_wview[0]=room_width*zoom;
view_hview[0]=room_height*zoom;

End Step

//pan
var mx=window_mouse_get_x()*(view_wview[0]/view_wport[0]);
var my=window_mouse_get_y()*(view_hview[0]/view_hport[0]);

if (mouse_check_button(mb_middle))
{
    view_xview[0]=clamp(view_xview[0]+(mx_previous-mx),-400,(7500-view_wview[0])+400);
    view_yview[0]=clamp(view_yview[0]+(my_previous-my),-400,(7500-view_hview[0])+400);
}

mx_previous=mx;
my_previous=my;

//zoom
if (mouse_wheel_up() && zoom>1)
{
    zoom-=0.1;
    view_xview[0]=clamp(view_xview[0]+(mx_previous-mx),-400,(7500-view_wview[0])+400);
    view_yview[0]=clamp(view_yview[0]+(my_previous-my),-400,(7500-view_hview[0])+400);
}

if (mouse_wheel_down() && zoom<4)
{
    zoom+=0.1
    view_xview[0]=clamp(view_xview[0]+(mx_previous-mx),-400,(7500-view_wview[0])+400);
    view_yview[0]=clamp(view_yview[0]+(my_previous-my),-400,(7500-view_hview[0])+400);
}

view_wview[0]=room_width*zoom;
view_hview[0]=room_height*zoom;

Any help on this would be greatly appreciated


  • 0

sig.jpg


#2 patted03

patted03

    GMC Member

  • GMC Member
  • 247 posts
  • Version:GM:Studio

Posted 28 March 2016 - 12:46 PM

What do you want? Does the code work and you want an explanation of why? Does it not work and your'e looking for where it might be wrong? If its the latter, maybe explain what the issue is (nothing happens/zooms away from mouse/makes the room pink)


  • 0

#3 kingsushi001

kingsushi001

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM:Studio

Posted 28 March 2016 - 01:33 PM

For some reason I just can't get my head around this. I'm trying to zoom

a view to the mouse position. I use some view panning code I got from HERE.

 

I know how and why the code works.

The code works fine for zooming and panning,

except that it always zooms to and from the top left corner (0,0).

 

I know why this is happening, the view_xview and view_yview

needs to be adjusted, while clamped, I'm just not sure how to

go about it.

 

 

if (mouse_wheel_up() && zoom>1)
{
    zoom-=0.1;
    view_xview[0]=clamp(view_xview[0]+(mx_previous-mx),-400,(7500-view_wview[0])+400);
    view_yview[0]=clamp(view_yview[0]+(my_previous-my),-400,(7500-view_hview[0])+400);

}

if (mouse_wheel_down() && zoom<4)
{
    zoom+=0.1
    view_xview[0]=clamp(view_xview[0]+(mx_previous-mx),-400,(7500-view_wview[0])+400);
    view_yview[0]=clamp(view_yview[0]+(my_previous-my),-400,(7500-view_hview[0])+400);

}

 

 

I know the bold parts needs to be changed, I just don't know to what


  • 0

sig.jpg


#4 flyingsaucerinvasion

flyingsaucerinvasion

    GMC Member

  • GMC Member
  • 720 posts
  • Version:GM:Studio

Posted 28 March 2016 - 07:39 PM   Best Answer

That seems convoluted to me.

 

Try this and let me know if it works for you

if (mouse_wheel_up() or mouse_wheel_down())
{
    //note zoom is not linear nor clamped in this example
    zoom *= (mouse_wheel_up() * 0.8 + mouse_wheel_down() * 1.25);
    
    view_wview[0] = view_wport[0] * zoom;
    view_hview[0] = view_hport[0] * zoom;

    //this asumes the view port takes up the whole window.    
    view_xview[0] = mouse_x - view_wview[0] * window_mouse_get_x() / window_get_width();
    view_yview[0] = mouse_y - view_hview[0] * window_mouse_get_y() / window_get_height();
}

Edited by flyingsaucerinvasion, 28 March 2016 - 08:11 PM.

  • 1

#5 kingsushi001

kingsushi001

    GMC Member

  • GMC Member
  • 130 posts
  • Version:GM:Studio

Posted 28 March 2016 - 08:18 PM

Thank you so much! This worked perfectly.

 

This was the part I was having trouble with

{  
    view_xview[0] = mouse_x - view_wview[0] * window_mouse_get_x() / window_get_width();
    view_yview[0] = mouse_y - view_hview[0] * window_mouse_get_y() / window_get_height();
}

  • 0

sig.jpg






Also tagged with one or more of these keywords: gm:studio