From a1913661e795ce3d2597824a30ac1eaa9aeeb8bd Mon Sep 17 00:00:00 2001 From: faiface <faiface@ksp.sk> Date: Fri, 2 Dec 2016 17:48:19 +0100 Subject: [PATCH] add Camera function --- transform.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/transform.go b/transform.go index 5e96fab..01b183b 100644 --- a/transform.go +++ b/transform.go @@ -27,6 +27,12 @@ func Position(position Vec) Transform { } } +// Position sets position. +func (t Transform) Position(position Vec) Transform { + t.pos = position + return t +} + // Move adds delta to position. func (t Transform) Move(delta Vec) Transform { t.pos += delta @@ -50,7 +56,7 @@ func (t Transform) MoveAnchor(delta Vec) Transform { // Note, that subsequent calls to this method accumulate the final scale factor. Scaling two times by 2 is equivalent // to scaling once by 4. func (t Transform) Scale(scale float64) Transform { - t.sca *= V(scale, scale) + t.sca = t.sca.Scaled(scale) return t } @@ -59,7 +65,7 @@ func (t Transform) Scale(scale float64) Transform { // Note, that subsequent calls to this method accumulate the final scale factor. Scaling two times by 2 is equivalent // to scaling once by 4. func (t Transform) ScaleXY(scale Vec) Transform { - t.sca *= scale + t.sca = V(t.sca.X()*scale.X(), t.sca.Y()*scale.Y()) return t } @@ -81,3 +87,13 @@ func (t Transform) Mat3() mgl32.Mat3 { mat = mat.Mul3(mgl32.Translate2D(float32(t.anc.X()), float32(t.anc.Y()))) return mat } + +// Camera is a convenience function, that returns a Transform that acts like a camera. +// Center is the position in the world coordinates, that will be projected onto the center of the screen. +// One unit in world coordinates will be projected onto zoom pixels. +// +// It is possible to apply additional rotations, scales and moves to the returned transform. +func Camera(center, zoom, screenSize Vec) Transform { + scale := screenSize * zoom / 2 + return Position(0).Anchor(center).ScaleXY(scale) +} -- GitLab