-
-
Save BjoernSchilberg/f9f0200e1e779216b89429bb70cdcb9f to your computer and use it in GitHub Desktop.
Ebiten with Leap Motion written in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"image/color" | |
"math" | |
"github.com/hajimehoshi/ebiten" | |
"github.com/hajimehoshi/ebiten/ebitenutil" | |
"github.com/jaxi/motion" | |
) | |
var x float64 | |
var y float64 | |
var blkSize = 16 | |
var d *motion.Device | |
func update(screen *ebiten.Image) error { | |
// 先幫畫布填上 #FF0000 顏色 | |
screen.Fill(color.NRGBA{0xff, 0x00, 0x00, 0xff}) | |
// 顯示一串除錯用的文字 | |
ebitenutil.DebugPrint(screen, "Leap Motion with Ebiten!") | |
// 顯示方塊的 X 座標 | |
ebitenutil.DebugPrint(screen, "\n"+fmt.Sprintf("X: %d", int64(x))) | |
// 顯示方塊的 Y 座標 | |
ebitenutil.DebugPrint(screen, "\n\n"+fmt.Sprintf("Y: %d", int64(y))) | |
// 顯示方塊的大小 | |
ebitenutil.DebugPrint(screen, "\n\n\n"+fmt.Sprintf("Size: %d", int64(blkSize))) | |
// 建立一個 blkSize 尺寸的新畫布,我們稍後會由 Leap Motion 控制其大小 | |
square, _ := ebiten.NewImage(blkSize, blkSize, ebiten.FilterNearest) | |
// 替 square 畫布填上白色 | |
square.Fill(color.White) | |
// 建立一個空白選項建構體 | |
opts := &ebiten.DrawImageOptions{} | |
// 變形選項,也就是方塊的 X, Y 位置, | |
// 還有,Y 軸會基於方塊大小偏移 | |
opts.GeoM.Translate(x, y-float64(blkSize)) | |
// 渲染 square 畫布到 screen 主畫布上 | |
screen.DrawImage(square, opts) | |
return nil | |
} | |
func leapMotion() { | |
// Leap Motion 座標偏移 | |
var xOffset float64 = 150 | |
var yOffset float64 = 50 | |
var zOffset float64 = 150 | |
// 方塊最小的尺寸 | |
var minBlkSize = 16 | |
// 建立新的 Leap Motion 裝置 | |
d, _ = motion.NewDevice() | |
// 建立與 Leap Motion 的連線 | |
fmt.Println("You have been connected to Leap Motion successfully!") | |
d.ListenAndReceive(true) | |
// 新增另一個 Goroutine 來監聽 Leap Motion 的手掌座標並更改方塊位置 | |
go func() { | |
for frame := range d.FrameQueue { | |
// 取得來自 Leap Motion 的手掌座標 | |
leapX, leapY, leapZ := float64(frame.Hands[0].PalmPosition[0]), | |
float64(frame.Hands[0].PalmPosition[1]), | |
float64(frame.Hands[0].PalmPosition[2]) | |
leapZ = leapZ - zOffset | |
initBlkSize := int(leapZ) | |
if initBlkSize < 0 { | |
initBlkSize = int(math.Abs(float64(initBlkSize))) | |
} else if initBlkSize > 0 { | |
initBlkSize = initBlkSize * -1 | |
} | |
if initBlkSize < minBlkSize { | |
initBlkSize = minBlkSize | |
} | |
blkSize = initBlkSize | |
initX := leapX - xOffset | |
initY := leapY - yOffset | |
x = 400 + initX - float64(blkSize) | |
y = 400 - initY - float64(blkSize) | |
// 除錯用 | |
fmt.Println("---") | |
fmt.Printf("Leap X: %.6f\n", leapX) | |
fmt.Printf("Leap Y: %.6f\n", leapY) | |
fmt.Printf("Leap Z: %.6f\n", leapZ) | |
} | |
}() | |
} | |
func main() { | |
leapMotion() | |
defer d.Close() | |
ebiten.Run(update, 400, 400, 2, "Hello world!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment