Sign in to follow this  
CodeCriminal

Zoom+Panning with Scale/Translate Transforms

Recommended Posts

Wow its been a while since ive come here to ask a question :P (atleast it feels like it)

So I am trying to create a zoom and panable makeshift viewport using an SFML shape (for now) the panning is fine its just the zooming..
My initial try to solve the problem was to translate to the position of the cursor (i.e. the place I wanted to zoom into) and do a directional scale of the shape so that so what ever is under the cursor will scale relatively uniformly and the whatever is around it will scale out from that position creating the illusion of a zoom-to-point mechanism. unfortunatly this resulted in some strange goings on. I believed this to be my forgetting to take into account the current position of the of the makeshift-shape-viewport which i was correct, but alas my solution was not (Position = Origin) while this went some way to solving my problem my because the zooming began to work as I had originally expected.. however it broke the panning functionality because each time i try to zoom the position of the box is set to "(0, 0)" making it seem as though the viewport is fixed in place when zooming (if you zoom on certain areas of the parent viewport (in this case a WinForms panel) it will snap the makeshift one back into its original place and continue to zoom normally.


Finally I decided that somehow I have to find the position prior to the transform (i.e the position in the previous coordinate system) and find that relative to the new frame of reference after the transformation has taken place.. I drew a little diagram of what a translation looks like and realised that I may be able to get this unknown by performing the inverse opertion on the origin and assigning that to the position.. this did not work to well.. so now I am stumped (or its possible that I did it incorrectly..)..



Please help :)



PS: Im going for something like what Gamemaker 8.1 has in its map editor (downloaded it the otherday because i needed to create some animations, then i found something else :P )

Share this post


Link to post
Share on other sites
[url="http://www.gamedev.net/topic/594055-zooming-onto-an-arbitrary-point/page__p__4765836"]see my post[/url]

And panning:
[font="Courier New"]
ViewCenter.x += MouseDX * CurrentZoom;
ViewCenter.y += MouseDY * CurrentZoom;[/font]

Where the Mouse delta values are in screen coordinates.
The zooming and panning is achieved through the projection matrix:

[font="Courier New"]glOrtho(
ViewCenter.x - width* CurrentZoom,
ViewCenter.x + width* CurrentZoom,
ViewCenter.y - height* CurrentZoom,
ViewCenter.y + height* CurrentZoom,-100,100);[/font]

Where width and height are the half sizes of the viewport.

I hope that helps.

Share this post


Link to post
Share on other sites
[quote name='szecs' timestamp='1313658041' post='4850675']
[url="http://www.gamedev.net/topic/594055-zooming-onto-an-arbitrary-point/page__p__4765836"]see my post[/url]

And panning:
[font="Courier New"]
ViewCenter.x += MouseDX * CurrentZoom;
ViewCenter.y += MouseDY * CurrentZoom;[/font]

Where the Mouse delta values are in screen coordinates.
The zooming and panning is achieved through the projection matrix:

[font="Courier New"]glOrtho(
ViewCenter.x - width* CurrentZoom,
ViewCenter.x + width* CurrentZoom,
ViewCenter.y - height* CurrentZoom,
ViewCenter.y + height* CurrentZoom,-100,100);[/font]

Where width and height are the half sizes of the viewport.

I hope that helps.
[/quote]


If you could forgive me, I am not quite as well endowed with mathematical talent such as yourself, though from the thread you linked I read through and I dont think we are suffering from the same problem exactly.
I should have posted this earlier but here it is:


C#/.Net/Winforms/SFML.Net
[code]void PanelView_MouseWheel(object sender, MouseEventArgs e)
{
float oldzoom = zoom;
float zoomfactor = (float)(e.Delta/120) * 0.1f;
zoom = Math.Max( 0.1f, Math.Min( 3.0f, zoom + zoomfactor ) );


SFML.Window.Vector2f origin = Map.Position;

Map.Origin = new SFML.Window.Vector2f( e.X, e.Y );
Map.Scale = new SFML.Window.Vector2f( zoom, zoom );


Render( );
}

void PanelView_MouseMove(object sender, MouseEventArgs e)
{
if ( e.Button != System.Windows.Forms.MouseButtons.Left || ModifierKeys != Keys.Control)
return ;

Map.Position = new SFML.Window.Vector2f (e.X - DragDiff.X, e.Y - DragDiff.Y);
Render( );
}

[/code]

What im trying to do is have the map (i.e. just a rectangle with a few gridlines across it) behave as though it were an object of the view which I am trying to zoom not the view itself, so i can zoom somewhere outside of the map area and it would (if position your trying to zoom to is far enough and the zoom level deep enough) go out of the actual viewing area the map is "a child of"

I will continue to study your post(s) and see if I can figure this thing out..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this