Skip to content

Commit

Permalink
Merge pull request #6 from bugrahana/master
Browse files Browse the repository at this point in the history
Fixed examples syntax errors
  • Loading branch information
rudransh61 authored Jun 2, 2024
2 parents fa0611e + 02a4d5b commit 6491ea3
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 271 deletions.
10 changes: 5 additions & 5 deletions examples/ex10.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"image/color"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/pkg/polygon"
"github.com/rudransh61/Physix-go/pkg/vector"
"github.com/rudransh61/Physix-go/dynamics/physics"
"image/color"
)

var (
Expand All @@ -17,7 +17,7 @@ var (
func update() error {
// Apply a force to simulate gravity
gravity := vector.Vector{X: 0, Y: 10}
github.com/rudransh61/Physix-go.ApplyForcePolygon(ball, gravity, dt)
physix.ApplyForcePolygon(ball, gravity, dt)

// Bounce off the walls
if ball.Position.X < 0 || ball.Position.X > 400 {
Expand All @@ -37,8 +37,8 @@ func draw(screen *ebiten.Image) {
ebitenutil.DrawRect(screen, v.X, v.Y, 10, 10, color.RGBA{R: 0xff, G: 0, B: 0, A: 0xff})
}
for _, v1 := range ball.Vertices {
for _, v2 := range ball.Vertices{
ebitenutil.DrawLine(screen, v1.X, v1.Y, v2.X, v2.Y , color.RGBA{R: 0, G: 0xff, B: 0, A: 0xff} )
for _, v2 := range ball.Vertices {
ebitenutil.DrawLine(screen, v1.X, v1.Y, v2.X, v2.Y, color.RGBA{R: 0, G: 0xff, B: 0, A: 0xff})
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions examples/ex11.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"github.com/rudransh61/Physix-go/dynamics/collision"
"github.com/rudransh61/Physix-go/pkg/polygon"
"github.com/rudransh61/Physix-go/pkg/vector"
"github.com/rudransh61/Physix-go/dynamics/collision"
"fmt"
// "github.com/rudransh61/Physix-go/dynamics/physics"
)

Expand All @@ -22,21 +22,21 @@ func main() {
Vertices: []vector.Vector{{X: 250, Y: 500}, {X: 250, Y: 300}, {X: 250, Y: 250}, {X: 200, Y: 250}},
}
// Check if the polygons are colliding
if collision.PolygonCollision(polygon1, polygon2) {
if collision.PolygonCollision(&polygon1, &polygon2) {
fmt.Println("Polygons 1 2 are colliding")
} else {
fmt.Println("Polygons 1 2 are not colliding")
}
// Check if the polygons are colliding
if collision.PolygonCollision(polygon1, polygon3) {
if collision.PolygonCollision(&polygon1, &polygon3) {
fmt.Println("Polygons 1 3 are colliding")
} else {
fmt.Println("Polygons 1 3 are not colliding")
}
// Check if the polygons are colliding
if collision.PolygonCollision(polygon2, polygon3) {
if collision.PolygonCollision(&polygon2, &polygon3) {
fmt.Println("Polygons 2 3 are colliding")
} else {
fmt.Println("Polygons 2 3 are not colliding")
}
}
}
39 changes: 18 additions & 21 deletions examples/ex12.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
package main

import (
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"image/color"
"github.com/rudransh61/Physix-go/dynamics/collision"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/pkg/polygon"
"github.com/rudransh61/Physix-go/pkg/vector"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/dynamics/collision"
"fmt"
"image/color"
)

var (
ball *polygon.Polygon
ball *polygon.Polygon
ball2 *polygon.Polygon
dt = 0.01
dt = 0.01
)


func update() error {
// Apply a force to simulate gravity
gravity := vector.Vector{X: 0, Y: 10}
github.com/rudransh61/Physix-go.ApplyForcePolygon(ball, gravity, dt)
github.com/rudransh61/Physix-go.ApplyForcePolygon(ball2, gravity, dt)
physix.ApplyForcePolygon(ball, gravity, dt)
physix.ApplyForcePolygon(ball2, gravity, dt)

if ball.Position.Y < 0 || ball.Position.Y > 400 {
ball.Velocity.Y = -10
}

// If colliding, handle collision response
if collision.PolygonCollision(ball, ball2) {
fmt.Println("collision")
// Resolve collision
collision.ResolveCollision(ball, ball2,0.995,0)
}

if collision.PolygonCollision(ball, ball2) {
fmt.Println("collision")
// Resolve collision
collision.ResolveCollision(ball, ball2, 0.995, 0)
}

// Update positions based on velocities
ball.UpdatePosition()
Expand All @@ -43,25 +41,24 @@ if collision.PolygonCollision(ball, ball2) {
return nil
}


func draw(screen *ebiten.Image) {
// Draw the ball using the github.com/rudransh61/Physix-go engine's position
// calculateCentroid calculates the centroid of a polygon given its vertices.
for _, v := range ball.Vertices {
ebitenutil.DrawRect(screen, v.X, v.Y, 10, 10, color.RGBA{R: 0xff, G: 0, B: 0, A: 0xff})
}
for _, v1 := range ball.Vertices {
for _, v2 := range ball.Vertices{
ebitenutil.DrawLine(screen, v1.X, v1.Y, v2.X, v2.Y , color.RGBA{R: 0xff, G: 0, B: 0, A: 0xff} )
for _, v2 := range ball.Vertices {
ebitenutil.DrawLine(screen, v1.X, v1.Y, v2.X, v2.Y, color.RGBA{R: 0xff, G: 0, B: 0, A: 0xff})
}
}
// ball2
for _, v := range ball2.Vertices {
ebitenutil.DrawRect(screen, v.X, v.Y, 10, 10, color.RGBA{R: 0, G: 0, B: 0xff, A: 0xff})
}
for _, v1 := range ball2.Vertices {
for _, v2 := range ball2.Vertices{
ebitenutil.DrawLine(screen, v1.X, v1.Y, v2.X, v2.Y , color.RGBA{R: 0, G: 0, B: 0xff, A: 0xff} )
for _, v2 := range ball2.Vertices {
ebitenutil.DrawLine(screen, v1.X, v1.Y, v2.X, v2.Y, color.RGBA{R: 0, G: 0, B: 0xff, A: 0xff})
}
}

Expand All @@ -77,7 +74,7 @@ func main() {

// Initialize a rigid body with your github.com/rudransh61/Physix-go engine
ball = polygon.NewPolygon(vertices, 50, true)
ball2 = polygon.NewPolygon( []vector.Vector{{X: 100, Y: 50}, {X: 50, Y: 100}, {X: 50, Y: 50}, {X: 200, Y: 200}}, 50, true)
ball2 = polygon.NewPolygon([]vector.Vector{{X: 100, Y: 50}, {X: 50, Y: 100}, {X: 50, Y: 50}, {X: 200, Y: 200}}, 50, true)
ball2.Velocity.X = 100
// Run the game loop
if err := ebiten.RunGame(&Game{}); err != nil {
Expand Down
15 changes: 7 additions & 8 deletions examples/ex13.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"image/color"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/pkg/rigidbody"
"github.com/rudransh61/Physix-go/pkg/vector"
"image/color"
// "log"
)

Expand All @@ -18,10 +18,9 @@ var (
func update() error {
ball.ApplyTorque(1)
// Update the github.com/rudransh61/Physix-go simulation
github.com/rudransh61/Physix-go.ApplyForce(ball, ball.Force, dt)
physix.ApplyForce(ball, ball.Force, dt)
// github.com/rudransh61/Physix-go.UpdateRotation(ball,dt)


return nil
}

Expand All @@ -37,11 +36,11 @@ func main() {

// Initialize a rigid body with your github.com/rudransh61/Physix-go engine
ball = &rigidbody.RigidBody{
Position: vector.Vector{X: 100, Y: 400},
Velocity: vector.Vector{X: 30, Y: -20},
Mass: 1,
Force : vector.Vector{X: 0, Y: 0},
IsMovable : true,
Position: vector.Vector{X: 100, Y: 400},
Velocity: vector.Vector{X: 30, Y: -20},
Mass: 1,
Force: vector.Vector{X: 0, Y: 0},
IsMovable: true,
}

// Run the game loop
Expand Down
43 changes: 21 additions & 22 deletions examples/ex15.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"image/color"
"github.com/rudransh61/Physix-go/dynamics/collision"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/pkg/rigidbody"
"github.com/rudransh61/Physix-go/pkg/vector"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/dynamics/collision"
"image/color"
"math"
"math/rand"
// "fmt"
Expand All @@ -19,39 +19,39 @@ var (
)

const (
Mass = 2
Shape = "Circle"
Radius = 10
Mass = 2
Shape = "Circle"
Radius = 10
)

func checkwall(ball *rigidbody.RigidBody) {
// Bounce off the walls
if ball.Position.X < 100+ball.Radius || ball.Position.X > 600-ball.Radius {
if ball.Position.X < 100+ball.Radius {
ball.Velocity.X = math.Abs(ball.Velocity.X*0.7)
ball.Position.X = 100+ball.Radius
ball.Velocity.X = math.Abs(ball.Velocity.X * 0.7)
ball.Position.X = 100 + ball.Radius
}
if ball.Position.X > 600-ball.Radius {
ball.Velocity.X = -0.7 * math.Abs(ball.Velocity.X)
ball.Position.X = 600-ball.Radius
ball.Position.X = 600 - ball.Radius
}
}
if ball.Position.Y < 100+ball.Radius || ball.Position.Y > 600-ball.Radius {
if ball.Position.Y < 100+ball.Radius {
ball.Velocity.Y = math.Abs(ball.Velocity.Y*0.7)
ball.Position.Y = 100+ball.Radius
ball.Velocity.Y = math.Abs(ball.Velocity.Y * 0.7)
ball.Position.Y = 100 + ball.Radius
}
if ball.Position.Y > 600-ball.Radius {
ball.Velocity.Y = -0.7 * math.Abs(ball.Velocity.Y)
ball.Position.Y = 600-ball.Radius
ball.Position.Y = 600 - ball.Radius
}
}
}

func update() error {
gravity := vector.Vector{X: 0, Y: 15}
for _, ball := range balls {
github.com/rudransh61/Physix-go.ApplyForce(ball, gravity, dt)
physix.ApplyForce(ball, gravity, dt)
checkwall(ball)
}

Expand Down Expand Up @@ -83,25 +83,24 @@ func resolveCollision(ball1, ball2 *rigidbody.RigidBody) {
// Calculate the amount by which to move the balls apart
moveAmount := minimumDistance - distanceMagnitude
// Calculate the movement vectors for each ball
moveVector1 := moveDirection.Scale(moveAmount/2)
moveVector2 := moveDirection.Scale(-moveAmount/2)
moveVector1 := moveDirection.Scale(moveAmount / 2)
moveVector2 := moveDirection.Scale(-moveAmount / 2)
// Move the balls apart
ball1.Position = ball1.Position.Add(moveVector1)
ball2.Position = ball2.Position.Add(moveVector2)
}
}


func draw(screen *ebiten.Image) {
for _, ball := range balls {
ebitenutil.DrawCircle(screen, ball.Position.X, ball.Position.Y, ball.Radius, color.RGBA{R: 0xff, G: 0, B: 0, A: 0xff})
}

//Boundary
ebitenutil.DrawRect(screen, 600.0, 100.0, 10, 500, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // right
ebitenutil.DrawRect(screen, 90.0, 100.0, 10, 500, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // left
ebitenutil.DrawRect(screen, 90.0, 100.0, 510, 10, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // up
ebitenutil.DrawRect(screen, 90.0, 600.0, 510, 10, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // down
ebitenutil.DrawRect(screen, 600.0, 100.0, 10, 500, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // right
ebitenutil.DrawRect(screen, 90.0, 100.0, 10, 500, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // left
ebitenutil.DrawRect(screen, 90.0, 100.0, 510, 10, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // up
ebitenutil.DrawRect(screen, 90.0, 600.0, 510, 10, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // down
}

func main() {
Expand All @@ -124,8 +123,8 @@ func initializeBalls(n int) {
balls = make([]*rigidbody.RigidBody, n)
for i := 0; i < n; i++ {
balls[i] = &rigidbody.RigidBody{
Position: vector.Vector{X: float64(rand.Intn(200)+200), Y: float64(rand.Intn(200)+200)},
Velocity: vector.Vector{X: float64(rand.Intn(20)) , Y: float64(rand.Intn(20))},
Position: vector.Vector{X: float64(rand.Intn(200) + 200), Y: float64(rand.Intn(200) + 200)},
Velocity: vector.Vector{X: float64(rand.Intn(20)), Y: float64(rand.Intn(20))},
Mass: Mass,
Shape: Shape,
Radius: Radius,
Expand Down
Loading

0 comments on commit 6491ea3

Please sign in to comment.