78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
|
|
package elements
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fluids/fluid"
|
||
|
|
"image/color"
|
||
|
|
|
||
|
|
"github.com/hajimehoshi/ebiten/v2"
|
||
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
FS10PixelWidth = 200
|
||
|
|
FS10PixelHeight = 100
|
||
|
|
FS10FluidWidth = 3.0 //meters
|
||
|
|
FS10FluidHeight = 1.0 //meters
|
||
|
|
FS10Resolution = 20 //need to workshop this
|
||
|
|
)
|
||
|
|
|
||
|
|
type FluidSim10 struct {
|
||
|
|
MappedEntityBase
|
||
|
|
fluid *fluid.Fluid
|
||
|
|
angle float64
|
||
|
|
particlebuff *ebiten.Image
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewFluidSim10() *FluidSim10 {
|
||
|
|
fsim := &FluidSim10{
|
||
|
|
fluid: fluid.NewFluid(fluid.FieldVector{X: FS10FluidWidth, Y: FS10FluidHeight}, FS10FluidHeight/FS10Resolution),
|
||
|
|
particlebuff: ebiten.NewImage(1, 1),
|
||
|
|
}
|
||
|
|
fsim.Initialize()
|
||
|
|
return fsim
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *FluidSim10) Initialize() {
|
||
|
|
f.Sprite = ebiten.NewImage(FS10PixelWidth, FS10PixelHeight)
|
||
|
|
f.particlebuff.Fill(color.White)
|
||
|
|
f.fluid.Initialize()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *FluidSim10) SetAngle(angle float64) {
|
||
|
|
f.angle = angle
|
||
|
|
f.fluid.SetAngle(float32(angle))
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *FluidSim10) GetAngle() float64 {
|
||
|
|
return f.angle
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *FluidSim10) Draw() {
|
||
|
|
f.Sprite.Clear()
|
||
|
|
|
||
|
|
vector.StrokeRect(f.Sprite, 0, 0, FS10PixelWidth, FS10PixelHeight, 2, color.White, true)
|
||
|
|
|
||
|
|
for i := range f.fluid.Particles {
|
||
|
|
//for each particle, compute its relative position based on its
|
||
|
|
//position within the fluid field
|
||
|
|
p := &f.fluid.Particles[i]
|
||
|
|
percentx := p.Position.X / f.fluid.Field.Dimensions.X
|
||
|
|
percenty := p.Position.Y / f.fluid.Field.Dimensions.Y
|
||
|
|
ox := float64(percentx * FS10PixelWidth)
|
||
|
|
oy := float64(percenty * FS10PixelHeight)
|
||
|
|
|
||
|
|
op := &ebiten.DrawImageOptions{}
|
||
|
|
op.GeoM.Translate(ox, oy)
|
||
|
|
f.Sprite.DrawImage(f.particlebuff, op)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (f *FluidSim10) Update() {
|
||
|
|
|
||
|
|
if f.paused {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
f.fluid.Step()
|
||
|
|
}
|