Improve physics and add examples
This commit is contained in:
parent
f200ce6e7e
commit
fc1eef80f8
7 changed files with 1039 additions and 376 deletions
118
examples/physics/physac/demo/main.go
Normal file
118
examples/physics/physac/demo/main.go
Normal file
|
@ -0,0 +1,118 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
//"fmt"
|
||||||
|
|
||||||
|
"github.com/gen2brain/raylib-go/physics"
|
||||||
|
"github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
screenWidth := int32(800)
|
||||||
|
screenHeight := int32(450)
|
||||||
|
|
||||||
|
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||||
|
raylib.InitWindow(screenWidth, screenHeight, "Physac [raylib] - physics demo")
|
||||||
|
|
||||||
|
// Physac logo drawing position
|
||||||
|
logoX := screenWidth - raylib.MeasureText("Physac", 30) - 10
|
||||||
|
logoY := int32(15)
|
||||||
|
|
||||||
|
// Initialize physics and default physics bodies
|
||||||
|
physics.Init()
|
||||||
|
|
||||||
|
// Create floor rectangle physics body
|
||||||
|
floor := physics.NewBodyRectangle(raylib.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(raylib.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2), 45, 10)
|
||||||
|
circle.Enabled = false // Disable body state to convert it to static (no dynamics, but collisions)
|
||||||
|
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
|
||||||
|
// Update created physics objects
|
||||||
|
physics.Update()
|
||||||
|
|
||||||
|
if raylib.IsKeyPressed(raylib.KeyR) { // Reset physics input
|
||||||
|
physics.Reset()
|
||||||
|
|
||||||
|
floor = physics.NewBodyRectangle(raylib.NewVector2(float32(screenWidth)/2, float32(screenHeight)), 500, 100, 10)
|
||||||
|
floor.Enabled = false
|
||||||
|
|
||||||
|
circle = physics.NewBodyCircle(raylib.NewVector2(float32(screenWidth)/2, float32(screenHeight)/2), 45, 10)
|
||||||
|
circle.Enabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Physics body creation inputs
|
||||||
|
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
|
||||||
|
physics.NewBodyPolygon(raylib.GetMousePosition(), float32(raylib.GetRandomValue(20, 80)), int(raylib.GetRandomValue(3, 8)), 10)
|
||||||
|
} else if raylib.IsMouseButtonPressed(raylib.MouseRightButton) {
|
||||||
|
physics.NewBodyCircle(raylib.GetMousePosition(), float32(raylib.GetRandomValue(10, 45)), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy falling physics bodies
|
||||||
|
for _, body := range physics.GetBodies() {
|
||||||
|
if body.Position.Y > float32(screenHeight)*2 {
|
||||||
|
physics.DestroyBody(body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
|
||||||
|
raylib.ClearBackground(raylib.Black)
|
||||||
|
|
||||||
|
raylib.DrawFPS(screenWidth-90, screenHeight-30)
|
||||||
|
|
||||||
|
// Draw created physics bodies
|
||||||
|
for i, body := range physics.GetBodies() {
|
||||||
|
//if body.Enabled && body.IsGrounded {
|
||||||
|
//fmt.Printf("%+v\n", body)
|
||||||
|
//fmt.Printf("Velocity: %.6f, %.6f\n", body.Velocity.X, body.Velocity.X)
|
||||||
|
//fmt.Printf("Force: %.6f, %.6f\n", body.Force.X, body.Force.X)
|
||||||
|
//fmt.Printf("AngularVelocity: %.6f\n", body.AngularVelocity)
|
||||||
|
//fmt.Printf("Torque: %.6f\n", body.Torque)
|
||||||
|
//fmt.Printf("Orient: %.6f\n", body.Orient)
|
||||||
|
//fmt.Printf("Inertia: %.6f\n", body.Inertia)
|
||||||
|
//fmt.Printf("InverseInertia: %.6f\n", body.InverseInertia)
|
||||||
|
//fmt.Printf("Mass: %.6f\n", body.Mass)
|
||||||
|
//fmt.Printf("InverseMass: %.6f\n", body.InverseMass)
|
||||||
|
//fmt.Printf("StaticF: %.6f\n", body.StaticFriction)
|
||||||
|
//fmt.Printf("DynamicF: %.6f\n", body.DynamicFriction)
|
||||||
|
//fmt.Printf("Restitution: %.6f\n\n", body.Restitution)
|
||||||
|
//fmt.Println()
|
||||||
|
//}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
raylib.DrawLineV(vertexA, vertexB, raylib.Green) // Draw a line between two vertex positions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.DrawText("Left mouse button to create a polygon", 10, 10, 10, raylib.White)
|
||||||
|
raylib.DrawText("Right mouse button to create a circle", 10, 25, 10, raylib.White)
|
||||||
|
raylib.DrawText("Press 'R' to reset example", 10, 40, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Physac", logoX, logoY, 30, raylib.White)
|
||||||
|
raylib.DrawText("Powered by", logoX+50, logoY-7, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
physics.Close() // Unitialize physics
|
||||||
|
|
||||||
|
raylib.CloseWindow()
|
||||||
|
}
|
114
examples/physics/physac/friction/main.go
Normal file
114
examples/physics/physac/friction/main.go
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gen2brain/raylib-go/physics"
|
||||||
|
"github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
screenWidth := int32(800)
|
||||||
|
screenHeight := int32(450)
|
||||||
|
|
||||||
|
raylib.SetDebug(true)
|
||||||
|
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||||
|
raylib.InitWindow(screenWidth, screenHeight, "Physac [raylib] - physics friction")
|
||||||
|
|
||||||
|
// Physac logo drawing position
|
||||||
|
logoX := screenWidth - raylib.MeasureText("Physac", 30) - 10
|
||||||
|
logoY := int32(15)
|
||||||
|
|
||||||
|
// Initialize physics and default physics bodies
|
||||||
|
physics.Init()
|
||||||
|
|
||||||
|
// Create floor rectangle physics body
|
||||||
|
floor := physics.NewBodyRectangle(raylib.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(raylib.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(raylib.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 * raylib.Deg2rad)
|
||||||
|
|
||||||
|
// Create right ramp physics body
|
||||||
|
rectRight := physics.NewBodyRectangle(raylib.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 * raylib.Deg2rad)
|
||||||
|
|
||||||
|
// Create dynamic physics bodies
|
||||||
|
bodyA := physics.NewBodyRectangle(raylib.NewVector2(35, float32(screenHeight)*0.6), 40, 40, 10)
|
||||||
|
bodyA.StaticFriction = 0.1
|
||||||
|
bodyA.DynamicFriction = 0.1
|
||||||
|
bodyA.SetRotation(30 * raylib.Deg2rad)
|
||||||
|
|
||||||
|
bodyB := physics.NewBodyRectangle(raylib.NewVector2(float32(screenWidth)-35, float32(screenHeight)*0.6), 40, 40, 10)
|
||||||
|
bodyB.StaticFriction = 1
|
||||||
|
bodyB.DynamicFriction = 1
|
||||||
|
bodyB.SetRotation(330 * raylib.Deg2rad)
|
||||||
|
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
// Physics steps calculations
|
||||||
|
physics.Update()
|
||||||
|
|
||||||
|
if raylib.IsKeyPressed(raylib.KeyR) { // Reset physics input
|
||||||
|
// Reset dynamic physics bodies position, velocity and rotation
|
||||||
|
bodyA.Position = raylib.NewVector2(35, float32(screenHeight)*0.6)
|
||||||
|
bodyA.Velocity = raylib.NewVector2(0, 0)
|
||||||
|
bodyA.AngularVelocity = 0
|
||||||
|
bodyA.SetRotation(30 * raylib.Deg2rad)
|
||||||
|
|
||||||
|
bodyB.Position = raylib.NewVector2(float32(screenWidth)-35, float32(screenHeight)*0.6)
|
||||||
|
bodyB.Velocity = raylib.NewVector2(0, 0)
|
||||||
|
bodyB.AngularVelocity = 0
|
||||||
|
bodyB.SetRotation(330 * raylib.Deg2rad)
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
|
||||||
|
raylib.ClearBackground(raylib.Black)
|
||||||
|
|
||||||
|
raylib.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)
|
||||||
|
|
||||||
|
raylib.DrawLineV(vertexA, vertexB, raylib.Green) // Draw a line between two vertex positions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.DrawRectangle(0, screenHeight-49, screenWidth, 49, raylib.Black)
|
||||||
|
|
||||||
|
raylib.DrawText("Friction amount", (screenWidth-raylib.MeasureText("Friction amount", 30))/2, 75, 30, raylib.White)
|
||||||
|
raylib.DrawText("0.1", int32(bodyA.Position.X)-raylib.MeasureText("0.1", 20)/2, int32(bodyA.Position.Y)-7, 20, raylib.White)
|
||||||
|
raylib.DrawText("1", int32(bodyB.Position.X)-raylib.MeasureText("1", 20)/2, int32(bodyB.Position.Y)-7, 20, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Press 'R' to reset example", 10, 10, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Physac", logoX, logoY, 30, raylib.White)
|
||||||
|
raylib.DrawText("Powered by", logoX+50, logoY-7, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
physics.Close() // Unitialize physics
|
||||||
|
|
||||||
|
raylib.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)
|
||||||
|
|
||||||
|
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||||
|
raylib.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - physics movement")
|
||||||
|
|
||||||
|
// Physac logo drawing position
|
||||||
|
logoX := int32(screenWidth) - raylib.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(raylib.NewVector2(screenWidth/2, screenHeight), screenWidth, 100, 10)
|
||||||
|
platformLeft := physics.NewBodyRectangle(raylib.NewVector2(screenWidth*0.25, screenHeight*0.6), screenWidth*0.25, 10, 10)
|
||||||
|
platformRight := physics.NewBodyRectangle(raylib.NewVector2(screenWidth*0.75, screenHeight*0.6), screenWidth*0.25, 10, 10)
|
||||||
|
wallLeft := physics.NewBodyRectangle(raylib.NewVector2(-5, screenHeight/2), 10, screenHeight, 10)
|
||||||
|
wallRight := physics.NewBodyRectangle(raylib.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(raylib.NewVector2(screenWidth/2, screenHeight/2), 50, 50, 1)
|
||||||
|
body.FreezeOrient = true // Constrain body rotation to avoid little collision torque amounts
|
||||||
|
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
// Update created physics objects
|
||||||
|
physics.Update()
|
||||||
|
|
||||||
|
if raylib.IsKeyPressed(raylib.KeyR) { // Reset physics input
|
||||||
|
// Reset movement physics body position, velocity and rotation
|
||||||
|
body.Position = raylib.NewVector2(screenWidth/2, screenHeight/2)
|
||||||
|
body.Velocity = raylib.NewVector2(0, 0)
|
||||||
|
body.SetRotation(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Physics body creation inputs
|
||||||
|
if raylib.IsKeyDown(raylib.KeyRight) {
|
||||||
|
body.Velocity.X = velocity
|
||||||
|
} else if raylib.IsKeyDown(raylib.KeyLeft) {
|
||||||
|
body.Velocity.X = -velocity
|
||||||
|
}
|
||||||
|
|
||||||
|
if raylib.IsKeyDown(raylib.KeyUp) && body.IsGrounded {
|
||||||
|
body.Velocity.Y = -velocity * 4
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
|
||||||
|
raylib.ClearBackground(raylib.Black)
|
||||||
|
|
||||||
|
raylib.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)
|
||||||
|
|
||||||
|
raylib.DrawLineV(vertexA, vertexB, raylib.Green) // Draw a line between two vertex positions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.DrawText("Use 'ARROWS' to move player", 10, 10, 10, raylib.White)
|
||||||
|
raylib.DrawText("Press 'R' to reset example", 10, 30, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Physac", logoX, logoY, 30, raylib.White)
|
||||||
|
raylib.DrawText("Powered by", logoX+50, logoY-7, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
physics.Close() // Unitialize physics
|
||||||
|
|
||||||
|
raylib.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)
|
||||||
|
|
||||||
|
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||||
|
raylib.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - physics restitution")
|
||||||
|
|
||||||
|
// Physac logo drawing position
|
||||||
|
logoX := int32(screenWidth) - raylib.MeasureText("Physac", 30) - 10
|
||||||
|
logoY := int32(15)
|
||||||
|
|
||||||
|
// Initialize physics and default physics bodies
|
||||||
|
physics.Init()
|
||||||
|
|
||||||
|
// Create floor rectangle physics body
|
||||||
|
floor := physics.NewBodyRectangle(raylib.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(raylib.NewVector2(screenWidth*0.25, screenHeight/2), 30, 10)
|
||||||
|
circleA.Restitution = 0
|
||||||
|
circleB := physics.NewBodyCircle(raylib.NewVector2(screenWidth*0.5, screenHeight/2), 30, 10)
|
||||||
|
circleB.Restitution = 0.5
|
||||||
|
circleC := physics.NewBodyCircle(raylib.NewVector2(screenWidth*0.75, screenHeight/2), 30, 10)
|
||||||
|
circleC.Restitution = 1
|
||||||
|
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
// Update created physics objects
|
||||||
|
physics.Update()
|
||||||
|
|
||||||
|
if raylib.IsKeyPressed(raylib.KeyR) { // Reset physics input
|
||||||
|
// Reset circles physics bodies position and velocity
|
||||||
|
circleA.Position = raylib.NewVector2(screenWidth*0.25, screenHeight/2)
|
||||||
|
circleA.Velocity = raylib.NewVector2(0, 0)
|
||||||
|
circleB.Position = raylib.NewVector2(screenWidth*0.5, screenHeight/2)
|
||||||
|
circleB.Velocity = raylib.NewVector2(0, 0)
|
||||||
|
circleC.Position = raylib.NewVector2(screenWidth*0.75, screenHeight/2)
|
||||||
|
circleC.Velocity = raylib.NewVector2(0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
|
||||||
|
raylib.ClearBackground(raylib.Black)
|
||||||
|
|
||||||
|
raylib.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)
|
||||||
|
|
||||||
|
raylib.DrawLineV(vertexA, vertexB, raylib.Green) // Draw a line between two vertex positions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.DrawText("Restitution amount", (int32(screenWidth)-raylib.MeasureText("Restitution amount", 30))/2, 75, 30, raylib.White)
|
||||||
|
raylib.DrawText("0", int32(circleA.Position.X)-raylib.MeasureText("0", 20)/2, int32(circleA.Position.Y)-7, 20, raylib.White)
|
||||||
|
raylib.DrawText("0.5", int32(circleB.Position.X)-raylib.MeasureText("0.5", 20)/2, int32(circleB.Position.Y)-7, 20, raylib.White)
|
||||||
|
raylib.DrawText("1", int32(circleC.Position.X)-raylib.MeasureText("1", 20)/2, int32(circleC.Position.Y)-7, 20, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Press 'R' to reset example", 10, 10, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Physac", logoX, logoY, 30, raylib.White)
|
||||||
|
raylib.DrawText("Powered by", logoX+50, logoY-7, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
physics.Close() // Unitialize physics
|
||||||
|
|
||||||
|
raylib.CloseWindow()
|
||||||
|
}
|
83
examples/physics/physac/shatter/main.go
Normal file
83
examples/physics/physac/shatter/main.go
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
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)
|
||||||
|
|
||||||
|
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||||
|
raylib.InitWindow(int32(screenWidth), int32(screenHeight), "Physac [raylib] - body shatter")
|
||||||
|
|
||||||
|
// Physac logo drawing position
|
||||||
|
logoX := int32(screenWidth) - raylib.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(raylib.NewVector2(screenWidth/2, screenHeight/2), float32(raylib.GetRandomValue(80, 200)), int(raylib.GetRandomValue(3, 8)), 10)
|
||||||
|
|
||||||
|
raylib.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !raylib.WindowShouldClose() {
|
||||||
|
// Update created physics objects
|
||||||
|
physics.Update()
|
||||||
|
|
||||||
|
if raylib.IsKeyPressed(raylib.KeyR) { // Reset physics input
|
||||||
|
physics.Reset()
|
||||||
|
|
||||||
|
// Create random polygon physics body to shatter
|
||||||
|
physics.NewBodyPolygon(raylib.NewVector2(screenWidth/2, screenHeight/2), float32(raylib.GetRandomValue(80, 200)), int(raylib.GetRandomValue(3, 8)), 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
|
||||||
|
for _, b := range physics.GetBodies() {
|
||||||
|
b.Shatter(raylib.GetMousePosition(), 10/b.InverseMass)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginDrawing()
|
||||||
|
|
||||||
|
raylib.ClearBackground(raylib.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)
|
||||||
|
|
||||||
|
raylib.DrawLineV(vertexA, vertexB, raylib.Green) // Draw a line between two vertex positions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.DrawText("Physac", logoX, logoY, 30, raylib.White)
|
||||||
|
raylib.DrawText("Powered by", logoX+50, logoY-7, 10, raylib.White)
|
||||||
|
|
||||||
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
physics.Close() // Unitialize physics
|
||||||
|
|
||||||
|
raylib.CloseWindow()
|
||||||
|
}
|
File diff suppressed because it is too large
Load diff
|
@ -7,22 +7,6 @@ import (
|
||||||
"github.com/gen2brain/raylib-go/raylib"
|
"github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Clamp - Clamp float value
|
|
||||||
func Clamp(value, min, max float32) float32 {
|
|
||||||
var res float32
|
|
||||||
if value < min {
|
|
||||||
res = min
|
|
||||||
} else {
|
|
||||||
res = value
|
|
||||||
}
|
|
||||||
|
|
||||||
if res > max {
|
|
||||||
return max
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vector2Zero - Vector with components value 0.0
|
// Vector2Zero - Vector with components value 0.0
|
||||||
func Vector2Zero() raylib.Vector2 {
|
func Vector2Zero() raylib.Vector2 {
|
||||||
return raylib.NewVector2(0.0, 0.0)
|
return raylib.NewVector2(0.0, 0.0)
|
||||||
|
@ -1039,3 +1023,19 @@ func QuaternionTransform(q *raylib.Quaternion, mat raylib.Matrix) {
|
||||||
q.Z = mat.M2*x + mat.M6*y + mat.M10*z + mat.M14*w
|
q.Z = mat.M2*x + mat.M6*y + mat.M10*z + mat.M14*w
|
||||||
q.W = mat.M3*x + mat.M7*y + mat.M11*z + mat.M15*w
|
q.W = mat.M3*x + mat.M7*y + mat.M11*z + mat.M15*w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clamp - Clamp float value
|
||||||
|
func Clamp(value, min, max float32) float32 {
|
||||||
|
var res float32
|
||||||
|
if value < min {
|
||||||
|
res = min
|
||||||
|
} else {
|
||||||
|
res = value
|
||||||
|
}
|
||||||
|
|
||||||
|
if res > max {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue