Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Box2D object wont move until tons of force is used

$
0
0

Im making a game for android im using LibGDX and Jbox2D it is a space flying exploration game controlled with the accelerometer. so I have set the world to no have no gravity. the objects in Box2D don’t like having forces applied to them apparently. Just to get an object moving more then a few pixels at a time I need to use over 10,000 in applyForceToCenter()

in the Game class I get the accelerometer in the update method and create a direction vector from the input and pass that to the player

    accelX = Gdx.input.getAccelerometerX();//get the various tilts
    accelY = Gdx.input.getAccelerometerY();
    accelZ = Gdx.input.getAccelerometerZ();

    //These are backwards because the game is in landscape and not portrait
    p1Direction.x = (accelY/10); //getAccelerometer comes back -10 to 10 
    p1Direction.y = -(accelX/10); //reduce this to -1 to 1;

    p1.setDirection(p1Move);
    p1.update(Gdx.graphics.getDeltaTime());

Then In the player I apply a force to the object based on the direction:

    acceleration.x += (directionVector.x * SPEED)*deltaTime; //speed is set to 200
    acceleration.y += (directionVector.y * SPEED)*deltaTime;

    this.getBody().applyForceToCenter(acceleratiom, true);

to try and get it moving I had to add:

    acceleration.x += (directionVector.x * SPEED)*deltaTime*1000; //speed is set to 200
    acceleration.y += (directionVector.y * SPEED)*deltaTime*1000;

    this.getBody().applyForceToCenter(acceleratiom, true);

now it moves a little better, but once it gets moving it won’t stop

Is there a way to fix this?

Update: The Mass, Friction, density, and restitution are all 1 right now here’s the camera:

    camera = new OrthographicCamera(screenWidth,screenHeight);
    camera.setToOrtho(false,screenWidth,screenHeight);
    camera.position.set(0, 0, 0);

and the player is rendered with

batch.draw(playerTexture, this.getBody().getPosition().x, this.getBody().getPosition().y,        
    (playerTexture.getWidth()/2), (playerTexture.getHeight()/2),   
    playerTexture.getWidth(), playerTexture.getHeight(), 
    1.0f, 1.0f, this.getBody().getAngle(), 0, 0, 
    playerTexture.getWidth(),  playerTexture.getHeight(), 
false, false);
//Texture position half width(so the texture rotates in the center), width and height of the texture, scale of 1, rotate by the angle (doesn't rotate right now) source position source width and height

Viewing all articles
Browse latest Browse all 434

Trending Articles