step
This commit is contained in:
parent
0bec81c656
commit
fe6d2c0ed3
190 changed files with 104835 additions and 5 deletions
513
examples/physics/box2d/main.go
Normal file
513
examples/physics/box2d/main.go
Normal file
|
@ -0,0 +1,513 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/gen2brain/raylib-go/raylib"
|
||||
|
||||
box2d "github.com/neguse/go-box2d-lite/box2dlite"
|
||||
)
|
||||
|
||||
// Game type
|
||||
type Game struct {
|
||||
World *box2d.World
|
||||
TimeStep float64
|
||||
}
|
||||
|
||||
// NewGame - Start new game
|
||||
func NewGame() (g Game) {
|
||||
g.Init()
|
||||
return
|
||||
}
|
||||
|
||||
// Init - Initialize game
|
||||
func (g *Game) Init() {
|
||||
gravity := box2d.Vec2{0.0, -10.0}
|
||||
g.World = box2d.NewWorld(gravity, 10) // 10 iterations
|
||||
}
|
||||
|
||||
// Update - Update game
|
||||
func (g *Game) Update() {
|
||||
// Keys 1-9 switch demos
|
||||
switch rl.GetKeyPressed() {
|
||||
case rl.KeyOne:
|
||||
g.Demo1()
|
||||
case rl.KeyTwo:
|
||||
g.Demo2()
|
||||
case rl.KeyThree:
|
||||
g.Demo3()
|
||||
case rl.KeyFour:
|
||||
g.Demo4()
|
||||
case rl.KeyFive:
|
||||
g.Demo5()
|
||||
case rl.KeySix:
|
||||
g.Demo6()
|
||||
case rl.KeySeven:
|
||||
g.Demo7()
|
||||
case rl.KeyEight:
|
||||
g.Demo8()
|
||||
case rl.KeyNine:
|
||||
g.Demo9()
|
||||
}
|
||||
|
||||
g.TimeStep = float64(rl.GetFrameTime())
|
||||
|
||||
// Physics steps calculations
|
||||
g.World.Step(g.TimeStep)
|
||||
}
|
||||
|
||||
// Draw - Draw game
|
||||
func (g *Game) Draw() {
|
||||
for _, b := range g.World.Bodies {
|
||||
g.DrawBody(b)
|
||||
}
|
||||
for _, j := range g.World.Joints {
|
||||
g.DrawJoint(j)
|
||||
}
|
||||
|
||||
rl.DrawText("Use keys 1-9 to switch current demo", 20, 20, 10, rl.RayWhite)
|
||||
}
|
||||
|
||||
// DrawBody - Draw body
|
||||
func (g *Game) DrawBody(b *box2d.Body) {
|
||||
R := box2d.Mat22ByAngle(b.Rotation)
|
||||
x := b.Position
|
||||
h := box2d.MulSV(0.5, b.Width)
|
||||
|
||||
o := box2d.Vec2{400, 400}
|
||||
S := box2d.Mat22{box2d.Vec2{20.0, 0.0}, box2d.Vec2{0.0, -20.0}}
|
||||
|
||||
v1 := o.Add(S.MulV(x.Add(R.MulV(box2d.Vec2{-h.X, -h.Y}))))
|
||||
v2 := o.Add(S.MulV(x.Add(R.MulV(box2d.Vec2{h.X, -h.Y}))))
|
||||
v3 := o.Add(S.MulV(x.Add(R.MulV(box2d.Vec2{h.X, h.Y}))))
|
||||
v4 := o.Add(S.MulV(x.Add(R.MulV(box2d.Vec2{-h.X, h.Y}))))
|
||||
|
||||
rl.DrawLine(int32(v1.X), int32(v1.Y), int32(v2.X), int32(v2.Y), rl.RayWhite)
|
||||
rl.DrawLine(int32(v2.X), int32(v2.Y), int32(v3.X), int32(v3.Y), rl.RayWhite)
|
||||
rl.DrawLine(int32(v3.X), int32(v3.Y), int32(v4.X), int32(v4.Y), rl.RayWhite)
|
||||
rl.DrawLine(int32(v4.X), int32(v4.Y), int32(v1.X), int32(v1.Y), rl.RayWhite)
|
||||
}
|
||||
|
||||
// DrawJoint - Draw joint
|
||||
func (g *Game) DrawJoint(j *box2d.Joint) {
|
||||
b1 := j.Body1
|
||||
b2 := j.Body2
|
||||
|
||||
R1 := box2d.Mat22ByAngle(b1.Rotation)
|
||||
R2 := box2d.Mat22ByAngle(b2.Rotation)
|
||||
|
||||
x1 := b1.Position
|
||||
p1 := x1.Add(R1.MulV(j.LocalAnchor1))
|
||||
|
||||
x2 := b2.Position
|
||||
p2 := x2.Add(R2.MulV(j.LocalAnchor2))
|
||||
|
||||
o := box2d.Vec2{400, 400}
|
||||
S := box2d.Mat22{box2d.Vec2{20.0, 0.0}, box2d.Vec2{0.0, -20.0}}
|
||||
|
||||
x1 = o.Add(S.MulV(x1))
|
||||
p1 = o.Add(S.MulV(p1))
|
||||
x2 = o.Add(S.MulV(x2))
|
||||
p2 = o.Add(S.MulV(p2))
|
||||
|
||||
rl.DrawLine(int32(x1.X), int32(x1.Y), int32(p1.X), int32(p1.Y), rl.RayWhite)
|
||||
rl.DrawLine(int32(x2.X), int32(x2.Y), int32(p2.X), int32(p2.Y), rl.RayWhite)
|
||||
}
|
||||
|
||||
// Demo1 - Single box
|
||||
func (g *Game) Demo1() {
|
||||
g.World.Clear()
|
||||
|
||||
var b1, b2 box2d.Body
|
||||
|
||||
b1.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b1.Position = box2d.Vec2{0.0, -0.5 * b1.Width.Y}
|
||||
g.World.AddBody(&b1)
|
||||
|
||||
b2.Set(&box2d.Vec2{1.0, 1.0}, 200.0)
|
||||
b2.Position = box2d.Vec2{0.0, 4.0}
|
||||
g.World.AddBody(&b2)
|
||||
}
|
||||
|
||||
// Demo2 - A simple pendulum
|
||||
func (g *Game) Demo2() {
|
||||
g.World.Clear()
|
||||
|
||||
var b2, b1 box2d.Body
|
||||
var j box2d.Joint
|
||||
|
||||
b1.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b1.Friction = 0.2
|
||||
b1.Position = box2d.Vec2{0.0, -0.5 * b1.Width.Y}
|
||||
b1.Rotation = 0.0
|
||||
g.World.AddBody(&b1)
|
||||
|
||||
b2.Set(&box2d.Vec2{1.0, 1.0}, 100.0)
|
||||
b2.Friction = 0.2
|
||||
b2.Position = box2d.Vec2{9.0, 11.0}
|
||||
b2.Rotation = 0.0
|
||||
g.World.AddBody(&b2)
|
||||
|
||||
j.Set(&b1, &b2, &box2d.Vec2{0.0, 11.0})
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
// Demo3 - Varying friction coefficients
|
||||
func (g *Game) Demo3() {
|
||||
g.World.Clear()
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{0.0, -0.5 * b.Width.Y}
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{13.0, 0.25}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{-2.0, 11.0}
|
||||
b.Rotation = -0.25
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{0.25, 1.0}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{5.25, 9.5}
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{13.0, 0.25}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{2.0, 7.0}
|
||||
b.Rotation = 0.25
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{0.25, 1.0}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{-5.25, 5.5}
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
frictions := []float64{0.75, 0.5, 0.35, 0.1, 0.0}
|
||||
for i := 0; i < 5; i++ {
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{0.5, 0.5}, 25.0)
|
||||
b.Friction = frictions[i]
|
||||
b.Position = box2d.Vec2{-7.5 + 2.0*float64(i), 14.0}
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Demo4 - A vertical stack
|
||||
func (g *Game) Demo4() {
|
||||
g.World.Clear()
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b.Friction = 0.2
|
||||
b.Position = box2d.Vec2{0.0, -0.5 * b.Width.Y}
|
||||
b.Rotation = 0.0
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{1.0, 1.0}, 1.0)
|
||||
b.Friction = 0.2
|
||||
x := rand.Float64()*0.2 - 0.1
|
||||
b.Position = box2d.Vec2{x, 0.51 + 1.05*float64(i)}
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Demo5 - A pyramid
|
||||
func (g *Game) Demo5() {
|
||||
g.World.Clear()
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b.Friction = 0.2
|
||||
b.Position = box2d.Vec2{0.0, -0.5 * b.Width.Y}
|
||||
b.Rotation = 0.0
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
x := box2d.Vec2{-6.0, 0.75}
|
||||
|
||||
for i := 0; i < 12; i++ {
|
||||
y := x
|
||||
for j := i; j < 12; j++ {
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{1.0, 1.0}, 10.0)
|
||||
b.Friction = 0.2
|
||||
b.Position = y
|
||||
g.World.AddBody(&b)
|
||||
|
||||
y = y.Add(box2d.Vec2{1.125, 0.0})
|
||||
}
|
||||
|
||||
x = x.Add(box2d.Vec2{0.5625, 2.0})
|
||||
}
|
||||
}
|
||||
|
||||
// Demo6 - A teeter
|
||||
func (g *Game) Demo6() {
|
||||
g.World.Clear()
|
||||
|
||||
var b1, b2, b3, b4, b5 box2d.Body
|
||||
b1.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b1.Position = box2d.Vec2{0.0, -0.5 * b1.Width.Y}
|
||||
g.World.AddBody(&b1)
|
||||
|
||||
b2.Set(&box2d.Vec2{12.0, 0.25}, 100)
|
||||
b2.Position = box2d.Vec2{0.0, 1.0}
|
||||
g.World.AddBody(&b2)
|
||||
|
||||
b3.Set(&box2d.Vec2{0.5, 0.5}, 25.0)
|
||||
b3.Position = box2d.Vec2{-5.0, 2.0}
|
||||
g.World.AddBody(&b3)
|
||||
|
||||
b4.Set(&box2d.Vec2{0.5, 0.5}, 25.0)
|
||||
b4.Position = box2d.Vec2{-5.5, 2.0}
|
||||
g.World.AddBody(&b4)
|
||||
|
||||
b5.Set(&box2d.Vec2{1.0, 1.0}, 100)
|
||||
b5.Position = box2d.Vec2{5.5, 15.0}
|
||||
g.World.AddBody(&b5)
|
||||
|
||||
{
|
||||
var j box2d.Joint
|
||||
j.Set(&b1, &b2, &box2d.Vec2{0.0, 1.0})
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Demo7 - A suspension bridge
|
||||
func (g *Game) Demo7() {
|
||||
g.World.Clear()
|
||||
|
||||
var ba []*box2d.Body
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b.Friction = 0.2
|
||||
b.Position = box2d.Vec2{0.0, -0.5 * b.Width.Y}
|
||||
b.Rotation = 0.0
|
||||
g.World.AddBody(&b)
|
||||
ba = append(ba, &b)
|
||||
}
|
||||
|
||||
const numPlunks = 15
|
||||
const mass = 50.0
|
||||
|
||||
for i := 0; i < numPlunks; i++ {
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{1.0, 0.25}, mass)
|
||||
b.Friction = 0.2
|
||||
b.Position = box2d.Vec2{-8.5 + 1.25*float64(i), 5.0}
|
||||
g.World.AddBody(&b)
|
||||
ba = append(ba, &b)
|
||||
}
|
||||
|
||||
// Tuning
|
||||
const frequencyHz = 2.0
|
||||
const dampingRatio = 0.7
|
||||
|
||||
// Frequency in radians
|
||||
const omega = 2.0 * math.Pi * frequencyHz
|
||||
|
||||
// Damping coefficient
|
||||
const d = 2.0 * mass * dampingRatio * omega
|
||||
|
||||
// Spring stifness
|
||||
const k = mass * omega * omega
|
||||
|
||||
// Magic formulas
|
||||
softness := 1.0 / (d + g.TimeStep*k)
|
||||
biasFactor := g.TimeStep * k / (d + g.TimeStep*k)
|
||||
|
||||
for i := 0; i <= numPlunks; i++ {
|
||||
var j box2d.Joint
|
||||
j.Set(ba[i], ba[(i+1)%(numPlunks+1)], &box2d.Vec2{-9.125 + 1.25*float64(i), 5.0})
|
||||
j.Softness = softness
|
||||
j.BiasFactor = biasFactor
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Demo8 - Dominos
|
||||
func (g *Game) Demo8() {
|
||||
g.World.Clear()
|
||||
|
||||
var b1 box2d.Body
|
||||
b1.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b1.Position = box2d.Vec2{0.0, -0.5 * b1.Width.Y}
|
||||
g.World.AddBody(&b1)
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{12.0, 0.5}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{-1.5, 10.0}
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{0.2, 2.0}, 10.0)
|
||||
b.Position = box2d.Vec2{-6.0 + 1.0*float64(i), 11.125}
|
||||
b.Friction = 0.1
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{14.0, 0.5}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{1.0, 6.0}
|
||||
b.Rotation = 0.3
|
||||
g.World.AddBody(&b)
|
||||
}
|
||||
|
||||
var b2 box2d.Body
|
||||
b2.Set(&box2d.Vec2{0.5, 3.0}, math.MaxFloat64)
|
||||
b2.Position = box2d.Vec2{-7.0, 4.0}
|
||||
g.World.AddBody(&b2)
|
||||
|
||||
var b3 box2d.Body
|
||||
b3.Set(&box2d.Vec2{12.0, 0.25}, 20.0)
|
||||
b3.Position = box2d.Vec2{-0.9, 1.0}
|
||||
g.World.AddBody(&b3)
|
||||
|
||||
{
|
||||
var j box2d.Joint
|
||||
j.Set(&b1, &b3, &box2d.Vec2{-2.0, 1.0})
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
var b4 box2d.Body
|
||||
b4.Set(&box2d.Vec2{0.5, 0.5}, 10.0)
|
||||
b4.Position = box2d.Vec2{-10.0, 15.0}
|
||||
g.World.AddBody(&b4)
|
||||
|
||||
{
|
||||
var j box2d.Joint
|
||||
j.Set(&b2, &b4, &box2d.Vec2{-7.0, 15.0})
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
var b5 box2d.Body
|
||||
b5.Set(&box2d.Vec2{2.0, 2.0}, 20.0)
|
||||
b5.Position = box2d.Vec2{6.0, 2.5}
|
||||
b5.Friction = 0.1
|
||||
g.World.AddBody(&b5)
|
||||
|
||||
{
|
||||
var j box2d.Joint
|
||||
j.Set(&b1, &b5, &box2d.Vec2{6.0, 2.6})
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
var b6 box2d.Body
|
||||
b6.Set(&box2d.Vec2{2.0, 0.2}, 10.0)
|
||||
b6.Position = box2d.Vec2{6.0, 3.6}
|
||||
g.World.AddBody(&b6)
|
||||
|
||||
{
|
||||
var j box2d.Joint
|
||||
j.Set(&b5, &b6, &box2d.Vec2{7.0, 3.5})
|
||||
g.World.AddJoint(&j)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Demo9 - A multi-pendulum
|
||||
func (g *Game) Demo9() {
|
||||
g.World.Clear()
|
||||
|
||||
var b1 *box2d.Body
|
||||
|
||||
{
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{100.0, 20.0}, math.MaxFloat64)
|
||||
b.Position = box2d.Vec2{0.0, -0.5 * b.Width.Y}
|
||||
g.World.AddBody(&b)
|
||||
b1 = &b
|
||||
}
|
||||
|
||||
const mass = 10.0
|
||||
|
||||
// Tuning
|
||||
const frequencyHz = 4.0
|
||||
const dampingRatio = 0.7
|
||||
|
||||
// Frequency in radians
|
||||
const omega = 2.0 * math.Pi * frequencyHz
|
||||
|
||||
// Damping coefficient
|
||||
const d = 2.0 * mass * dampingRatio * omega
|
||||
|
||||
// Spring stiffness
|
||||
const k = mass * omega * omega
|
||||
|
||||
// Magic formulas
|
||||
softness := 1.0 / (d + g.TimeStep*k)
|
||||
biasFactor := g.TimeStep * k / (d + g.TimeStep*k)
|
||||
|
||||
const y = 12.0
|
||||
|
||||
for i := 0; i < 15; i++ {
|
||||
x := box2d.Vec2{0.5 + float64(i), y}
|
||||
|
||||
var b box2d.Body
|
||||
b.Set(&box2d.Vec2{0.75, 0.25}, mass)
|
||||
b.Friction = 0.2
|
||||
b.Position = x
|
||||
b.Rotation = 0.0
|
||||
g.World.AddBody(&b)
|
||||
|
||||
var j box2d.Joint
|
||||
j.Set(b1, &b, &box2d.Vec2{float64(i), y})
|
||||
j.Softness = softness
|
||||
j.BiasFactor = biasFactor
|
||||
g.World.AddJoint(&j)
|
||||
|
||||
b1 = &b
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
rl.InitWindow(800, 450, "raylib [physics] example - box2d")
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
game := NewGame()
|
||||
|
||||
game.Demo1()
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
|
||||
game.Update()
|
||||
|
||||
game.Draw()
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
163
examples/physics/chipmunk/main.go
Normal file
163
examples/physics/chipmunk/main.go
Normal file
|
@ -0,0 +1,163 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
"github.com/jakecoffman/cp"
|
||||
)
|
||||
|
||||
var grabbableMaskBit uint = 1 << 31
|
||||
var grabFilter = cp.ShapeFilter{
|
||||
cp.NO_GROUP, grabbableMaskBit, grabbableMaskBit,
|
||||
}
|
||||
|
||||
func randUnitCircle() cp.Vector {
|
||||
v := cp.Vector{X: rand.Float64()*2.0 - 1.0, Y: rand.Float64()*2.0 - 1.0}
|
||||
if v.LengthSq() < 1.0 {
|
||||
return v
|
||||
}
|
||||
return randUnitCircle()
|
||||
}
|
||||
|
||||
var simpleTerrainVerts = []cp.Vector{
|
||||
{350.00, 425.07}, {336.00, 436.55}, {272.00, 435.39}, {258.00, 427.63}, {225.28, 420.00}, {202.82, 396.00},
|
||||
{191.81, 388.00}, {189.00, 381.89}, {173.00, 380.39}, {162.59, 368.00}, {150.47, 319.00}, {128.00, 311.55},
|
||||
{119.14, 286.00}, {126.84, 263.00}, {120.56, 227.00}, {141.14, 178.00}, {137.52, 162.00}, {146.51, 142.00},
|
||||
{156.23, 136.00}, {158.00, 118.27}, {170.00, 100.77}, {208.43, 84.00}, {224.00, 69.65}, {249.30, 68.00},
|
||||
{257.00, 54.77}, {363.00, 45.94}, {374.15, 54.00}, {386.00, 69.60}, {413.00, 70.73}, {456.00, 84.89},
|
||||
{468.09, 99.00}, {467.09, 123.00}, {464.92, 135.00}, {469.00, 141.03}, {497.00, 148.67}, {513.85, 180.00},
|
||||
{509.56, 223.00}, {523.51, 247.00}, {523.00, 277.00}, {497.79, 311.00}, {478.67, 348.00}, {467.90, 360.00},
|
||||
{456.76, 382.00}, {432.95, 389.00}, {417.00, 411.32}, {373.00, 433.19}, {361.00, 430.02}, {350.00, 425.07},
|
||||
}
|
||||
|
||||
// creates a circle with random placement
|
||||
func addCircle(space *cp.Space, radius float64) {
|
||||
mass := radius * radius / 25.0
|
||||
body := space.AddBody(cp.NewBody(mass, cp.MomentForCircle(mass, 0, radius, cp.Vector{})))
|
||||
body.SetPosition(randUnitCircle().Mult(180))
|
||||
|
||||
shape := space.AddShape(cp.NewCircle(body, radius, cp.Vector{}))
|
||||
shape.SetElasticity(0)
|
||||
shape.SetFriction(0.9)
|
||||
}
|
||||
|
||||
// creates a simple terrain to contain bodies
|
||||
func simpleTerrain() *cp.Space {
|
||||
space := cp.NewSpace()
|
||||
space.Iterations = 10
|
||||
space.SetGravity(cp.Vector{0, -100})
|
||||
space.SetCollisionSlop(0.5)
|
||||
|
||||
offset := cp.Vector{X: -320, Y: -240}
|
||||
for i := 0; i < len(simpleTerrainVerts)-1; i++ {
|
||||
a := simpleTerrainVerts[i]
|
||||
b := simpleTerrainVerts[i+1]
|
||||
space.AddShape(cp.NewSegment(space.StaticBody, a.Add(offset), b.Add(offset), 0))
|
||||
}
|
||||
|
||||
return space
|
||||
}
|
||||
|
||||
func main() {
|
||||
const width, height = 800, 450
|
||||
const physicsTickrate = 1.0 / 60.0
|
||||
|
||||
rl.SetConfigFlags(rl.FlagVsyncHint)
|
||||
rl.InitWindow(width, height, "raylib [physics] example - chipmunk")
|
||||
|
||||
offset := rl.Vector2{X: width / 2, Y: height / 2}
|
||||
// since the example ported from elsewhere, flip the camera 180 and offset to center it
|
||||
camera := rl.NewCamera2D(offset, rl.Vector2{}, 180, 1)
|
||||
|
||||
space := simpleTerrain()
|
||||
for i := 0; i < 1000; i++ {
|
||||
addCircle(space, 5)
|
||||
}
|
||||
mouseBody := cp.NewKinematicBody()
|
||||
var mouse cp.Vector
|
||||
var mouseJoint *cp.Constraint
|
||||
|
||||
var accumulator, dt float32
|
||||
lastTime := rl.GetTime()
|
||||
for !rl.WindowShouldClose() {
|
||||
// calculate dt
|
||||
now := rl.GetTime()
|
||||
dt = float32(now - lastTime)
|
||||
lastTime = now
|
||||
|
||||
// update the mouse position
|
||||
mousePos := rl.GetMousePosition()
|
||||
// alter the mouse coordinates based on the camera position, rotation
|
||||
mouse.X = float64(mousePos.X-camera.Offset.X) * -1
|
||||
mouse.Y = float64(mousePos.Y-camera.Offset.Y) * -1
|
||||
// smooth mouse movements to new position
|
||||
newPoint := mouseBody.Position().Lerp(mouse, 0.25)
|
||||
mouseBody.SetVelocityVector(newPoint.Sub(mouseBody.Position()).Mult(60.0))
|
||||
mouseBody.SetPosition(newPoint)
|
||||
|
||||
// handle grabbing
|
||||
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||
result := space.PointQueryNearest(mouse, 5, grabFilter)
|
||||
if result.Shape != nil && result.Shape.Body().Mass() < cp.INFINITY {
|
||||
var nearest cp.Vector
|
||||
if result.Distance > 0 {
|
||||
nearest = result.Point
|
||||
} else {
|
||||
nearest = mouse
|
||||
}
|
||||
|
||||
// create a new constraint where the mouse is to draw the body towards the mouse
|
||||
body := result.Shape.Body()
|
||||
mouseJoint = cp.NewPivotJoint2(mouseBody, body, cp.Vector{}, body.WorldToLocal(nearest))
|
||||
mouseJoint.SetMaxForce(50000)
|
||||
mouseJoint.SetErrorBias(math.Pow(1.0-0.15, 60.0))
|
||||
space.AddConstraint(mouseJoint)
|
||||
}
|
||||
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) && mouseJoint != nil {
|
||||
space.RemoveConstraint(mouseJoint)
|
||||
mouseJoint = nil
|
||||
}
|
||||
|
||||
// perform a fixed rate physics tick
|
||||
accumulator += dt
|
||||
for accumulator >= physicsTickrate {
|
||||
space.Step(physicsTickrate)
|
||||
accumulator -= physicsTickrate
|
||||
}
|
||||
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.RayWhite)
|
||||
rl.BeginMode2D(camera)
|
||||
|
||||
// this is a generic way to iterate over the shapes in a space,
|
||||
// to avoid the type switch just keep a pointer to the shapes when they've been created
|
||||
space.EachShape(func(s *cp.Shape) {
|
||||
switch s.Class.(type) {
|
||||
case *cp.Segment:
|
||||
segment := s.Class.(*cp.Segment)
|
||||
a := segment.A()
|
||||
b := segment.B()
|
||||
rl.DrawLineV(v(a), v(b), rl.Black)
|
||||
case *cp.Circle:
|
||||
circle := s.Class.(*cp.Circle)
|
||||
pos := circle.Body().Position()
|
||||
rl.DrawCircleV(v(pos), float32(circle.Radius()), rl.Red)
|
||||
default:
|
||||
fmt.Println("unexpected shape", s.Class)
|
||||
}
|
||||
})
|
||||
|
||||
rl.EndMode2D()
|
||||
rl.DrawFPS(0, 0)
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
||||
|
||||
func v(v cp.Vector) rl.Vector2 {
|
||||
return rl.Vector2{X: float32(v.X), Y: float32(v.Y)}
|
||||
}
|
102
examples/physics/physac/demo/main.go
Normal file
102
examples/physics/physac/demo/main.go
Normal file
|
@ -0,0 +1,102 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gen2brain/raylib-go/physics"
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
screenWidth := int32(800)
|
||||
screenHeight := int32(450)
|
||||
|
||||
rl.SetConfigFlags(rl.FlagMsaa4xHint)
|
||||
rl.InitWindow(screenWidth, screenHeight, "Physac [raylib] - physics demo")
|
||||
|
||||
// Physac logo drawing position
|
||||
logoX := screenWidth - rl.MeasureText("Physac", 30) - 10
|
||||
logoY := int32(15)
|
||||
|
||||
// Initialize physics and default physics bodies
|
||||
physics.Init()
|
||||
|
||||
// Create floor rectangle physics body
|
||||
floor := physics.NewBodyRectangle(rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)), 500, 100, 10)
|
||||
floor.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
|
||||
// Create obstacle circle physics body
|
||||
circle := physics.NewBodyCircle(rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2), 45, 10)
|
||||
circle.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
|
||||
// Update created physics objects
|
||||
physics.Update()
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyR) { // Reset physics input
|
||||
physics.Reset()
|
||||
|
||||
floor = physics.NewBodyRectangle(rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)), 500, 100, 10)
|
||||
floor.Enabled = false
|
||||
|
||||
circle = physics.NewBodyCircle(rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2), 45, 10)
|
||||
circle.Enabled = false
|
||||
}
|
||||
|
||||
// Physics body creation inputs
|
||||
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||
physics.NewBodyPolygon(rl.GetMousePosition(), float32(rl.GetRandomValue(20, 80)), int(rl.GetRandomValue(3, 8)), 10)
|
||||
} else if rl.IsMouseButtonPressed(rl.MouseRightButton) {
|
||||
physics.NewBodyCircle(rl.GetMousePosition(), float32(rl.GetRandomValue(10, 45)), 10)
|
||||
}
|
||||
|
||||
// Destroy falling physics bodies
|
||||
for i := 0; i < physics.GetBodiesCount(); i++ {
|
||||
body := physics.GetBody(i)
|
||||
if body.Position.Y > float32(screenHeight)*2 {
|
||||
body.Destroy()
|
||||
}
|
||||
}
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
|
||||
rl.DrawFPS(screenWidth-90, screenHeight-30)
|
||||
|
||||
// Draw created physics bodies
|
||||
for i := 0; i < physics.GetBodiesCount(); i++ {
|
||||
body := physics.GetBody(i)
|
||||
|
||||
vertexCount := physics.GetShapeVerticesCount(i)
|
||||
for j := 0; j < vertexCount; j++ {
|
||||
// Get physics bodies shape vertices to draw lines
|
||||
// NOTE: GetShapeVertex() already calculates rotation transformations
|
||||
vertexA := body.GetShapeVertex(j)
|
||||
|
||||
jj := 0
|
||||
if j+1 < vertexCount { // Get next vertex or first to close the shape
|
||||
jj = j + 1
|
||||
}
|
||||
|
||||
vertexB := body.GetShapeVertex(jj)
|
||||
|
||||
rl.DrawLineV(vertexA, vertexB, rl.Green) // Draw a line between two vertex positions
|
||||
}
|
||||
}
|
||||
|
||||
rl.DrawText("Left mouse button to create a polygon", 10, 10, 10, rl.White)
|
||||
rl.DrawText("Right mouse button to create a circle", 10, 25, 10, rl.White)
|
||||
rl.DrawText("Press 'R' to reset example", 10, 40, 10, rl.White)
|
||||
|
||||
rl.DrawText("Physac", logoX, logoY, 30, rl.White)
|
||||
rl.DrawText("Powered by", logoX+50, logoY-7, 10, rl.White)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
physics.Close() // Unitialize physics
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
113
examples/physics/physac/friction/main.go
Normal file
113
examples/physics/physac/friction/main.go
Normal file
|
@ -0,0 +1,113 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gen2brain/raylib-go/physics"
|
||||
"github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
screenWidth := int32(800)
|
||||
screenHeight := int32(450)
|
||||
|
||||
rl.SetConfigFlags(rl.FlagMsaa4xHint)
|
||||
rl.InitWindow(screenWidth, screenHeight, "Physac [raylib] - physics friction")
|
||||
|
||||
// Physac logo drawing position
|
||||
logoX := screenWidth - rl.MeasureText("Physac", 30) - 10
|
||||
logoY := int32(15)
|
||||
|
||||
// Initialize physics and default physics bodies
|
||||
physics.Init()
|
||||
|
||||
// Create floor rectangle physics body
|
||||
floor := physics.NewBodyRectangle(rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)), float32(screenHeight), 100, 10)
|
||||
floor.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
wall := physics.NewBodyRectangle(rl.NewVector2(float32(screenWidth)/2, float32(screenHeight)*0.8), 10, 80, 10)
|
||||
wall.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
|
||||
// Create left ramp physics body
|
||||
rectLeft := physics.NewBodyRectangle(rl.NewVector2(25, float32(screenHeight)-5), 250, 250, 10)
|
||||
rectLeft.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
rectLeft.SetRotation(30 * rl.Deg2rad)
|
||||
|
||||
// Create right ramp physics body
|
||||
rectRight := physics.NewBodyRectangle(rl.NewVector2(float32(screenWidth)-25, float32(screenHeight)-5), 250, 250, 10)
|
||||
rectRight.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
rectRight.SetRotation(330 * rl.Deg2rad)
|
||||
|
||||
// Create dynamic physics bodies
|
||||
bodyA := physics.NewBodyRectangle(rl.NewVector2(35, float32(screenHeight)*0.6), 40, 40, 10)
|
||||
bodyA.StaticFriction = 0.1
|
||||
bodyA.DynamicFriction = 0.1
|
||||
bodyA.SetRotation(30 * rl.Deg2rad)
|
||||
|
||||
bodyB := physics.NewBodyRectangle(rl.NewVector2(float32(screenWidth)-35, float32(screenHeight)*0.6), 40, 40, 10)
|
||||
bodyB.StaticFriction = 1
|
||||
bodyB.DynamicFriction = 1
|
||||
bodyB.SetRotation(330 * rl.Deg2rad)
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
// Physics steps calculations
|
||||
physics.Update()
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyR) { // Reset physics input
|
||||
// Reset dynamic physics bodies position, velocity and rotation
|
||||
bodyA.Position = rl.NewVector2(35, float32(screenHeight)*0.6)
|
||||
bodyA.Velocity = rl.NewVector2(0, 0)
|
||||
bodyA.AngularVelocity = 0
|
||||
bodyA.SetRotation(30 * rl.Deg2rad)
|
||||
|
||||
bodyB.Position = rl.NewVector2(float32(screenWidth)-35, float32(screenHeight)*0.6)
|
||||
bodyB.Velocity = rl.NewVector2(0, 0)
|
||||
bodyB.AngularVelocity = 0
|
||||
bodyB.SetRotation(330 * rl.Deg2rad)
|
||||
}
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
|
||||
rl.DrawFPS(screenWidth-90, screenHeight-30)
|
||||
|
||||
// Draw created physics bodies
|
||||
bodiesCount := physics.GetBodiesCount()
|
||||
for i := 0; i < bodiesCount; i++ {
|
||||
body := physics.GetBody(i)
|
||||
|
||||
vertexCount := physics.GetShapeVerticesCount(i)
|
||||
for j := 0; j < vertexCount; j++ {
|
||||
// Get physics bodies shape vertices to draw lines
|
||||
// NOTE: GetShapeVertex() already calculates rotation transformations
|
||||
vertexA := body.GetShapeVertex(j)
|
||||
|
||||
jj := 0
|
||||
if j+1 < vertexCount { // Get next vertex or first to close the shape
|
||||
jj = j + 1
|
||||
}
|
||||
|
||||
vertexB := body.GetShapeVertex(jj)
|
||||
|
||||
rl.DrawLineV(vertexA, vertexB, rl.Green) // Draw a line between two vertex positions
|
||||
}
|
||||
}
|
||||
|
||||
rl.DrawRectangle(0, screenHeight-49, screenWidth, 49, rl.Black)
|
||||
|
||||
rl.DrawText("Friction amount", (screenWidth-rl.MeasureText("Friction amount", 30))/2, 75, 30, rl.White)
|
||||
rl.DrawText("0.1", int32(bodyA.Position.X)-rl.MeasureText("0.1", 20)/2, int32(bodyA.Position.Y)-7, 20, rl.White)
|
||||
rl.DrawText("1", int32(bodyB.Position.X)-rl.MeasureText("1", 20)/2, int32(bodyB.Position.Y)-7, 20, rl.White)
|
||||
|
||||
rl.DrawText("Press 'R' to reset example", 10, 10, 10, rl.White)
|
||||
|
||||
rl.DrawText("Physac", logoX, logoY, 30, rl.White)
|
||||
rl.DrawText("Powered by", logoX+50, logoY-7, 10, rl.White)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
physics.Close() // Unitialize physics
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
105
examples/physics/physac/movement/main.go
Normal file
105
examples/physics/physac/movement/main.go
Normal file
|
@ -0,0 +1,105 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gen2brain/raylib-go/physics"
|
||||
"github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const (
|
||||
velocity = 0.5
|
||||
)
|
||||
|
||||
func main() {
|
||||
screenWidth := float32(800)
|
||||
screenHeight := float32(450)
|
||||
|
||||
rl.SetConfigFlags(rl.FlagMsaa4xHint)
|
||||
rl.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - physics movement")
|
||||
|
||||
// Physac logo drawing position
|
||||
logoX := int32(screenWidth) - rl.MeasureText("Physac", 30) - 10
|
||||
logoY := int32(15)
|
||||
|
||||
// Initialize physics and default physics bodies
|
||||
physics.Init()
|
||||
|
||||
// Create floor and walls rectangle physics body
|
||||
floor := physics.NewBodyRectangle(rl.NewVector2(screenWidth/2, screenHeight), screenWidth, 100, 10)
|
||||
platformLeft := physics.NewBodyRectangle(rl.NewVector2(screenWidth*0.25, screenHeight*0.6), screenWidth*0.25, 10, 10)
|
||||
platformRight := physics.NewBodyRectangle(rl.NewVector2(screenWidth*0.75, screenHeight*0.6), screenWidth*0.25, 10, 10)
|
||||
wallLeft := physics.NewBodyRectangle(rl.NewVector2(-5, screenHeight/2), 10, screenHeight, 10)
|
||||
wallRight := physics.NewBodyRectangle(rl.NewVector2(screenWidth+5, screenHeight/2), 10, screenHeight, 10)
|
||||
|
||||
// Disable dynamics to floor and walls physics bodies
|
||||
floor.Enabled = false
|
||||
platformLeft.Enabled = false
|
||||
platformRight.Enabled = false
|
||||
wallLeft.Enabled = false
|
||||
wallRight.Enabled = false
|
||||
|
||||
// Create movement physics body
|
||||
body := physics.NewBodyRectangle(rl.NewVector2(screenWidth/2, screenHeight/2), 50, 50, 1)
|
||||
body.FreezeOrient = true // Constrain body rotation to avoid little collision torque amounts
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
// Update created physics objects
|
||||
physics.Update()
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyR) { // Reset physics input
|
||||
// Reset movement physics body position, velocity and rotation
|
||||
body.Position = rl.NewVector2(screenWidth/2, screenHeight/2)
|
||||
body.Velocity = rl.NewVector2(0, 0)
|
||||
body.SetRotation(0)
|
||||
}
|
||||
|
||||
// Physics body creation inputs
|
||||
if rl.IsKeyDown(rl.KeyRight) {
|
||||
body.Velocity.X = velocity
|
||||
} else if rl.IsKeyDown(rl.KeyLeft) {
|
||||
body.Velocity.X = -velocity
|
||||
}
|
||||
|
||||
if rl.IsKeyDown(rl.KeyUp) && body.IsGrounded {
|
||||
body.Velocity.Y = -velocity * 4
|
||||
}
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
|
||||
rl.DrawFPS(int32(screenWidth)-90, int32(screenHeight)-30)
|
||||
|
||||
// Draw created physics bodies
|
||||
for i, body := range physics.GetBodies() {
|
||||
vertexCount := physics.GetShapeVerticesCount(i)
|
||||
for j := 0; j < vertexCount; j++ {
|
||||
// Get physics bodies shape vertices to draw lines
|
||||
// NOTE: GetShapeVertex() already calculates rotation transformations
|
||||
vertexA := body.GetShapeVertex(j)
|
||||
|
||||
jj := 0
|
||||
if j+1 < vertexCount { // Get next vertex or first to close the shape
|
||||
jj = j + 1
|
||||
}
|
||||
|
||||
vertexB := body.GetShapeVertex(jj)
|
||||
|
||||
rl.DrawLineV(vertexA, vertexB, rl.Green) // Draw a line between two vertex positions
|
||||
}
|
||||
}
|
||||
|
||||
rl.DrawText("Use 'ARROWS' to move player", 10, 10, 10, rl.White)
|
||||
rl.DrawText("Press 'R' to reset example", 10, 30, 10, rl.White)
|
||||
|
||||
rl.DrawText("Physac", logoX, logoY, 30, rl.White)
|
||||
rl.DrawText("Powered by", logoX+50, logoY-7, 10, rl.White)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
physics.Close() // Unitialize physics
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
96
examples/physics/physac/restitution/main.go
Normal file
96
examples/physics/physac/restitution/main.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gen2brain/raylib-go/physics"
|
||||
"github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const (
|
||||
velocity = 0.5
|
||||
)
|
||||
|
||||
func main() {
|
||||
screenWidth := float32(800)
|
||||
screenHeight := float32(450)
|
||||
|
||||
rl.SetConfigFlags(rl.FlagMsaa4xHint)
|
||||
rl.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - physics restitution")
|
||||
|
||||
// Physac logo drawing position
|
||||
logoX := int32(screenWidth) - rl.MeasureText("Physac", 30) - 10
|
||||
logoY := int32(15)
|
||||
|
||||
// Initialize physics and default physics bodies
|
||||
physics.Init()
|
||||
|
||||
// Create floor rectangle physics body
|
||||
floor := physics.NewBodyRectangle(rl.NewVector2(screenWidth/2, screenHeight), screenWidth, 100, 10)
|
||||
floor.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||
floor.Restitution = 1
|
||||
|
||||
// Create circles physics body
|
||||
circleA := physics.NewBodyCircle(rl.NewVector2(screenWidth*0.25, screenHeight/2), 30, 10)
|
||||
circleA.Restitution = 0
|
||||
circleB := physics.NewBodyCircle(rl.NewVector2(screenWidth*0.5, screenHeight/2), 30, 10)
|
||||
circleB.Restitution = 0.5
|
||||
circleC := physics.NewBodyCircle(rl.NewVector2(screenWidth*0.75, screenHeight/2), 30, 10)
|
||||
circleC.Restitution = 1
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
// Update created physics objects
|
||||
physics.Update()
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyR) { // Reset physics input
|
||||
// Reset circles physics bodies position and velocity
|
||||
circleA.Position = rl.NewVector2(screenWidth*0.25, screenHeight/2)
|
||||
circleA.Velocity = rl.NewVector2(0, 0)
|
||||
circleB.Position = rl.NewVector2(screenWidth*0.5, screenHeight/2)
|
||||
circleB.Velocity = rl.NewVector2(0, 0)
|
||||
circleC.Position = rl.NewVector2(screenWidth*0.75, screenHeight/2)
|
||||
circleC.Velocity = rl.NewVector2(0, 0)
|
||||
}
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
|
||||
rl.DrawFPS(int32(screenWidth)-90, int32(screenHeight)-30)
|
||||
|
||||
// Draw created physics bodies
|
||||
for i, body := range physics.GetBodies() {
|
||||
vertexCount := physics.GetShapeVerticesCount(i)
|
||||
for j := 0; j < vertexCount; j++ {
|
||||
// Get physics bodies shape vertices to draw lines
|
||||
// NOTE: GetShapeVertex() already calculates rotation transformations
|
||||
vertexA := body.GetShapeVertex(j)
|
||||
|
||||
jj := 0
|
||||
if j+1 < vertexCount { // Get next vertex or first to close the shape
|
||||
jj = j + 1
|
||||
}
|
||||
|
||||
vertexB := body.GetShapeVertex(jj)
|
||||
|
||||
rl.DrawLineV(vertexA, vertexB, rl.Green) // Draw a line between two vertex positions
|
||||
}
|
||||
}
|
||||
|
||||
rl.DrawText("Restitution amount", (int32(screenWidth)-rl.MeasureText("Restitution amount", 30))/2, 75, 30, rl.White)
|
||||
rl.DrawText("0", int32(circleA.Position.X)-rl.MeasureText("0", 20)/2, int32(circleA.Position.Y)-7, 20, rl.White)
|
||||
rl.DrawText("0.5", int32(circleB.Position.X)-rl.MeasureText("0.5", 20)/2, int32(circleB.Position.Y)-7, 20, rl.White)
|
||||
rl.DrawText("1", int32(circleC.Position.X)-rl.MeasureText("1", 20)/2, int32(circleC.Position.Y)-7, 20, rl.White)
|
||||
|
||||
rl.DrawText("Press 'R' to reset example", 10, 10, 10, rl.White)
|
||||
|
||||
rl.DrawText("Physac", logoX, logoY, 30, rl.White)
|
||||
rl.DrawText("Powered by", logoX+50, logoY-7, 10, rl.White)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
physics.Close() // Unitialize physics
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
89
examples/physics/physac/shatter/main.go
Normal file
89
examples/physics/physac/shatter/main.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gen2brain/raylib-go/physics"
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const (
|
||||
velocity = 0.5
|
||||
)
|
||||
|
||||
func main() {
|
||||
screenWidth := float32(800)
|
||||
screenHeight := float32(450)
|
||||
|
||||
rl.SetConfigFlags(rl.FlagMsaa4xHint)
|
||||
rl.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - body shatter")
|
||||
|
||||
// Physac logo drawing position
|
||||
logoX := int32(screenWidth) - rl.MeasureText("Physac", 30) - 10
|
||||
logoY := int32(15)
|
||||
|
||||
// Initialize physics and default physics bodies
|
||||
physics.Init()
|
||||
physics.SetGravity(0, 0)
|
||||
|
||||
// Create random polygon physics body to shatter
|
||||
physics.NewBodyPolygon(rl.NewVector2(screenWidth/2, screenHeight/2), float32(rl.GetRandomValue(80, 200)), int(rl.GetRandomValue(3, 8)), 10)
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
// Update created physics objects
|
||||
physics.Update()
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyR) { // Reset physics input
|
||||
physics.Reset()
|
||||
|
||||
// Create random polygon physics body to shatter
|
||||
physics.NewBodyPolygon(rl.NewVector2(screenWidth/2, screenHeight/2), float32(rl.GetRandomValue(80, 200)), int(rl.GetRandomValue(3, 8)), 10)
|
||||
}
|
||||
|
||||
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
|
||||
//for _, b := range physics.GetBodies() {
|
||||
for i := 0; i < physics.GetBodiesCount(); i++ {
|
||||
body := physics.GetBody(i)
|
||||
if body == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
physics.Shatter(body, rl.GetMousePosition(), 10/body.InverseMass)
|
||||
}
|
||||
}
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
|
||||
// Draw created physics bodies
|
||||
for i, body := range physics.GetBodies() {
|
||||
vertexCount := physics.GetShapeVerticesCount(i)
|
||||
for j := 0; j < vertexCount; j++ {
|
||||
// Get physics bodies shape vertices to draw lines
|
||||
// NOTE: GetShapeVertex() already calculates rotation transformations
|
||||
vertexA := body.GetShapeVertex(j)
|
||||
|
||||
jj := 0
|
||||
if j+1 < vertexCount { // Get next vertex or first to close the shape
|
||||
jj = j + 1
|
||||
}
|
||||
|
||||
vertexB := body.GetShapeVertex(jj)
|
||||
|
||||
rl.DrawLineV(vertexA, vertexB, rl.Green) // Draw a line between two vertex positions
|
||||
}
|
||||
}
|
||||
|
||||
rl.DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, rl.White)
|
||||
|
||||
rl.DrawText("Physac", logoX, logoY, 30, rl.White)
|
||||
rl.DrawText("Powered by", logoX+50, logoY-7, 10, rl.White)
|
||||
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
physics.Close() // Unitialize physics
|
||||
|
||||
rl.CloseWindow()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue