camera

package module
v0.0.0-...-695003d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 2 Imported by: 6

README

ebiten-camera

A simple camera implementation based on vrld's hump for LÖVE

Usage

📖 Docs
Look at the examples to see how to use the library.

Here is a stripped-down version of the example code, highlighting the most important functions.

It won't run, so please don't bother trying it 😄


var (
    cam *ebitenCamera.Camera
    // other vars excluded, such as tiles, PlayerX etc
)


func main() {
    w,h := 640, 480
    // excluding normal ebiten setup
    cam = camera.NewCamera(w, h, 0, 0, 0, 1)
}

func (g *Game) Update() error {
    // Follows the player
    cam.SetPosition(PlayerX+float64(PlayerSize)/2, PlayerY+float64(PlayerSize)/2)
    return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
    // Clear camera surface
    cam.Surface.Clear()
    cam.Surface.Fill(color.RGBA{255, 128, 128, 255})
    // Draw tiles
    tileOps := &ebiten.DrawImageOptions{}
    cam.Surface.DrawImage(tiles, cam.GetTranslation(tileOps, 0, 0))
    // Draw the player
    playerOps := &ebiten.DrawImageOptions{}
    playerOps = cam.GetRotation(playerOps, PlayerRot, -float64(PlayerSize)/2, -float64(PlayerSize)/2)
    playerOps = cam.GetScale(playerOps, 0.5, 0.5)
    playerOps = cam.GetSkew(playerOps, 0, -0.5)
    playerOps = cam.GetTranslation(playerOps, PlayerX, PlayerY)
    cam.Surface.DrawImage(player, playerOps)
    
    // Draw to screen and zoom
    cam.Blit(screen)
}

Considerations

  1. When setting the *ebiten.DrawImageOptions in the ebitenCamera.Surface.DrawImage function, the order of operation is important!
    Rotate, Scale, Skew and then Translate!

  2. My example doesn't include a range for the camera's zoom to highlight what happens if you zoom too far in either direction. This is because I use a render texture to draw everything to, and I simply resize this when zooming in or out. This texture has a max size, causing the positioning logic to stop centering it. I'll probably change this at some point (unless you beat me to it with a PR, of course 😆)

Documentation

Overview

Package camera provides a simple camera system for use with ebiten

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Camera

type Camera struct {
	X, Y, Rot, Scale float64
	Width, Height    int
	Surface          *ebiten.Image
}

Camera can look at positions, zoom and rotate.

func NewCamera

func NewCamera(width, height int, x, y, rotation, zoom float64) *Camera

NewCamera returns a new Camera

func (*Camera) Blit

func (c *Camera) Blit(screen *ebiten.Image)

Blit draws the camera's surface to the screen and applies zoom

func (*Camera) GetCursorCoords

func (c *Camera) GetCursorCoords() (float64, float64)

GetCursorCoords converts cursor/screen coords into world coords

func (*Camera) GetRotation

func (c *Camera) GetRotation(ops *ebiten.DrawImageOptions, rot, originX, originY float64) *ebiten.DrawImageOptions

GetRotation alters the provided *ebiten.DrawImageOptions' rotation using the provided originX and originY args

func (*Camera) GetScale

func (c *Camera) GetScale(ops *ebiten.DrawImageOptions, scaleX, scaleY float64) *ebiten.DrawImageOptions

GetScale alters the provided *ebiten.DrawImageOptions' scale

func (*Camera) GetScreenCoords

func (c *Camera) GetScreenCoords(x, y float64) (float64, float64)

GetScreenCoords converts world coords into screen coords

func (*Camera) GetSkew

func (c *Camera) GetSkew(ops *ebiten.DrawImageOptions, skewX, skewY float64) *ebiten.DrawImageOptions

GetSkew alters the provided *ebiten.DrawImageOptions' skew

func (*Camera) GetTranslation

func (c *Camera) GetTranslation(ops *ebiten.DrawImageOptions, x, y float64) *ebiten.DrawImageOptions

GetTranslation alters the provided *ebiten.DrawImageOptions' translation based on the given x,y offset and the camera's position

func (*Camera) GetWorldCoords

func (c *Camera) GetWorldCoords(x, y float64) (float64, float64)

GetWorldCoords converts screen coords into world coords

func (*Camera) MovePosition

func (c *Camera) MovePosition(x, y float64) *Camera

MovePosition moves the Camera by x and y. Use SetPosition if you want to set the position

func (*Camera) Resize

func (c *Camera) Resize(w, h int) *Camera

Resize resizes the camera Surface

func (*Camera) Rotate

func (c *Camera) Rotate(phi float64) *Camera

Rotate rotates by phi

func (*Camera) SetPosition

func (c *Camera) SetPosition(x, y float64) *Camera

SetPosition looks at a position

func (*Camera) SetRotation

func (c *Camera) SetRotation(rot float64) *Camera

SetRotation sets the rotation to rot

func (*Camera) SetZoom

func (c *Camera) SetZoom(zoom float64) *Camera

SetZoom sets the zoom

func (*Camera) Zoom

func (c *Camera) Zoom(mul float64) *Camera

Zoom *= the current zoom

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL