https://github.com/demouth/ebiten-chipmunk
ebiten-chipmunk is an implementation of the cp.Drawer interface of jakecoffman/cp/v2 using Ebitengine.
https://github.com/demouth/ebiten-chipmunk
chipmunk2d ebitengine physics
Last synced: 3 months ago
JSON representation
ebiten-chipmunk is an implementation of the cp.Drawer interface of jakecoffman/cp/v2 using Ebitengine.
- Host: GitHub
- URL: https://github.com/demouth/ebiten-chipmunk
- Owner: demouth
- License: mit
- Created: 2024-08-10T01:15:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-20T00:13:26.000Z (over 1 year ago)
- Last Synced: 2024-09-20T06:48:20.995Z (over 1 year ago)
- Topics: chipmunk2d, ebitengine, physics
- Language: Go
- Homepage:
- Size: 334 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ebiten-chipmunk
**ebiten-chipmunk** is an implementation of the `cp.Drawer` interface from [jakecoffman/cp/v2](https://github.com/jakecoffman/cp). This implementation utilizes [hajimehoshi/ebiten/v2](https://github.com/hajimehoshi/ebiten), making it possible to run across multiple platforms.

## Usage
Within your `Draw()` method, invoke the `cp.DrawSpace()` function, passing in both a `*cp.Space` and a `*ebitencp.Drawer` as parameters.
```go
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.
```go
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.
```go
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/](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:
```diff go
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)
// ...
}
```