add more shapes examples

This commit is contained in:
unkl nik 2023-11-05 23:24:20 +02:00
parent 94df411e86
commit 1300701806
4 changed files with 551 additions and 0 deletions

View file

@ -0,0 +1,63 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(1280)
screenHeight := int32(720)
rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - bouncing ball")
ballPos := rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2)
ballSpeed := rl.NewVector2(5, 4)
ballRadius := 20
pause := false
frames := 0
rl.SetTargetFPS(60)
rl.SetMousePosition(0, 0)
for !rl.WindowShouldClose() {
if rl.IsKeyPressed(rl.KeySpace) {
pause = !pause
}
if !pause {
ballPos.X += ballSpeed.X
ballPos.Y += ballSpeed.Y
if ballPos.X >= float32(screenWidth)-float32(ballRadius) || ballPos.X <= float32(ballRadius) {
ballSpeed.X *= -1
}
if ballPos.Y >= float32(screenHeight)-float32(ballRadius) || ballPos.Y <= float32(ballRadius) {
ballSpeed.Y *= -1
}
} else {
frames++
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("SPACE key to pause", 10, 10, 20, rl.Black)
rl.DrawCircleV(ballPos, float32(ballRadius), rl.Red)
if pause && (frames/30)%2 == 0 {
rl.DrawText("PAUSED", 10, screenHeight-40, 30, rl.Black)
}
rl.DrawFPS(screenWidth-100, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,319 @@
// Package easings - Useful easing functions for values animation
//
// A port of Robert Penner's easing equations (http://robertpenner.com/easing/)
package main
import (
"math"
)
// Linear Easing functions
// LinearNone easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func LinearNone(t, b, c, d float32) float32 {
return c*t/d + b
}
// LinearIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func LinearIn(t, b, c, d float32) float32 {
return c*t/d + b
}
// LinearOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func LinearOut(t, b, c, d float32) float32 {
return c*t/d + b
}
// LinearInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func LinearInOut(t, b, c, d float32) float32 {
return c*t/d + b
}
// Sine Easing functions
// SineIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func SineIn(t, b, c, d float32) float32 {
return -c*float32(math.Cos(float64(t/d)*(math.Pi/2))) + c + b
}
// SineOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func SineOut(t, b, c, d float32) float32 {
return c*float32(math.Sin(float64(t/d)*(math.Pi/2))) + b
}
// SineInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func SineInOut(t, b, c, d float32) float32 {
return -c/2*(float32(math.Cos(math.Pi*float64(t/d)))-1) + b
}
// Circular Easing functions
// CircIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func CircIn(t, b, c, d float32) float32 {
t = t / d
return -c*(float32(math.Sqrt(float64(1-t*t)))-1) + b
}
// CircOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func CircOut(t, b, c, d float32) float32 {
return c*float32(math.Sqrt(1-float64((t/d-1)*t))) + b
}
// CircInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func CircInOut(t, b, c, d float32) float32 {
t = t / d * 2
if t < 1 {
return -c/2*(float32(math.Sqrt(float64(1-t*t)))-1) + b
}
t = t - 2
return c/2*(float32(math.Sqrt(1-float64(t*t)))+1) + b
}
// Cubic Easing functions
// CubicIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func CubicIn(t, b, c, d float32) float32 {
t = t / d
return c*t*t*t + b
}
// CubicOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func CubicOut(t, b, c, d float32) float32 {
t = t/d - 1
return c*(t*t*t+1) + b
}
// CubicInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func CubicInOut(t, b, c, d float32) float32 {
t = t / d * 2
if t < 1 {
return (c/2*t*t*t + b)
}
t = t - 2
return c/2*(t*t*t+2) + b
}
// Quadratic Easing functions
// QuadIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func QuadIn(t, b, c, d float32) float32 {
t = t / d
return c*t*t + b
}
// QuadOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func QuadOut(t, b, c, d float32) float32 {
t = t / d
return (-c*t*(t-2) + b)
}
// QuadInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func QuadInOut(t, b, c, d float32) float32 {
t = t / d * 2
if t < 1 {
return ((c / 2) * (t * t)) + b
}
return -c/2*((t-1)*(t-3)-1) + b
}
// Exponential Easing functions
// ExpoIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func ExpoIn(t, b, c, d float32) float32 {
if t == 0 {
return b
}
return (c*float32(math.Pow(2, 10*float64(t/d-1))) + b)
}
// ExpoOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func ExpoOut(t, b, c, d float32) float32 {
if t == d {
return (b + c)
}
return c*(-float32(math.Pow(2, -10*float64(t/d)))+1) + b
}
// ExpoInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func ExpoInOut(t, b, c, d float32) float32 {
if t == 0 {
return b
}
if t == d {
return (b + c)
}
t = t / d * 2
if t < 1 {
return (c/2*float32(math.Pow(2, 10*float64(t-1))) + b)
}
t = t - 1
return (c/2*(-float32(math.Pow(2, -10*float64(t)))+2) + b)
}
// Back Easing functions
// BackIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func BackIn(t, b, c, d float32) float32 {
s := float32(1.70158)
t = t / d
return c*t*t*((s+1)*t-s) + b
}
// BackOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func BackOut(t, b, c, d float32) float32 {
s := float32(1.70158)
t = t/d - 1
return c*(t*t*((s+1)*t+s)+1) + b
}
// BackInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func BackInOut(t, b, c, d float32) float32 {
s := float32(1.70158)
s = s * 1.525
t = t / d * 2
if t < 1 {
return c/2*(t*t*((s+1)*t-s)) + b
}
t = t - 2
return c/2*(t*t*((s+1)*t+s)+2) + b
}
// Bounce Easing functions
// BounceIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func BounceIn(t, b, c, d float32) float32 {
return (c - BounceOut(d-t, 0, c, d) + b)
}
// BounceOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func BounceOut(t, b, c, d float32) float32 {
t = t / d
if t < (1 / 2.75) {
return (c*(7.5625*t*t) + b)
} else if t < (2 / 2.75) {
t = t - (1.5 / 2.75)
return c*(7.5625*t*t+0.75) + b
} else if t < (2.5 / 2.75) {
t = t - (2.25 / 2.75)
return c*(7.5625*t*t+0.9375) + b
}
t = t - (2.625 / 2.75)
return c*(7.5625*t*t+0.984375) + b
}
// BounceInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func BounceInOut(t, b, c, d float32) float32 {
if t < d/2 {
return BounceIn(t*2, 0, c, d)*0.5 + b
}
return BounceOut(t*2-d, 0, c, d)*0.5 + c*0.5 + b
}
// Elastic Easing functions
// ElasticIn easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func ElasticIn(t, b, c, d float32) float32 {
if t == 0 {
return b
}
t = t / d
if t == 1 {
return b + c
}
p := d * 0.3
a := c
s := p / 4
postFix := a * float32(math.Pow(2, 10*float64(t-1)))
return -(postFix * float32(math.Sin(float64(t*d-s)*(2*math.Pi)/float64(p)))) + b
}
// ElasticOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func ElasticOut(t, b, c, d float32) float32 {
if t == 0 {
return b
}
t = t / d
if t == 1 {
return b + c
}
p := d * 0.3
a := c
s := p / 4
return a*float32(math.Pow(2, -10*float64(t)))*float32(math.Sin(float64(t*d-s)*(2*math.Pi)/float64(p))) + c + b
}
// ElasticInOut easing
// t: current time, b: begInnIng value, c: change In value, d: duration
func ElasticInOut(t, b, c, d float32) float32 {
if t == 0 {
return b
}
t = t / d * 2
if t == 2 {
return b + c
}
p := d * (0.3 * 1.5)
a := c
s := p / 4
if t < 1 {
t = t - 1
postFix := a * float32(math.Pow(2, 10*float64(t)))
return -0.5*(postFix*float32(math.Sin(float64(t*d-s)*(2*math.Pi)/float64(p)))) + b
}
t = t - 1
postFix := a * float32(math.Pow(2, -10*(float64(t))))
return postFix*float32(math.Sin(float64(t*d-s)*(2*math.Pi)/float64(p)))*0.5 + c + b
}

View file

@ -0,0 +1,85 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings ball anim")
ballPosX := -100
ballRadius := 20
ballAlpha := float32(0)
state := 0
frames := 0
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if state == 0 {
frames++
ballPosX = int(ElasticOut(float32(frames), -100, float32(screenWidth/2)+100, 100))
if frames >= 100 {
frames = 0
state = 1
}
} else if state == 1 {
frames++
ballRadius = int(ElasticIn(float32(frames), 20, 500, 150))
if frames >= 150 {
frames = 0
state = 2
}
} else if state == 2 {
frames++
ballAlpha = CubicOut(float32(frames), 0, 1, 150)
if frames >= 150 {
frames = 0
state = 3
}
} else if state == 3 {
if rl.IsKeyPressed(rl.KeyEnter) {
ballPosX = -100
ballRadius = 20
ballAlpha = 0
state = 0
}
}
if rl.IsKeyPressed(rl.KeyR) {
frames = 0
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
if state >= 2 {
rl.DrawRectangle(0, 0, screenWidth, screenHeight, rl.Green)
}
rl.DrawCircle(int32(ballPosX), 200, float32(ballRadius), rl.Fade(rl.Red, 1-ballAlpha))
if state == 3 {
textlen := rl.MeasureText("press ENTER to replay", 20)
rl.DrawText("press ENTER to replay", (screenWidth/2)-textlen/2, 200, 20, rl.Black)
}
rl.EndDrawing()
}
rl.CloseWindow()
}

View file

@ -0,0 +1,84 @@
package main
import (
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(1280)
screenHeight := int32(720)
rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes")
scleraLpos := rl.NewVector2(float32(screenWidth/2)-100, float32(screenHeight/2))
scleraRpos := rl.NewVector2(float32(screenWidth/2)+100, float32(screenHeight/2))
scleraRad := 80
irisLpos := rl.NewVector2(float32(screenWidth/2)-100, float32(screenHeight/2))
irisRpos := rl.NewVector2(float32(screenWidth/2)+100, float32(screenHeight/2))
irisRad := 24
angle := float32(0)
dx, dy, dxx, dyy := float32(0), float32(0), float32(0), float32(0)
rl.SetTargetFPS(60)
rl.SetMousePosition(0, 0)
for !rl.WindowShouldClose() {
irisLpos = rl.GetMousePosition()
irisRpos = rl.GetMousePosition()
if !rl.CheckCollisionPointCircle(irisLpos, scleraLpos, float32(scleraRad-20)) {
dx = irisLpos.X - scleraLpos.X
dy = irisLpos.Y - scleraLpos.Y
angle = float32(math.Atan2(float64(dy), float64(dx)))
dxx = (float32(scleraRad-irisRad) * float32(math.Cos(float64(angle))))
dyy = (float32(scleraRad-irisRad) * float32(math.Sin(float64(angle))))
irisLpos.X = scleraLpos.X + dxx
irisLpos.Y = scleraLpos.Y + dyy
}
if !rl.CheckCollisionPointCircle(irisRpos, scleraRpos, float32(scleraRad)-20) {
dx = irisRpos.X - scleraRpos.X
dy = irisRpos.Y - scleraRpos.Y
angle = float32(math.Atan2(float64(dy), float64(dx)))
dxx = (float32(scleraRad-irisRad) * float32(math.Cos(float64(angle))))
dyy = (float32(scleraRad-irisRad) * float32(math.Sin(float64(angle))))
irisRpos.X = scleraRpos.X + dxx
irisRpos.Y = scleraRpos.Y + dyy
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawCircleV(scleraLpos, float32(scleraRad), rl.LightGray)
rl.DrawCircleV(irisLpos, float32(irisRad), rl.Red)
rl.DrawCircleV(irisLpos, 10, rl.Black)
rl.DrawCircleV(scleraRpos, float32(scleraRad), rl.LightGray)
rl.DrawCircleV(irisRpos, float32(irisRad), rl.Orange)
rl.DrawCircleV(irisRpos, 10, rl.Black)
rl.DrawFPS(screenWidth-100, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}