I don't need 100% accurate fluid for the game I am working on but I would like it fairly realistic. Here is what I got so far.
As you can see this is using libgdx and its version of box2d. What I am doing is creating a bunch of little balls and spawning them in and basiclly using them as water. It works well except that water doesn't "even out" so if you have a U shaped space, water that flows into the left side of the U clogs and backs up and doesn't fill up the right side of the U. No pressure to push it up. Hope that is clear, if not I'll post a screen shot.
This is expected from how the code is written, however what I am wondering is there an easy way to stop this from happening? Basicly a way to let the water settle and push up the other side instead of clogging? My first attempt was to just add a little bit of random force to the balls during render so that they would "jiggle" but this didn't work very well. Any other ideas?
Code: Select all
package com.oberonserver.stranded.model;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.oberonserver.stranded.CollisionMasks;
import com.oberonserver.stranded.ImageCache;
public class Fluid {
private Body myBody;
private Sprite sprite;
private TextureRegion texture;
private long lifeStart;
public static final float SIZE_HEIGHT = 1f;
public static final float SIZE_WIDTH = 1f;
public static final float SCALE=1f;
public static final long LIFE=60000;
public World world;
private String textureName;
public Fluid(World world, float x, float y) {
BodyDef myBodyDef = new BodyDef();
myBodyDef.type = BodyType.DynamicBody;
myBodyDef.position.set(x, y);
myBody = world.createBody(myBodyDef);
CircleShape myShape = new CircleShape();
myShape.setRadius(0.1f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape=myShape;
fixtureDef.density=0f;
fixtureDef.friction=0.2f;
fixtureDef.restitution=0.2f;
fixtureDef.filter.categoryBits=CollisionMasks.CATEGORY_FLUID;
fixtureDef.filter.maskBits=CollisionMasks.MASK_FLUID;
myBody.createFixture(fixtureDef);
myBody.setUserData(this);
sprite = new Sprite(ImageCache.getTexture("waterdrop"));
setTexture("waterdrop");
lifeStart = System.currentTimeMillis();
this.world=world;
}
public void render(SpriteBatch spriteBatch) {
sprite.setTexture(getTexture().getTexture());
sprite.setBounds(myBody.getPosition().x-SIZE_WIDTH/2, myBody.getPosition().y-SIZE_HEIGHT/2, SIZE_WIDTH*SCALE, SIZE_HEIGHT*SCALE);
sprite.draw(spriteBatch);
if (System.currentTimeMillis()-lifeStart > LIFE) { //Destroy it as we have lived long enough
world.destroyBody(myBody);
}
}
public TextureRegion getTexture() {
return texture;
}
public void setTexture(String texture) {
textureName=texture;
this.texture=ImageCache.getTexture(textureName);
}
}
As you can see this is using libgdx and its version of box2d. What I am doing is creating a bunch of little balls and spawning them in and basiclly using them as water. It works well except that water doesn't "even out" so if you have a U shaped space, water that flows into the left side of the U clogs and backs up and doesn't fill up the right side of the U. No pressure to push it up. Hope that is clear, if not I'll post a screen shot.
This is expected from how the code is written, however what I am wondering is there an easy way to stop this from happening? Basicly a way to let the water settle and push up the other side instead of clogging? My first attempt was to just add a little bit of random force to the balls during render so that they would "jiggle" but this didn't work very well. Any other ideas?