[libGDX] change direction after colliding

Discuss issues specific the Java port of Box2D
shizzle31
Posts: 1
Joined: Sat Feb 07, 2015 12:43 pm

[libGDX] change direction after colliding

Postby shizzle31 » Sat Feb 07, 2015 12:50 pm

i have a problem with the velocity change of my body. Normaly the linearVelocity is 3f, and when the body collides with a wall the velocity shoud change at -3f. But when it does it goes around 0.

Here's the code:

private void createPlayer(){
...
bdef.velocity.set(-3f,0);
...
}

private void render(){
...
world.step(V.STEP, V.VELOCITYITERATIONS, V.POSITIONITERATIONS);
if(cHandler.changeVelocity())
ball.getBody().setLinearVelocity(-ball.getBody().getLinearVelocity().x, 0);
...
}

i've already try it in beginContact, endContact, presolve and endsolve but always it is about 0.

And when i put the collision detection in beginContact and set it manually to -3f then it is -3f but the jumping does not work anymore correctly, i mean the gravity do not grab the ball anymore to the ground.

when i set it manually:

if(cHandler.changeVelocity()){
ball.getBody().setLinearVelocity(-3f, 0);
}

the log before:

velocityX: 3.0022564 velocityY: -5.261436

and after:

velocityX: -3.0 velocityY: 0.0

that mean i will that the body move continius right and after colliding continius left till a new collide..
p.s the world gravity is (0, -10)
i hope you can help me.

Thanks!

seannybgoode
Posts: 6
Joined: Fri Nov 14, 2014 12:41 pm

Re: [libGDX] change direction after colliding

Postby seannybgoode » Sun Feb 22, 2015 9:30 pm

Why are you setting the velocity manually? If you want things to bounce around naturally, or bounce off things, you need to look at the restitution property of the collidable objects.


Return to “Java”



Who is online

Users browsing this forum: Google [Bot] and 2 guests