add Camera function

This commit is contained in:
faiface 2016-12-02 17:48:19 +01:00
parent 21733d0858
commit a1913661e7
1 changed files with 18 additions and 2 deletions

View File

@ -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. // Move adds delta to position.
func (t Transform) Move(delta Vec) Transform { func (t Transform) Move(delta Vec) Transform {
t.pos += delta 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 // Note, that subsequent calls to this method accumulate the final scale factor. Scaling two times by 2 is equivalent
// to scaling once by 4. // to scaling once by 4.
func (t Transform) Scale(scale float64) Transform { func (t Transform) Scale(scale float64) Transform {
t.sca *= V(scale, scale) t.sca = t.sca.Scaled(scale)
return t 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 // Note, that subsequent calls to this method accumulate the final scale factor. Scaling two times by 2 is equivalent
// to scaling once by 4. // to scaling once by 4.
func (t Transform) ScaleXY(scale Vec) Transform { 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 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()))) mat = mat.Mul3(mgl32.Translate2D(float32(t.anc.X()), float32(t.anc.Y())))
return mat 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)
}