So I'm trying to create a zoom function for my 2D camera.
The best result I've got so far is:
But something still feels off, and I just can't seem to pinpoint it.
My code currently, which simply takes the substraction between before and after zoom:
void Zoom(float value, float mx, float my, float w, float h) { float bmwx = (w - (w - mx)) * this->zoom, bmwy = (h - (h - my)) * this->zoom; // before if (value < 0.0f) this->zoom *= 0.9f; if (value > 0.0f) this->zoom *= 1.1f; float amwx = (w - (w - mx)) * this->zoom, amwy = (h - (h - my)) * this->zoom; // after this->center.x += amwx - bmwx; this->center.y += amwy - bmwy; } My other function, for matrix construction:
void Update(float mx, float my, float w, float h) { float hw = w * 0.5f, hh = h * 0.5f; float left = -hw + this->center.x; float right = hw + this->center.x; float top = -hh + this->center.y; float bottom = hh + this->center.y; this->ortho.InitOrthographic(drx::gfx::ogl::n, drx::gfx::ogl::f, left, right, top, bottom); this->mZoom.LoadIdentity(); this->mZoom.Scale(this->zoom, this->zoom, this->zoom); this->matrix = this->ortho * this->mZoom; } Update, to current code (Update: which also fails if I move the central point):
void Zoom(float value, float mx, float my, float w, float h) { this->os.x += mx * this->zoom; this->os.y += my * this->zoom; if (value < 0.0f) this->zoom *= 0.75f; if (value > 0.0f) this->zoom *= 1.25f; this->os.x -= mx * this->zoom; this->os.y -= my * this->zoom; } void Update(float mx, float my, float w, float h) { float hw = w * 0.5f, hh = h * 0.5f; float left = -hw + this->center.x; float right = hw + this->center.x; float top = -hh + this->center.y; float bottom = hh + this->center.y; this->ortho.InitOrthographic(drx::gfx::ogl::n, drx::gfx::ogl::f, left, right, top, bottom); this->mZoom.LoadIdentity(); this->mZoom.Scale(this->zoom, this->zoom, this->zoom); this->mZoom.Translate(this->os.x, this->os.y, 0.0f); this->matrix = this->ortho * this->mZoom; } 


(w - (w - mx)), w is subtracted from itself so this could be replaced with justmxthe same thing happens four times in your calculation. \$\endgroup\$center, I added a translation to the matrix calculation for offset. \$\endgroup\$