Created
November 14, 2013 02:18
-
-
Save jrenner/7460218 to your computer and use it in GitHub Desktop.
box2d working properly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.libgdx.skeleton; | |
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.math.Matrix4; | |
import com.badlogic.gdx.math.Rectangle; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.*; | |
public class SkeletonMain implements ApplicationListener { | |
World world ; | |
Box2DDebugRenderer debugRenderer; | |
OrthographicCamera camera; | |
static final float BOX_STEP=1/60f; | |
static final int BOX_VELOCITY_ITERATIONS=8; | |
static final int BOX_POSITION_ITERATIONS=3; | |
static final float WORLD_TO_BOX=0.01f; | |
static final float BOX_WORLD_TO=100f; | |
Rectangle ball; | |
Body body; | |
@Override | |
public void create() { | |
world = new World(new Vector2(0, -100), true); | |
camera = new OrthographicCamera(); | |
camera.viewportHeight = 480; | |
camera.viewportWidth = 800; | |
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f); | |
camera.zoom = 1f; | |
camera.update(); | |
//Ground body | |
BodyDef groundBodyDef =new BodyDef(); | |
groundBodyDef.position.set(new Vector2(0, 10 * WORLD_TO_BOX)); | |
Body groundBody = world.createBody(groundBodyDef); | |
PolygonShape groundBox = new PolygonShape(); | |
float w = (camera.viewportWidth * 2) * WORLD_TO_BOX; | |
float h = 10.0f * WORLD_TO_BOX; | |
groundBox.setAsBox(w, h); | |
groundBody.createFixture(groundBox, 0.0f); | |
//Dynamic Body | |
BodyDef bodyDef = new BodyDef(); | |
bodyDef.type = BodyDef.BodyType.DynamicBody; | |
float posX = (camera.viewportWidth / 8) * WORLD_TO_BOX; | |
float posY = (camera.viewportHeight / 2) * WORLD_TO_BOX; | |
bodyDef.position.set(posX, posY); | |
body = world.createBody(bodyDef); | |
// create a Rectangle to logically represent the ball | |
CircleShape dynamicCircle = new CircleShape(); | |
float radius = 40f * WORLD_TO_BOX; | |
dynamicCircle.setRadius(radius); | |
FixtureDef fixtureDef = new FixtureDef(); | |
fixtureDef.shape = dynamicCircle; | |
fixtureDef.density = 1.0f; | |
fixtureDef.friction = 0.0f; | |
fixtureDef.restitution = 0.7f; | |
body.createFixture(fixtureDef); | |
debugRenderer = new Box2DDebugRenderer(); | |
listener listener1=new listener(); | |
world.setContactListener(listener1); | |
} | |
private static Matrix4 debugProjection = new Matrix4(); | |
@Override | |
public void render() { | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); | |
debugProjection.set(camera.combined); | |
debugProjection.scl(BOX_WORLD_TO); | |
debugRenderer.render(world, debugProjection); | |
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS); | |
} | |
class listener implements ContactListener | |
{ | |
@Override | |
public void beginContact(Contact contact) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void endContact(Contact contact) { | |
// TODO Auto-generated method stub | |
body.applyLinearImpulse(0, 2f, body.getWorldCenter().x, body.getWorldCenter().y, true); | |
//body.setLinearVelocity(0,50000000); | |
System.out.println("Collision detected"); | |
} | |
@Override | |
public void preSolve(Contact contact, Manifold oldManifold) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void postSolve(Contact contact, ContactImpulse impulse) { | |
// TODO Auto-generated method stub | |
} | |
}; | |
@Override | |
public void resize(int width, int height) { | |
} | |
@Override | |
public void pause() { | |
} | |
@Override | |
public void resume() { | |
} | |
@Override | |
public void dispose() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment