Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
current state
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryBrownEEngr committed Dec 8, 2023
1 parent dabd266 commit 71b009c
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 56 deletions.
7 changes: 0 additions & 7 deletions examples/marblemachine/imageBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,15 @@ func createRectangleImage(width, height float64, c color.Color) image.Image {

func createSegmentImage(width, height float64, c color.Color) image.Image {
hw := width / 2
// hh := height / 2

dc := gg.NewContext(int(width), int(height))
dc.SetColor(c)

dc.MoveTo(0, hw)
dc.DrawArc(hw, hw, hw, 1.0*math.Pi, 2*math.Pi)
// dc.LineTo(width, hw)
dc.LineTo(width, height-hw)
dc.DrawArc(hw, height-hw, hw, 0*math.Pi, 1*math.Pi)
// dc.LineTo(0, height-hw)
dc.LineTo(0, hw)
//
// dc.DrawLine(width, height-hw, width, hw)
// dc.DrawArc(hw, hw, hw, 0*math.Pi, -1*math.Pi)
// dc.ClosePath()
dc.FillPreserve()
dc.Stroke()

Expand Down
92 changes: 45 additions & 47 deletions examples/marblemachine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func main() {
type object struct {
sprite models.Sprite
body *cp.Body
shape *cp.Shape
}

/*
A Body appears to be what has mass, momentum,
A Body appears to be what has mass, momentum, and moment of inertia.
A Shape is what touches other things, and is attached to a body.
A shape can be made out of segments, a circle, or a rectangle.
Expand All @@ -39,32 +40,20 @@ func simStartFunc(sim models.Scratch) {
space := cp.NewSpace()
space.SetGravity(cp.Vector{X: 0, Y: -600})

oList = append(oList, AddContainer(sim, space))
_ = AddMarbleTrack(sim, space)

// oList = append(oList, AddContainer(sim, space))

mass := 1.0
width := 30.0
height := width * 2

for i := 0; i < 7; i++ {
for j := 0; j < 3; j++ {
pos := cp.Vector{X: float64(i) * width, Y: float64(j) * height}

typ := rand.Intn(3)
if typ == 0 {
a := NewBox(sim, space, pos, mass, width, height)
oList = append(oList, a)
} else if typ == 1 {
a := AddSegment(sim, space, pos, mass, width, height)
oList = append(oList, a)
} else {
a := NewCircle(sim, space, pos.Add(cp.Vector{X: 0, Y: (height - width) / 2}), mass, width/2)
b := NewCircle(sim, space, pos.Add(cp.Vector{X: 0, Y: (width - height) / 2}), mass, width/2)
oList = append(oList, a, b)

}
}
// height := width * 2

for i := 0; i < 1000; i++ {
a := NewCircle(sim, space, cp.Vector{X: (rand.Float64()*2 - 1) * 300, Y: rand.Float64() * 300}, mass, width/2)
oList = append(oList, a)
}

// Run the processing loop forever.
for {
time.Sleep(time.Millisecond)
space.Step(.010)
Expand All @@ -73,13 +62,11 @@ func simStartFunc(sim models.Scratch) {
p := o.body.Position()
o.sprite.Pos(p.X, p.Y)
o.sprite.Angle(180.0 / math.Pi * o.body.Angle())

}
}

}

func AddContainer(sim models.Scratch, space *cp.Space) *object {
func AddMarbleElevator(sim models.Scratch, space *cp.Space) *object {
container := space.AddBody(cp.NewKinematicBody())
container.SetAngularVelocity(0.4)
container.SetPosition(cp.Vector{X: 0, Y: 0})
Expand All @@ -89,6 +76,13 @@ func AddContainer(sim models.Scratch, space *cp.Space) *object {
c := cp.Vector{X: 200, Y: 200}
d := cp.Vector{X: 200, Y: -200}

AddWall := func(space *cp.Space, body *cp.Body, a, b cp.Vector, radius float64) {
shape := cp.NewSegment(body, a, b, radius)
_ = space.AddShape(shape)
shape.SetElasticity(1)
shape.SetFriction(1)
}

AddWall(space, container, a, b, 1)
AddWall(space, container, b, c, 1)
AddWall(space, container, c, d, 1)
Expand All @@ -111,16 +105,19 @@ func AddContainer(sim models.Scratch, space *cp.Space) *object {
return ret
}

func AddWall(space *cp.Space, body *cp.Body, a, b cp.Vector, radius float64) {
// swap so we always draw the same direction horizontally
if a.X < b.X {
a, b = b, a
}
func AddMarbleTrack(sim models.Scratch, space *cp.Space) []*object {
oList := []*object{}

seg := cp.NewSegment(body, a, b, radius).Class.(*cp.Segment)
shape := space.AddShape(seg.Shape)
shape.SetElasticity(1)
shape.SetFriction(1)
o := NewStaticBox(sim, space, cp.Vector{X: 0, Y: -490}, 990, 4)
oList = append(oList, o)
o = NewStaticBox(sim, space, cp.Vector{X: -490, Y: 0}, 4, 990)
oList = append(oList, o)
o = NewStaticBox(sim, space, cp.Vector{X: 490, Y: 0}, 4, 990)
oList = append(oList, o)
o = NewStaticBox(sim, space, cp.Vector{X: 0, Y: 490}, 990, 4)
oList = append(oList, o)

return oList
}

func NewBox(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, width, height float64) *object {
Expand All @@ -144,23 +141,24 @@ func NewBox(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, width, hei
ret := &object{
sprite: sprite,
body: body,
shape: shape,
}
return ret
}

func NewCircle(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, radius float64) *object {
body := cp.NewBody(mass, cp.MomentForCircle(mass, 0, radius, cp.Vector{}))
func NewStaticBox(sim models.Scratch, space *cp.Space, pos cp.Vector, width, height float64) *object {
body := cp.NewStaticBody()
_ = space.AddBody(body)
body.SetPosition(pos)

shape := cp.NewCircle(body, radius, cp.Vector{})
shape := cp.NewBox(body, width, height, 0)
_ = space.AddShape(shape)
shape.SetElasticity(0)
shape.SetFriction(0.7)
shape.SetElasticity(1.0)
shape.SetFriction(1.0)

c := color.RGBA{R: uint8(rand.Intn(256)), G: uint8(rand.Intn(256)), B: uint8(rand.Intn(256)), A: 0xFF}
costumeName := fmt.Sprintf("%X", rand.Uint64())
sim.AddCostume(createCircleImage(radius, c), costumeName)
costumeName := fmt.Sprintf("NewStaticBox:%X", rand.Uint64())
sim.AddCostume(createRectangleImage(width, height, c), costumeName)
sprite := sim.AddSprite("")
sprite.Costume(costumeName)
sprite.Pos(pos.X, pos.Y)
Expand All @@ -169,25 +167,24 @@ func NewCircle(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, radius
ret := &object{
sprite: sprite,
body: body,
shape: shape,
}
return ret
}

func AddSegment(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, width, height float64) *object {
body := cp.NewBody(mass, cp.MomentForBox(mass, width, height))
func NewCircle(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, radius float64) *object {
body := cp.NewBody(mass, cp.MomentForCircle(mass, 0, radius, cp.Vector{}))
_ = space.AddBody(body)
body.SetPosition(pos)

a, b := cp.Vector{X: 0, Y: (height - width) / 2.0}, cp.Vector{X: 0, Y: (width - height) / 2.0}
shape := cp.NewSegment(body, a, b, width/2.0)
shape := cp.NewCircle(body, radius, cp.Vector{})
_ = space.AddShape(shape)
shape.SetElasticity(0)
shape.SetFriction(0.7)

c := color.RGBA{R: uint8(rand.Intn(256)), G: uint8(rand.Intn(256)), B: uint8(rand.Intn(256)), A: 0xFF}
costumeName := fmt.Sprintf("%X", rand.Uint64())
sim.AddCostume(createSegmentImage(width, height, c), costumeName)
// sim.AddCostume(createRectangleImage(width, height, c), costumeName)
sim.AddCostume(createCircleImage(radius, c), costumeName)
sprite := sim.AddSprite("")
sprite.Costume(costumeName)
sprite.Pos(pos.X, pos.Y)
Expand All @@ -196,6 +193,7 @@ func AddSegment(sim models.Scratch, space *cp.Space, pos cp.Vector, mass, width,
ret := &object{
sprite: sprite,
body: body,
shape: shape,
}
return ret
}

0 comments on commit 71b009c

Please sign in to comment.