This commit is contained in:
Konstantin8105 2022-11-22 18:50:35 +03:00
parent 0bec81c656
commit fe6d2c0ed3
190 changed files with 104835 additions and 5 deletions

View 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()
}

View 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()
}

View 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()
}

View 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()
}

View 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()
}