ebitencp

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2024 License: MIT Imports: 6 Imported by: 12

README

ebiten-chipmunk

ebiten-chipmunk is an implementation of the cp.Drawer interface from jakecoffman/cp/v2. This implementation utilizes hajimehoshi/ebiten/v2, making it possible to run across multiple platforms.

demo

Usage

Within your Draw() method, invoke the cp.DrawSpace() function, passing in both a *cp.Space and a *ebitencp.Drawer as parameters.

type Game struct {
	space  *cp.Space
	drawer *ebitencp.Drawer
}
func main() {
    // ...
	game := &Game{}
	game.space = space
	game.drawer = ebitencp.NewDrawer(screenWidth, screenHeight)
    // ...
}
func (g *Game) Draw(screen *ebiten.Image) {
	// Drawing with Ebitengine/v2
	cp.DrawSpace(g.space, g.drawer.WithScreen(screen))
}

If you want to enable dragging, call the HandleMouseEvent() function within the Update method, passing the *cp.Space object. This will allow objects to be dragged using a mouse or touch device.

func (g *Game) Update() error {
	// Handling dragging
	g.drawer.HandleMouseEvent(g.space)
	g.space.Step(1 / 60.0)
	return nil
}

Below is a simple example that demonstrates the implementation in action.

package main

import (
	_ "image/png"
	"log"

	"github.com/demouth/ebitencp"
	"github.com/hajimehoshi/ebiten/v2"

	"github.com/jakecoffman/cp/v2"
)

const (
	screenWidth   = 640
	screenHeight  = 480
	hScreenWidth  = screenWidth / 2
	hScreenHeight = screenHeight / 2
)

type Game struct {
	space  *cp.Space
	drawer *ebitencp.Drawer
}

func (g *Game) Update() error {
	// Handling dragging
	g.drawer.HandleMouseEvent(g.space)
	g.space.Step(1 / 60.0)
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	// Drawing with Ebitengine/v2
	g.drawer.Screen = screen
	cp.DrawSpace(g.space, g.drawer)
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
	return screenWidth, screenHeight
}

func main() {
	// Initialising Chipmunk
	space := cp.NewSpace()
	space.SleepTimeThreshold = 0.5
	space.SetGravity(cp.Vector{X: 0, Y: -100})
	walls := []cp.Vector{
		{X: -hScreenWidth, Y: -hScreenHeight}, {X: -hScreenWidth, Y: hScreenHeight},
		{X: hScreenWidth, Y: -hScreenHeight}, {X: hScreenWidth, Y: hScreenHeight},
		{X: -hScreenWidth, Y: -hScreenHeight}, {X: hScreenWidth, Y: -hScreenHeight},
		{X: -hScreenWidth, Y: hScreenHeight}, {X: hScreenWidth, Y: hScreenHeight},
		{X: -100, Y: -100}, {X: 100, Y: -80},
	}
	for i := 0; i < len(walls)-1; i += 2 {
		shape := space.AddShape(cp.NewSegment(space.StaticBody, walls[i], walls[i+1], 0))
		shape.SetElasticity(0.5)
		shape.SetFriction(0.5)
	}
	addBall(space, 0, 0, 50)
	addBall(space, 0, 100, 20)

	// Initialising Ebitengine/v2
	game := &Game{}
	game.space = space
	game.drawer = ebitencp.NewDrawer(screenWidth, screenHeight)
	ebiten.SetWindowSize(screenWidth, screenHeight)
	ebiten.SetWindowTitle("ebiten-chipmunk - ball")
	if err := ebiten.RunGame(game); err != nil {
		log.Fatal(err)
	}
}

func addBall(space *cp.Space, x, y, radius float64) {
	mass := radius * radius / 100.0
	body := space.AddBody(cp.NewBody(mass, cp.MomentForCircle(mass, 0, radius, cp.Vector{})))
	body.SetPosition(cp.Vector{X: x, Y: y})
	shape := space.AddShape(cp.NewCircle(body, radius, cp.Vector{}))
	shape.SetElasticity(0.5)
	shape.SetFriction(0.5)
}

Additional examples can be found in the examples/ directory. These examples can help you adapt the implementation to your own projects.

Using Ebitengine

You can correct the coordinate system by setting FlipYAxis to true.

Example:

  func main() {
	// ...
  	game.drawer = ebitencp.NewDrawer(screenWidth, screenHeight)
+ 	game.drawer.FlipYAxis = true
+	// Set the camera offset to the center of the screen
+	drawer.GeoM.Translate(-screenWidth/2, -screenHeight/2)
	// ...
  }

Documentation

Index

Constants

View Source
const DrawPointLineScale = 1.0
View Source
const GRABBABLE_MASK_BIT uint = 1 << 31

Variables

This section is empty.

Functions

func ScreenToWorld added in v1.4.0

func ScreenToWorld(screenPoint cp.Vector, cameraGeoM ebiten.GeoM, camera Camera, flipYAxis bool, screenWidth, screenHeight int) cp.Vector

ScreenToWorld converts screen-space coordinates to world-space

Types

type Camera added in v1.3.0

type Camera struct {
	Offset cp.Vector
}

type Drawer

type Drawer struct {
	Screen       *ebiten.Image
	ScreenWidth  int
	ScreenHeight int
	StrokeWidth  float32
	FlipYAxis    bool
	// Drawing colors
	Theme *Theme
	// GeoM for drawing vertices. Useful for cameras.
	// Apply GeoM to shift the drawing.
	GeoM      *ebiten.GeoM
	OptStroke *ebiten.DrawTrianglesOptions
	OptFill   *ebiten.DrawTrianglesOptions

	// Deprecated: Use GeoM instead of Camera
	Camera Camera
	// Deprecated: Use OptStroke and OptFill instead of AntiAlias
	AntiAlias bool
	// contains filtered or unexported fields
}

func NewDrawer

func NewDrawer(screenWidth, screenHeight int) *Drawer

func (*Drawer) CollisionPointColor

func (d *Drawer) CollisionPointColor() cp.FColor

func (*Drawer) ConstraintColor

func (d *Drawer) ConstraintColor() cp.FColor

func (*Drawer) Data

func (d *Drawer) Data() interface{}

func (*Drawer) DrawCircle

func (d *Drawer) DrawCircle(pos cp.Vector, angle, radius float64, outline, fill cp.FColor, data interface{})

func (*Drawer) DrawDot

func (d *Drawer) DrawDot(size float64, pos cp.Vector, fill cp.FColor, data interface{})

func (*Drawer) DrawFatSegment

func (d *Drawer) DrawFatSegment(a, b cp.Vector, radius float64, outline, fill cp.FColor, data interface{})

func (*Drawer) DrawPolygon

func (d *Drawer) DrawPolygon(count int, verts []cp.Vector, radius float64, outline, fill cp.FColor, data interface{})

func (*Drawer) DrawSegment

func (d *Drawer) DrawSegment(a, b cp.Vector, fill cp.FColor, data interface{})

func (*Drawer) Flags

func (d *Drawer) Flags() uint

func (*Drawer) HandleMouseEvent

func (d *Drawer) HandleMouseEvent(space *cp.Space)

func (*Drawer) OutlineColor

func (d *Drawer) OutlineColor() cp.FColor

func (*Drawer) ShapeColor

func (d *Drawer) ShapeColor(shape *cp.Shape, data interface{}) cp.FColor

func (*Drawer) WithScreen added in v1.1.0

func (d *Drawer) WithScreen(screen *ebiten.Image) *Drawer

type Theme added in v1.5.0

type Theme struct {
	Outline                         color.RGBA
	Shape, ShapeSleeping, ShapeIdle color.RGBA
	Constraint, CollisionPoint      color.RGBA
}

func DefaultTheme added in v1.5.0

func DefaultTheme() *Theme

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳