C ++放大到屏幕的二维坐标的中心 [英] C++ Zoom into the centre of the screen in 2D coordinates

查看:304
本文介绍了C ++放大到屏幕的二维坐标的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有工作了正确的计算,以放大在2D画面的中心坐标困难,同时保持一切都在正确的比例。

I'm having difficulty working out the correct calculations in order to zoom into the centre of the screen in 2D coordinates whilst keeping everything in the correct scale.

我有我用来处理在我的地图编辑器移动如下向量:

I have a vector which I use to handle moving around my map editor as follows:

scroll = sf::Vector2<float>(-640.0f, -360.0f);

它设置为-640.0f,-360.0f使0,0屏幕上初始化中心(根据我的窗户是1280×720)。

It's set at -640.0f, -360.0f to make 0,0 the centre of the screen on initialising (based on my window being 1280x720).

从0.1F到2.0f我的缩放值的范围,它的增加或0.05增量下降:

My zoom value ranges from 0.1f to 2.0f and it's increased or decreased in 0.05 increments:

zoomScale = zoomScale + 0.05;

在使用下列code绘制到屏幕上的元素,他们被吸引:

When drawing elements on to the screen they are drawn using the following code:

sf::Rect<float> dRect;
dRect.left = (mapSeg[i]->position.x - scroll.x) * (layerScales[l] * zoomScale);
dRect.top = (mapSeg[i]->position.y - scroll.y) * (layerScales[l] * zoomScale);
dRect.width = (float)segDef[mapSeg[i]->segmentIndex]->width;
dRect.height = (float)segDef[mapSeg[i]->segmentIndex]->height;

sf::Sprite segSprite;
segSprite.setTexture(segDef[mapSeg[i]->segmentIndex]->tex);
segSprite.setPosition(dRect.left, dRect.top);
segSprite.setScale((layerScales[l] * zoomScale), (layerScales[l] * zoomScale));
segSprite.setOrigin(segDef[mapSeg[i]->segmentIndex]->width / 2, segDef[mapSeg[i]->segmentIndex]->height / 2);
segSprite.setRotation(mapSeg[i]->rotation);
Window.draw(segSprite);

layerScales是用来扩展段层视差滚动的值。

layerScales is a value used to scale up layers of segments for parallax scrolling.

这似乎放大和缩小时,工作正常,但中心点似乎转移(据我所知应该总是在0,0将设在不同的坐标元素,只要我缩放)。我用以下方法来计算一下位置在鼠标来测试这个如下:

This seems to work fine when zooming in and out but the centre point seems to shift (an element that I know should always be at 0,0 will be located at different co-ordinates as soon as I zoom). I use the following to calculate what the position as at the mouse to test this as follows:

mosPosX = ((float)input.mousePos.x + scroll.x) / zoomScale)
mosPosY = ((float)input.mousePos.y + scroll.y) / zoomScale)

我敢肯定有一个计算我应该做的滚动矢量考虑到这变焦,但我似乎无法得到它的工作的权利。

I'm sure there's a calculation I should be doing to the 'scroll' vector to take into account this zoom but I can't seem to get it to work right.

我尝试推行类似下面,但它并没有产生正确的结果:

I tried implementing something like below but it didn't produce the correct results:

scroll.x = (scroll.x - (SCREEN_WIDTH / 2)) * zoomScale - (scroll.x - (SCREEN_WIDTH / 2));
scroll.y = (scroll.y - (SCREEN_HEIGHT / 2)) * zoomScale - (scroll.y - (SCREEN_HEIGHT / 2));

任何想法,我做错了什么?

Any ideas what I'm doing wrong?

推荐答案

我会做这个简单的方法(不是最有效的,但能正常工作),并只对单一轴(第二个是相同的)

I will do this the easy way (not most efficient but works fine) and only for single axis (second is the same)

这是更好地抵消了缩放的:

it is better to have offset unscaled:

scaledpos = (unscaledpos*zoomscale)+scrolloffset

知道中心点比例改变后,应保持不动(0手段后,1是指前):

know center point should not move after scale change (0 means before 1 means after):

scaledpos0 == scaledpos1

这样做:

scaledpos0 = (midpointpos*zoomscale0)+scrolloffset0; // old scale
scaledpos1 = (midpointpos*zoomscale1)+scrolloffset0; // change zoom only
scrolloffset1+=scaledpos0-scaledpos1;                  // correct offset so midpoint stays where is ... i usualy use mouse coordinate instead of midpoint so i zoom where the mouse is 

当你不能改变的换算关系,然后只是做同样的与你的

when you can not change the scaling equation then just do the same with yours

scaledpos0 = (midpointpos+scrolloffset0)*zoomscale0;
scaledpos1 = (midpointpos+scrolloffset0)*zoomscale1;
scrolloffset1+=(scaledpos0-scaledpos1)/zoomscale1;

希望我没有任何愚蠢的错误在那里(从内存中写入)

Hope I did no silly error in there (writing from memory)

这篇关于C ++放大到屏幕的二维坐标的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆