Single Tech Games

prototype7 single tech games

Libgdx y Box2D, parte 1


package single.tech.games.box2d;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
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.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
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;
public class box2dPrueba implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
//'''''''''Variables de Box 2D''''''''''''''''
private World mundo;
static final float MUNDO_A_BOX=0.01f;
static final float BOX_DEL_MUNDO=100f;
protected Body cuerpo;
static final float BOX_PASO=1/120f;
static final int  BOX_VELOCIDAD_ITERACIONES=8;
static final int BOX_POSICION_ITERACIONES=3;
private float acumulador;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
//'''''''''''Codigo Box2D'''''''''''''''''''
mundo=new World(new Vector2(0, -10), true);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
actualizar(Gdx.graphics.getDeltaTime());
dibujar(Gdx.graphics.getDeltaTime());
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
//''''''''''''Clases privadas''''''''''''''''
private void actualizar(float tiempoDelta) {
acumulador+=tiempoDelta;
while(acumulador>BOX_PASO){
mundo.step(BOX_PASO,BOX_VELOCIDAD_ITERACIONES,BOX_POSICION_ITERACIONES);
acumulador-=BOX_PASO;
}
}
private void dibujar(float tiempoDelta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
private float convertirABox(float x){
return x*MUNDO_A_BOX;
}
private void crearCuerpo(World mundo,Vector2 pos,float angulo){
BodyDef defCuerpo = new BodyDef();
defCuerpo.type = BodyType.DynamicBody;;
defCuerpo.position.set(convertirABox(pos.x),convertirABox(pos.y));
defCuerpo.angle=angulo;
cuerpo = mundo.createBody(defCuerpo);
}
private void hacerFixtureRectangular(float ancho,float alto,BodyDef.BodyType tipoCuerpo,
float densidad,float restitucion, Vector2 pos,float angulo){
PolygonShape formaCuerpo = new PolygonShape();
float w=convertirABox(ancho/2f);
float h=convertirABox(alto/2f);
formaCuerpo.setAsBox(w,h);
FixtureDef defFixture=new FixtureDef();
defFixture.density=densidad;
defFixture.restitution=restitucion;
defFixture.shape=formaCuerpo;
cuerpo.createFixture(defFixture);
formaCuerpo.dispose();
}
private void hacerFixtureCircular(float radio,BodyDef.BodyType tipoCuerpo,
float densidad,float restitucion, Vector2 pos,float angulo){
FixtureDef defFixture=new FixtureDef();
defFixture.density=densidad;
defFixture.restitution=restitucion;
defFixture.shape=new CircleShape();
defFixture.shape.setRadius(5.0f);
cuerpo.createFixture(defFixture);
defFixture.shape.dispose();
}
}


Páginas: 1 2

0 0 votes
Article Rating
Subscribe
Notify of
guest
5 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
YaYo Arellano

Otra pagina muy interesante para aprender box2d y totalmente en español
http://box2despanol.blogspot.mx/

[…] y lo malogro, no aprendo nada en programación, así que utilizando lo que aprendimos en el primer tutorial y mirando en la web, podemos crear el más basico de todos los ejemplos, una pelota […]

Atlantis Road

Esta información es muy útil para los que estamos empezando a desarrollar en Android…gracias 🙂