vector - C++ Zoom into the centre of the screen in 2D coordinates -
i'm having difficulty working out correct calculations in order zoom centre of screen in 2d coordinates whilst keeping in correct scale.
i have vector use handle moving around map editor follows:
scroll = sf::vector2<float>(-640.0f, -360.0f);
it's set @ -640.0f, -360.0f make 0,0 centre of screen on initialising (based on window being 1280x720).
my zoom value ranges 0.1f 2.0f , it's increased or decreased in 0.05 increments:
zoomscale = zoomscale + 0.05;
when drawing elements on screen drawn using 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 value used scale layers of segments parallax scrolling.
this seems work fine when zooming in , out centre point seems shift (an element know should @ 0,0 located @ different co-ordinates zoom). use following calculate position @ mouse test follows:
mosposx = ((float)input.mousepos.x + scroll.x) / zoomscale) mosposy = ((float)input.mousepos.y + scroll.y) / zoomscale)
i'm sure there's calculation should doing 'scroll' vector take account zoom can't seem work right.
i tried implementing below didn't produce 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 i'm doing wrong?
i easy way (not efficient works fine) , single axis (second same)
it better have offset unscaled:
scaledpos = (unscaledpos*zoomscale)+scrolloffset
know center point should not move after scale change (0 means before 1 means after):
scaledpos0 == scaledpos1
so this:
scaledpos0 = (midpointpos*zoomscale0)+scrolloffset0; // old scale scaledpos1 = (midpointpos*zoomscale1)+scrolloffset0; // change zoom scrolloffset1+=scaledpos0-scaledpos1; // correct offset midpoint stays ... usualy use mouse coordinate instead of midpoint zoom mouse
when can not change scaling equation same yours
scaledpos0 = (midpointpos+scrolloffset0)*zoomscale0; scaledpos1 = (midpointpos+scrolloffset0)*zoomscale1; scrolloffset1+=(scaledpos0-scaledpos1)/zoomscale1;
hope did no silly error in there (writing memory)
Comments
Post a Comment