harpoon as the pang game in libgdx - android

I am using Scene2D.
This is my Shoot Actor
public class ActorDisparo extends Actor {
private TextureRegion textureDisparo = new TextureRegion();
private World world;
public static Body body;
//private Fixture fixture;
private FixtureDef fdef;
private String userData;
private float anchoDisparo, altoDisparo;
private float positionX,positionY;
private static float porcentajeDeancho = 0,anchoMediano = 0, anchoPequeno = 0, anchoDiminuto = 0;
private TextureRegion arponRegion = new TextureRegion();
public ActorDisparo(World world, TextureRegion disparo, Vector2 position, float anchoDisparo, float altoDisparo, float fuerzaX, float fuerzaY, String userData){
this.textureDisparo = disparo;
this.world = world;
this.anchoDisparo = anchoDisparo;
this.altoDisparo = altoDisparo;
this.userData = userData;
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(position);
bodyDef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bodyDef);
PolygonShape shapeDisparo = new PolygonShape();
shapeDisparo.setAsBox((anchoDisparo/2)/2,altoDisparo/2);
fdef = new FixtureDef();
fdef.shape = shapeDisparo;
//fdef.restitution = 0.70f;
fdef.filter.categoryBits = BolasGame.DISPARO_BIT;
fdef.filter.maskBits = BolasGame.BOLAROJA_BIT |
BolasGame.TECHO_BIT;
body.createFixture(fdef).setUserData(userData);
shapeDisparo.dispose();
body.setGravityScale(0f);
body.applyLinearImpulse(fuerzaX,fuerzaY,position.x,position.y,true);
setSize( anchoDisparo *BolasGame.MetrosAPixels ,altoDisparo*BolasGame.MetrosAPixels);
//setBounds(0, 0, getWidth(), getHeight());
}
public float getPositionX() {
return getPositionX();
}
public float getPositionY() {
return getPositionY();
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
setPosition( (body.getPosition().x * BolasGame.MetrosAPixels) - getWidth()/2, (body.getPosition().y * BolasGame.MetrosAPixels) - getHeight()/2);
batch.draw(textureDisparo,getX(),getY(),getWidth(),getHeight());
}
public void detach(){
world.destroyBody(body);
remove();
}
}
and this is the call to shoot class
if(Gdx.input.justTouched() && hasdisparado == false){
hasdisparado =true;
touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
stage.getCamera().unproject(touch);
arponRegion = new TextureRegion(textureDisparo, 0, 0, 20, 40);
actorDisparo = new ActorDisparo(world,arponRegion,new Vector2(touch.x/BolasGame.MetrosAPixels,touch.y/BolasGame.MetrosAPixels ),20/BolasGame.MetrosAPixels,40/BolasGame.MetrosAPixels,0,2f,BolasGame.USERDATA_DISPARO);
stage.addActor(actorDisparo);
}
this creates a shot like a bullet but I need since I touched the screen until it is destroyed is continuous like the harpoon of game pang
the textureregion the charge of a texture whose dimensions are 446x20
textureDisparo = new Texture("gfx/disparo.png");
I have no idea how to do this because you have to repeat the image and body in box2d for collisions.
I totally lost
if someone can give me an idea would appreciate
Thank you
(Sorry for my English, I'm learning now)

Related

Capture touch events on 3D model in LibGDX

public static class GameObject extends ModelInstance {
public final Vector3 center = new Vector3();
public final Vector3 dimensions = new Vector3();
public final float radius;
private final static BoundingBox bounds = new BoundingBox();
public GameObject (Model model, String rootNode, boolean mergeTransform)
{
super(model, rootNode, mergeTransform);
calculateBoundingBox(bounds);
bounds.getCenter(center);
bounds.getDimensions(dimensions);
radius = dimensions.len() / 2f;
}
}
#Override
public void create()
{
camera = new PerspectiveCamera(150,Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
camera.position.set(0f, 0f, 0f);
camera.lookAt(0f, 0f, 0);
camera.near = 0f;
camera.far = 100f;
camera.update();
modelBatch = new ModelBatch();
UBJsonReader jsonReader = new UBJsonReader();
G3dModelLoader modelLoader = new G3dModelLoader(jsonReader);
model = modelLoader.loadModel(Gdx.files.getFileHandle("map.g3db", Files.FileType.Internal));
modelInstance = new ModelInstance(model);
GameObject object = null;
for (int i = 0; i < modelInstance.nodes.size; i++)
{
object = new GameObject(model, modelInstance.nodes.get(i).id, true);
countries.add(object);
System.out.println(object.center);
}
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
countryIsTouched(screenX, screenY);
// TODO Auto-generated method stub
return false;
}
public void countryIsTouched(int screenX, int screenY)
{
Ray ray = camera.getPickRay(screenX, screenY);
modelInstance.transform.getTranslation(position);
float distance = -1;
int result = -1;
for (int i = 0; i < countries.size; ++i)
{
final GameObject instance = countries.get(i);
instance.transform.getTranslation(position);
position.add(instance.center);
final float len = ray.direction.dot(position.x-ray.origin.x, position.y-ray.origin.y, position.z-ray.origin.z);
if (len < 0f)
continue;
float dist2 = position.dst2(ray.origin.x+ray.direction.x*len, ray.origin.y+ray.direction.y*len, ray.origin.z+ray.direction.z*len);
if (distance >= 0f && dist2 > distance)
continue;
if (dist2 <= instance.radius * instance.radius) {
result = i;
distance = dist2;
}
}
}
Thanks for any help or guidance!
Does the camera properties have any impact on this since the ray picking never detect any touch?

My box2d body drops way too slow

I tried almost everything but nothing works.
If you run it you will see a square falling down very slow.
I'm a beginner so explain it not too complicated.
Any questions about this code dylan.missu#gmail.com
Here is my code:
#Override
public void show() {
camera = new OrthographicCamera();
debugRenderer = new Box2DDebugRenderer();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(100,100);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(Gdx.graphics.getHeight()/6,Gdx.graphics.getHeight()/6);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.restitution = 2;
Fixture fixture = body.createFixture(fixtureDef);
}
private void line(float X, float Y, float w, float h)
{
BodyDef bodyDef = new BodyDef();
bodyDef.type=BodyDef.BodyType.StaticBody;
bodyDef.position.set(X,Y);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(w,h);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=0.4f;
fixtureDef.friction=0.5f;
Body theFloor=world.createBody(bodyDef);
theFloor.createFixture(fixtureDef);
}
private void wall(float A)
{
// is there a better way of doing this?
line(0,Gdx.graphics.getHeight()/2-A,Gdx.graphics.getWidth()/2-A,0);
line(Gdx.graphics.getWidth()/2-A,0,0,Gdx.graphics.getHeight()/2-A);
line(0,-Gdx.graphics.getHeight()/2+A,Gdx.graphics.getWidth()/2-A,0);
line(-Gdx.graphics.getWidth()/2+A,0,0,Gdx.graphics.getHeight()/2-A);
}
#Override
public void render(float delta) {
world.step(1 / 5f, 6, 2);
OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
processAccelerometer();
wall(1);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
}
#Override
public void dispose() {
world.dispose();
debugRenderer.dispose();
}
private void processAccelerometer() {
float y = Gdx.input.getAccelerometerY();
float x = Gdx.input.getAccelerometerX();
if (prevAccelX != x || prevAccelY != y) {
world.setGravity(new Vector2(y, -x));
prevAccelX = x;
prevAccelY = y;
}
}
#Override
public void hide()
{
// TODO: Implement this method
}
#Override
public void resize(int p1, int p2)
{
// TODO: Implement this method
}
#Override
public void resume()
{
// TODO: Implement this method
}
#Override
public void pause()
{
// TODO: Implement this method
}
#Override
public void render()
{
// TODO: Implement this method
}
}
In Box2D you have to take into account that 1 pixel = 1 meter. So, basically, everything you simulate in Box2D will be HUGE by default. And Box2D simulations are not accurate for huge distances, huge masses, huge speeds...
So all you have to do is convert your viewport with a conversion factor, so you'll just look at small entities, that will be easier to simulate.
For example, let's say you want 100 pixel = 1 meter, you'll put that code when you create your game screen :
WORLD_TO_BOX = 1/100f;
BOX_TO_WORLD = 1/WORLD_TO_BOX;
//Creation of the camera with your factor 100
camera = new OrthographicCamera();
camera.viewportHeight = Gdx.graphics.getHeight() * WORLD_TO_BOX;
camera.viewportWidth = Gdx.graphics.getWidth() * WORLD_TO_BOX;
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);
camera.update();
Then, you'll create your boxes in term of camera.viewportWidth and camera.viewportHeight, instead of Gdx.graphics.getWidth() and Gdx.graphics.getHeight()
I your case you'll have :
PolygonShape shape = new PolygonShape();
shape.setAsBox(camera.viewportHeight/6,camera.viewportHeight/6);
You can see in my code, there is also a BOX_TO_WORLD conversion factor. It will be used when you want to render graphics over your box2D bodies.
For that, look at the answer of this question.

Why does wheel movement in Box2d doesn't cause car to move?

I'm working on moving vehicle based on Box 2d physics for my Android game (Java + LibGDX). When I thought I have everything up and running my, my car with two wheels stands on the ground but when I rotate the wheels The car does not move.
That's my Game class:
public class MyGdxGame implements ApplicationListener {
World world = new World(new Vector2(0, -10), true);
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final float BOX_STEP=1/60f;
static final int BOX_VELOCITY_ITERATIONS=6;
static final int BOX_POSITION_ITERATIONS=2;
static final int WIDTH=256;
static final int HEIGHT=169;
Hero hero;
MovableBox box;
#Override
public void create() {
camera = new OrthographicCamera();
camera.viewportHeight = HEIGHT;
camera.viewportWidth = WIDTH;
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
camera.update();
Vector2[] anArray;
anArray = new Vector2[4];
anArray[0]= new Vector2(-30,0);
anArray[1]= new Vector2(55,55);
anArray[2]= new Vector2(110,55);
anArray[3]= new Vector2(195,0);
//Ground body
BodyDef groundBodyDef =new BodyDef();
groundBodyDef.position.set(new Vector2(-8-13-20-20-10, -44));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.set(anArray);
//groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody.createFixture(groundBox, 0.0f);
//Ground body
BodyDef groundBodyDef2 =new BodyDef();
groundBodyDef2.position.set(new Vector2(13*11-4-7-8-13-20-20-10, -44));
Body groundBody2 = world.createBody(groundBodyDef2);
PolygonShape groundBox2 = new PolygonShape();
groundBox2.set(anArray);
//groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);
groundBody2.createFixture(groundBox2, 0.0f);
hero = new Hero();
hero.create(world, 0, 0);
box.create(world, WIDTH / 4 * 3, HEIGHT / 2);
boxes.add(box);
debugRenderer = new Box2DDebugRenderer();
}
private void update(){
hero.update();
for(MovableBox b : boxes) {
b.update();
}
}
#Override
public void dispose() {
}
#Override
public void render() {
update();
camera.position.set(hero.getPos(), 0f);
camera.update();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
And Hero class (which should be called a car actually)
Body body;
Body wheel, wheel2;
public void create(World world, int posx, int posy){
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(posx, posy);
body = world.createBody(bodyDef);
PolygonShape dynamicCircle = new PolygonShape();
dynamicCircle.setAsBox(8, 3);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 4.0f;
fixtureDef.restitution = 0.1f;
fixtureDef.friction=0.95f;
body.createFixture(fixtureDef);
BodyDef bodyDef2 = new BodyDef();
bodyDef2.type = BodyDef.BodyType.DynamicBody;
wheel = world.createBody(bodyDef2);
CircleShape dynamicCircle2 = new CircleShape();
dynamicCircle2.setRadius(2);
FixtureDef fixtureDef2 = new FixtureDef();
fixtureDef2.shape = dynamicCircle2;
fixtureDef2.density = 4;
fixtureDef2.friction=3.0f;
fixtureDef2.restitution=0.1f;
wheel.createFixture(fixtureDef2);
wheel2 = world.createBody(bodyDef2);
wheel2.createFixture(fixtureDef2);
RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.bodyA = body;
revoluteJointDef.bodyB = wheel;
revoluteJointDef.localAnchorA.add(-8,-6);
world.createJoint(revoluteJointDef);
RevoluteJointDef revoluteJointDef2 = new RevoluteJointDef();
revoluteJointDef2.bodyA = body;
revoluteJointDef2.bodyB = wheel2;
revoluteJointDef2.localAnchorA.add(8,-6);
world.createJoint(revoluteJointDef2);
}
public Vector2 getPos(){
return body.getPosition();
}
public void update(){
if(Gdx.input.isTouched())
{
if(Gdx.input.getX()<600){
wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()+0.1f);
wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y, wheel2. getAngle() + 0.1f);
}
else{
wheel.setTransform(wheel.getPosition().x,wheel.getPosition().y,wheel.getAngle()-0.1f);
wheel2.setTransform(wheel2.getPosition().x,wheel2.getPosition().y,wheel2.getAngle()-0.1f);
}
}
}
Well I think there are two things you need to consider.
First of all I think if you want your physics engine to "force" the car to move on wheels spin, you probably should look into damping parameters of BodyDef (linearDamping and angularDamping). Setting those for ground and car wheels might help you move your car on wheels spin.
The second thing is that you really need to analyze if it's the proper approach. Maybe you should think about simulating movement of your car by moving the body of your car itself and spinning your wheels. I think this approach should give you more control on the car movement.

Newton's cradle on andengine, box2d

i'm trying to create something like Newton's cradle. But when one ball hits another, all balls move simultaneously in same direction. How can I decide this problem? Should i create another physical options of balls in createFixtureDef? Or I need to use some specific algorithm to transfer impuls between balls?
public class MainActivity extends SimpleBaseGameActivity implements IAccelerationListener,
IOnAreaTouchListener
{
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private BitmapTextureAtlas mBitmapTextureAtlas;
final String TAG = "States";
private Scene mScene;
protected ITiledTextureRegion mBoxFaceTextureRegion;
protected ITiledTextureRegion mCircleFaceTextureRegion;
protected PhysicsWorld mPhysicsWorld;
#Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
this.mBitmapTextureAtlas.load();
}
#Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new Background(0, 0, 0));
//this.mScene.setOnSceneTouchListener(this);
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
this.initJoints(mScene);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
this.mScene.setOnAreaTouchListener(this);
return this.mScene;
}
private void initJoints(final Scene pScene) {
final float centerY = CAMERA_HEIGHT / 2;
final float spriteWidth = this.mBoxFaceTextureRegion.getWidth();
final float spriteHeight = this.mBoxFaceTextureRegion.getHeight();
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(30, 0.2f, 0.2f);
for(int i = 0; i < 10; i++) {
final float anchorFaceX = 100 + i * spriteWidth ;
final float anchorFaceY = centerY;
final AnimatedSprite anchorFace = new AnimatedSprite(anchorFaceX, anchorFaceY,
this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
final Body anchorBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
anchorFace, BodyType.StaticBody,
objectFixtureDef);
final AnimatedSprite movingFace = new AnimatedSprite(anchorFaceX, anchorFaceY + 150,
this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager()) ;
// movingFace.setScale(1.2f);
final Body movingBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
movingFace, BodyType.DynamicBody, objectFixtureDef);
movingFace.setUserData(movingBody);
// anchorFace.setScale(1.2f);
anchorFace.animate(200);
anchorFace.animate(200);
final Line connectionLine = new Line(anchorFaceX + spriteWidth / 2,
anchorFaceY + spriteHeight / 2,
anchorFaceX + spriteWidth / 2,
anchorFaceY + spriteHeight / 2,
this.getVertexBufferObjectManager());
pScene.registerTouchArea(movingFace);
pScene.attachChild(connectionLine);
pScene.attachChild(anchorFace);
pScene.attachChild(movingFace);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(anchorFace,
anchorBody, true, true){
#Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
final Vector2 movingBodyWorldCenter = movingBody.getWorldCenter();
connectionLine.setPosition(connectionLine.getX1(), connectionLine.getY1(),
movingBodyWorldCenter.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
movingBodyWorldCenter.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
}
});
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(movingFace, movingBody, true, true));
final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(anchorBody, movingBody, anchorBody.getWorldCenter());
this.mPhysicsWorld.createJoint(revoluteJointDef);
}
}
#Override
public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
}
#Override
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
public boolean onAreaTouched( final TouchEvent pSceneTouchEvent,
final ITouchArea pTouchArea,final float pTouchAreaLocalX,
final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionMove())
{
float touchX = pSceneTouchEvent.getX();
float touchY = pSceneTouchEvent.getY();
Log.d(TAG, "move to in X" +touchX + "n Y " +touchY);
final AnimatedSprite anchorFace = (AnimatedSprite) pTouchArea;
final Body tochedBody = (Body)anchorFace.getUserData();
//move sprite to xy
final float x = pSceneTouchEvent.getX();
final float y = pSceneTouchEvent.getY();
final float widthD2 = anchorFace.getWidth() / 2;
final float heightD2 = anchorFace.getHeight() / 2;
final float angle = tochedBody.getAngle(); // keeps the body angle
final Vector2 v2 = Vector2Pool.obtain((x + widthD2) / 32, (y + heightD2) / 32);
tochedBody.setTransform(v2, angle);
Vector2Pool.recycle(v2);
return true;
}
return false;
}
}
Unfortunately Box2D is not really suitable for this.
In my experience, you can get one or two swing-thrus to work as long as the balls are not touching each other to start with. That is, each ball starts with a very small gap between it and the neighbor on each side. This means that when they collide each collision is solved using just two balls at a time, for a total of 4 separate collisions for the impact to travel to the other side (for 5 balls), instead of solving all 5 balls as a single 'island'.
Trouble is, the positioning needs to be very precise, and after a few swings the balls stray from that, so that you end up with collisions involving more than two balls at a time. I only ever saw at most 2-3 swings work as desired...

jbox2d on Android - circle not applied gravity and other staff

I am doing simple application that should put circle on Canvas when the user taps the screen and then put that circle in the PhysicalWorld I have defined.
I draw the circle in "onDraw" method, but when it is created its position does not change (seems like the gravity and staff are not applied) and the circle is static (stays on the position where it's created).
can you check this code and tell me if I am doing anything wrong:
[update]
public class PhysicsWorld extends SurfaceView implements SurfaceHolder.Callback {
private AABB worldAABB;
private World world;
private PolygonDef groundShapeDef;
public int W_width, W_height;
private static final String TAG = PhysicsWorld.class.getSimpleName();
protected static final int GUIUPDATEIDENTIFIER = 0x231;
public int targetFPS = 40;
public float timeStep = (10.0f/targetFPS);
public int iterations = 5 ;
private int count=0;
private Body[] theBodies ;
private Paint paint;
private float radius = 20;
private MyThread mMyThread;
public PhysicsWorld(Context context) {
super(context);
}
public PhysicsWorld(Context context, AttributeSet set) {
super(context, set);
getHolder().addCallback(this);
W_width = 500;
W_height = 700;
worldAABB = new AABB();
Vec2 min = new Vec2(-50, -50);
Vec2 max = new Vec2(W_width+50, W_height+50);
worldAABB.lowerBound.set(min);
worldAABB.upperBound.set(max);
Vec2 gravity = new Vec2((float) 10.0, (float) 9.8);
boolean doSleep = false;
world = new World(worldAABB, gravity, doSleep);
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(W_width, 10);
groundBody.createShape(groundShapeDef);
// up :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) (W_height + 10.0)));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(W_width, 10);
groundBody.createShape(groundShapeDef);
// left :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2(-10, (float) 0.0));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(10, W_height);
groundBody.createShape(groundShapeDef);
// right :
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) W_width + 10, (float) 0.0));
groundBody = world.createBody(groundBodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox(10, W_height);
groundBody.createShape(groundShapeDef);
theBodies = new Body[50];
paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
// setWillNotDraw(false);
}
public void addBall(int x, int y) {
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(x, y);
Log.d(TAG,"Ball Created At: " + Integer.toString(x) + "," + Integer.toString(y));
theBodies[count] = world.createBody(bodyDef);
CircleDef circle = new CircleDef();
circle.radius = radius;
circle.density = (float) 1.0;
circle.restitution = 0.5f;
theBodies[count].createShape(circle);
theBodies[count].setMassFromShapes();
count+=1;
}
public void update() {
world.step(timeStep, iterations);
postInvalidate();
}
#Override
protected void onDraw(Canvas canvas) {
paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
for (int j = 0; j < count; j++) {
canvas.drawCircle(theBodies[j].getPosition().x, W_height - theBodies[j].getPosition().y, radius, paint);
Log.v(TAG + " x: ", String.valueOf(theBodies[j].getPosition().x));
Log.v(TAG + " y:", String.valueOf(W_height - theBodies[j].getPosition().y));
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
addBall((int)event.getX(),(int)event.getY());
}
return true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mMyThread = new MyThread(holder, this);
mMyThread.setFlag(true);
mMyThread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
My experience with box2D is in C++ and iPhone, but it seems your forgetting to make the Ball dynamic.
I've kinda guessed at the solution as I don't know the exact syntax for JBox2D (Please edit if anyone knows actual code)
bodyDef.type = BodyDef.dynamicBody;
I've had a look at the documentation on google code and I think you should use world.createDynamicBody(bodyDef);

Categories

Resources