Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Help needed with liquidfun faucet — Gideros Forum

Help needed with liquidfun faucet

I wrote this little Excel Macro (attached) which allows converting an excel drawing into a Box2d/liquidfun drawing:




I would like to add a continuous flow of water in the environment, but unfortunately I found that the Box2d/liquidfun version included in Gideros is buggish, as it lacks some file and declarations (for example ParticleEmitter); so the example Faucet does not compile.
So I tried adding manually particles inside .Step() functio, but I don't understand how to use this function.
So I'm stuck. Any help?

Macro creates a testParticles.js file to be replaced to the original file in LFJS folder of BOX2d/Liquidfun distribution.



Likes: MoKaLux, antix

+1 -1 (+2 / -0 )Share on Facebook

Comments

  • hgy29hgy29 Maintainer
    jumpjack said:


    I would like to add a continuous flow of water in the environment, but unfortunately I found that the Box2d/liquidfun version included in Gideros is buggish, as it lacks some file and declarations (for example ParticleEmitter);

    If something is missing in Gideros this doesn't mean it is buggish, rather that no one ever felt the need to add it. The ParticleEmitter you are referring to isn't part of liquidfun API, it is a helper class for tests (it is located in the TestBed folder of liquidfun distribution). If you look at wat it does, it simply emits particles once ina while, using the createParticle call of a ParticleSystem object. This call is implemented in gideros (https://wiki.giderosmobile.com/index.php/B2.ParticleSystem:createParticle) , so you could mimic or recode the particle emitter yourself in lua.

    Likes: Apollo14

    +1 -1 (+1 / -0 )Share on Facebook
  • I don't know LUA, so if I could find a full version of Liquidfun it would be better than starting from scratch learning a new language.
    Do you know how I can fix Liquidfun inside Giderors distribution to get Faucet example working?
  • MoKaLuxMoKaLux Member
    edited August 2019
    @jumpjack take it one step at a time. Do something simple first with liquidfun so you understand the basics. Then add more cool stuff. I don't think liquidfun need fixing in gideros, it works exactly as box2d used to (same functions, …)

    BTW gideros + excel + excel macros = great combo.

    Likes: antix

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    jumpjack said:

    I don't know LUA, so if I could find a full version of Liquidfun it would be better than starting from scratch learning a new language.

    I am confused. Gideros is LUA based, so if you are looking for a liquidfun implementation for another language, I would say that you aren't at the right place here.


  • hgy29 said:

    jumpjack said:

    I don't know LUA, so if I could find a full version of Liquidfun it would be better than starting from scratch learning a new language.

    I am confused. Gideros is LUA based, so if you are looking for a liquidfun implementation for another language, I would say that you aren't at the right place here.


    I am confused too: there is a functional Javascript implementation of Liquidfun inside Gideros distribution. :-)

    Anyway, how would I implement a liquidfun particles emitter in LUA/GIDEROS? Maybe I can do the remaining part (adding walls and water) by myself, but I need some help in adding a continuous flow of water particles.
  • olegoleg Member
    edited August 2019
    @jumpjack
    See examples that come with gideros:


    I understand correctly?- -you want to port the game from JS to gideros?

    @SinisterSoft Somewhere on the forum posted examples with fluid?
    image.png
    515 x 435 - 62K

    Likes: MoKaLux

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+1 / -0 )Share on Facebook
  • oleg said:



    I understand correctly?- -you want to port the game from JS to gideros?

    yes

  • jumpjackjumpjack Member
    edited August 2019
    Found LUA source of the particle example, in this folder:
    gideros-master\samplecode\Physics\Particles (sorry, can't post links yet)

    I attach the LUA file.
    To test it online in the "emulator" you must remove the reference to the texture image:
    ps1:setTexture(Texture.new("Bubble.png"))
    http://giderosmobile.com/code/


    Particles example in action:
    giderosmobile.com/examples#Physics/Particles


    Let's see how complex it is porting a Box2d source from JS to LUA...
    lua
    lua
    main.lua
    1K

    Likes: oleg

    +1 -1 (+1 / -0 )Share on Facebook
  • Ok now I just have to figure out how to make bodies visible...
    http://giderosmobile.com/code/BBuMB8hXPyFX278DYslZsy1wRmqEBP2u

    Apart from this, does anybody know if I can set "buoyancy level", or "weight", or how they call it, of different particles in Box2d?
  • antixantix Member
    Accepted Answer
    @jumpjack To make a basic emitter in Lua you can do something like this...
    require "box2d"
     
    application:setScaleMode("letterbox"); application:setOrientation(application.LANDSCAPE_LEFT); application:setLogicalDimensions(600,800)
     
    local cw, ch = application:getContentWidth(), application:getContentHeight() -- stage bounds
     
    local world = b2.World.new(0, 9.8) -- create world
     
    local ground = world:createBody({}) -- create ground body
    ground:createFixture({shape = b2.EdgeShape.new(0, ch, cw, ch), density = 0}) -- create an edge shape, and attach it to the ground body as a fixture
    ground:createFixture({shape = b2.EdgeShape.new(cw, 0, cw, ch), density = 0})
    ground:createFixture({shape = b2.EdgeShape.new(0, 0, 0, ch), density = 0})
    ground:createFixture({shape = b2.EdgeShape.new(0, 0, cw, 0), density = 0})
     
    local ps1= world:createParticleSystem({radius = 5})
    stage:addChild(ps1)
     
    local emitter = { -- our humble emitter
      frequency   = 0.10, -- how often to emit a particle (in seconds)
      counter     = 0, -- keeping count of when to emit
      particle      = { -- what this emitter emits
        flags     = 0,--FLAG_WATER --+ FLAG_VISCOUS,
        position  = {x = 100, y = 50},
        velocity  = {x = 0.0010, y = -0.0005},
        color     = 0x2266dd,
        alpha     = 1,
      },
    }
     
    local function onEnterFrame(e) -- called every frame
      local dt = e.deltaTime -- time (in ms) since last enterframe event
     
      local counter = emitter.counter + dt -- increase counter
      if counter >= emitter.frequency then -- if counter exceeds threshold..
        local id = ps1:createParticle(emitter.particle) -- emit a waterdrop
        counter = 0 -- reset counter
      end
      emitter.counter = counter -- save counter
     
    	world:step(1/60, 8, 3) -- simulate
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
    An interesting question came to mind while I was coding this.. "how does one remove a waterdrop when it goes off screen or down a plughole"?

    My thought was to have a plughole physics object and any waterdrop that touched it shape would be destroyed. It seems however that particles are not able to partake in such collision detection so in turn it appears that using particles to simulate water wouldn't be viable if you wanted waterdrops to disappear when they went down a plughole.

    Is there any way to make a plughole work with particles or would it only be possible using more traditional box2d objects?

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • isn't a particle automatically removed once it gets off the screen?
  • antix said:

    @jumpjack To make a basic emitter in Lua you can do something like this...

    Thanks for the code, I extended it a little to have a "general purpose faucet":
    require "box2d"
     
    application:setScaleMode("letterbox"); application:setOrientation(application.LANDSCAPE_LEFT); application:setLogicalDimensions(600,800)
     
    local cw, ch = application:getContentWidth(), application:getContentHeight() -- stage bounds
     
    local world = b2.World.new(0, 9.8) -- create world
     
    local ground = world:createBody({}) -- create ground body
    ground:createFixture({shape = b2.EdgeShape.new(0, ch, cw, ch), density = 0}) -- create an edge shape, and attach it to the ground body as a fixture
    ground:createFixture({shape = b2.EdgeShape.new(cw, 0, cw, ch), density = 0})
    ground:createFixture({shape = b2.EdgeShape.new(0, 0, 0, ch), density = 0})
    ground:createFixture({shape = b2.EdgeShape.new(0, 0, cw, 0), density = 0})
     
     
    local ps1= world:createParticleSystem({radius = 2})
    stage:addChild(ps1)
     
    local emitter = { -- our humble emitter
      flow        = 100, -- number of particles emitted per second
      visible     = false, -- for debugging put it to true to see particles while they are created
      counter     = 0, -- keeping count of when to emit
      particle      = { -- what this emitter emits
        flags     = 0,--FLAG_WATER --+ FLAG_VISCOUS,
        position  = {x = 100, y = 50},
        velocity  = {x = 0.0010, y = -0.0005},
        color     = 0x2266dd,
        alpha     = 10,
      },
    }
    local FaucetSize = 20;
    local TLX=emitter.particle.position.x-FaucetSize/2
    local TLY=emitter.particle.position.y-FaucetSize/2
    local BRX=emitter.particle.position.x+FaucetSize/2
    local BRY=emitter.particle.position.y+FaucetSize/2
     
    -- Physical faucet box (not visible):
    -- At least one side must be open to allow water pouring
    ground:createFixture({shape = b2.EdgeShape.new(TLX,TLY,BRX,TLY), density = 0}) -- TOP
    --ground:createFixture({shape = b2.EdgeShape.new(BRX,TLY,BRX,BRY), density = 0}) -- RIGHT
    ground:createFixture({shape = b2.EdgeShape.new(BRX,BRY,TLX,BRY), density = 0}) -- BOTTOM
    ground:createFixture({shape = b2.EdgeShape.new(TLX,BRY,TLX,TLY), density = 0}) -- LEFT
     
    -- Visible faucet box (just graphics):
    local FaucetThickness = 3
    local shape = Shape.new()
    if emitter.visible then
        shape:setFillStyle(Shape.NONE, 0xff0000)
    else
        shape:setFillStyle(Shape.SOLID, 0xff0000)
    end
    shape:setLineStyle(1, 0x0000ff, 1)
    shape:beginPath()
    shape:moveTo(TLX-FaucetThickness,TLY-FaucetThickness)
    shape:lineTo(BRX+FaucetThickness,TLY-FaucetThickness)
    shape:lineTo(BRX+FaucetThickness,BRY+FaucetThickness)
    shape:lineTo(TLX-FaucetThickness,BRY+FaucetThickness)
    shape:lineTo(TLX-FaucetThickness,TLY-FaucetThickness)
    shape:endPath()
    shape:setPosition(0,0)
    stage:addChild(shape)
     
     
    local function onEnterFrame(e) -- called every frame
      local dt = e.deltaTime -- time (in ms) since last enterframe event
     
      local counter = emitter.counter + dt -- increase counter
      if counter >= 1/emitter.flow then -- if counter exceeds threshold..
        local id = ps1:createParticle(emitter.particle) -- emit a waterdrop
        counter = 0 -- reset counter
      end
      emitter.counter = counter -- save counter
     
    	world:step(1/60, 8, 3) -- simulate
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

    +1 -1 (+3 / -0 )Share on Facebook
  • wow btw one interesting liquidfun game came to my youtube recommendations:

    Likes: MoKaLux

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.