# Stop 3D objects moving when windows is resized [XNA/WinForms/C#]

This topic is 1382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have an XNA windows inside a a windows form, and I am running into a problem when I resize the window.

The behaviour I want is for the rendered cube to not move at all. So if there was a second cube offscreen to the right, and the user resized the window to the right, then the second cube would come into view. So basically resizing the window has no effect on the rendered elements at all.

But when I resize the window, for example drag it right, the cube also moves to the right

And when I drag the window down the cube zooms in slowly.

Here is my draw code for each quad of the cube:

GraphicsDevice dev = Core.Device;

dev.BlendState = BlendState.Opaque;
dev.DepthStencilState = DepthStencilState.Default;

dev.SetVertexBuffer(_vb);
dev.Indices = _ib;

effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 150), Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, dev.Viewport.AspectRatio, 0.1f, 10000.0f);
effect.World = Matrix.Identity;

effect.EnableDefaultLighting();
effect.Texture = Texture;

effect.CurrentTechnique.Passes[0].Apply();
dev.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);


I am using the winforms/xna sample from the xna website. When the window is resized the device is reset and the backbuffer width and height get set to the xna windows width and height.

This means the Viewport.AspectRatio gets updated and the 3d cubes do not get stretched. Now I just need their position to not change.

Does anyone know what else I could try?

Edited by Akron

##### Share on other sites

The resizing, etc., appears correct, and the changes of screen position reflect your setup. That is, you create a view matrix which defines what world position/direction appears in the center of the window, whatever it's aspect ratio. Commonly, resizing a window is for the user's convenience, not the programmer's.

The description of your desired behavior is very unusual and may not turn out the way you think it should. It's possible and a bit complicated but, because you're using a perspective view, you'll have to pick just one object to stay stationary. Other objects will change screen position when the window is resized.

What you would have to do:

1. When the resize procedure is called, pick one object to stay stationary, and calculate it's screen position.

2. Before you create the view matrix, adjust the camera position, or the camera view direction, or both, such that the selected object's world position converts to the same rendered screen position as it had prior to the resize.

Can you describe a bit better why the user changing window size is such a problem?

##### Share on other sites
Not sure how XNA windows work, but I'd say you have to handle the corresponding windows messages. Like WM_SIZE, which could trigger some sort of pause (used in your main loop).

##### Share on other sites

Not sure how XNA windows work, but I'd say you have to handle the corresponding windows messages. Like WM_SIZE, which could trigger some sort of pause (used in your main loop).

XNA is a higher level framework that abstracts many things away from you. Setting PreferredBackBufferWidth and PreferredBackBufferHeight on the GDM followed by ApplyChanges should be sufficient to resize the game window. I did notice weird behaviour though if you just drag the window border with the mouse.

IDK how XNA handles this stuff internally, but in the end I resorted to disable user resizing and only allow resolution change through the game menu

I did find a small sample on MSDN though on handling resizing: https://msdn.microsoft.com/en-us/library/bb203876.aspx

##### Share on other sites

I remember when I used to work on XNA that I also gave up with trying to have dynamic resizing. I've sinced moved onto SharpDX which has allowed me to customize resizing the way I want. I recommend learning the raw API's once you feel comfortable enough with XNA, they're not as scary to use as it seems.

##### Share on other sites

I remember when I used to work on XNA that I also gave up with trying to have dynamic resizing. I've sinced moved onto SharpDX which has allowed me to customize resizing the way I want. I recommend learning the raw API's once you feel comfortable enough with XNA, they're not as scary to use as it seems.

I second that. I also started with XNA, but when it's limitations got in my way more and more I changed to SharpDX and sticked with it

But now, lets get back on topic :)

##### Share on other sites

I've seen some projects that use the Control.Handle property to get the window parameters needed to resize the window. Don't remember off hand exactly how it is done, but it renders the viewport on every frame while you are still resizing the window. Typically, just using the XNA framework, the viewport isn't updated until you release the mouse button, and you just see a stretched image of the last buffer output before resizing.

1. 1
2. 2
3. 3
Rutin
18
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013216
×