Skip to main content
Formatted the display. Please consult this page for more information: http://gamedev.stackexchange.com/editing-help
Source Link

Or for people using XNA when the system is flipped.
First

First calculate the mouse cursor in the world. Since the mouse position system in XNA is flipped subtract the mouse Y from the height of the screen.
var mouseinWorld = new Vector2((cursor.X * oldZoomLevel) - world.X, ((cursor.Y - screen.Height) * oldZoomLevel) + world.Y));
Now

var mouseinWorld = new Vector2(
    (cursor.X * oldZoomLevel) - world.X, 
    ((cursor.Y - screen.Height) * oldZoomLevel) + world.Y));

Now change your zoomlevel.
Next

Next is to calculate the new world location.
var newX = (cursor.X * newZoomLevel) - mouseinWorld.X; var newY = mouseinWorld.Y - ((cursor.Y - screen.Height) * newZoomLevel)
Now

var newX = (cursor.X * newZoomLevel) - mouseinWorld.X;
var newY = mouseinWorld.Y - ((cursor.Y - screen.Height) * newZoomLevel)

Now update the world location with the newly calculated coordinates.
world = new Vector2(newX, newY)

world = new Vector2(newX, newY)

Or for people using XNA when the system is flipped.
First calculate the mouse cursor in the world. Since the mouse position system in XNA is flipped subtract the mouse Y from the height of the screen.
var mouseinWorld = new Vector2((cursor.X * oldZoomLevel) - world.X, ((cursor.Y - screen.Height) * oldZoomLevel) + world.Y));
Now change your zoomlevel.
Next is to calculate the new world location.
var newX = (cursor.X * newZoomLevel) - mouseinWorld.X; var newY = mouseinWorld.Y - ((cursor.Y - screen.Height) * newZoomLevel)
Now update the world location with the newly calculated coordinates.
world = new Vector2(newX, newY)

Or for people using XNA when the system is flipped.

First calculate the mouse cursor in the world. Since the mouse position system in XNA is flipped subtract the mouse Y from the height of the screen.

var mouseinWorld = new Vector2(
    (cursor.X * oldZoomLevel) - world.X, 
    ((cursor.Y - screen.Height) * oldZoomLevel) + world.Y));

Now change your zoomlevel.

Next is to calculate the new world location.

var newX = (cursor.X * newZoomLevel) - mouseinWorld.X;
var newY = mouseinWorld.Y - ((cursor.Y - screen.Height) * newZoomLevel)

Now update the world location with the newly calculated coordinates.

world = new Vector2(newX, newY)
Source Link
Taxos
  • 21
  • 1

Or for people using XNA when the system is flipped.
First calculate the mouse cursor in the world. Since the mouse position system in XNA is flipped subtract the mouse Y from the height of the screen.
var mouseinWorld = new Vector2((cursor.X * oldZoomLevel) - world.X, ((cursor.Y - screen.Height) * oldZoomLevel) + world.Y));
Now change your zoomlevel.
Next is to calculate the new world location.
var newX = (cursor.X * newZoomLevel) - mouseinWorld.X; var newY = mouseinWorld.Y - ((cursor.Y - screen.Height) * newZoomLevel)
Now update the world location with the newly calculated coordinates.
world = new Vector2(newX, newY)