Controlling BetterCharacterControl speed

Hello. I’ve two questions about BetterCharacterControl which are possibly related.

1. I’m not sure how to stop it from falling through terrain when falling at high speeds (this is part of the game). The terrain is also physics-controlled.

First, I get my preemptive ‘red flag’ for when the player is about to pass through the ground by casting a ray down, getting the distance, then checking this against the player’s velocity * tpf.

To stop the player, I’ve tried negating the gravity, but I can’t get the numbers right here (player shoots off into space), and using ‘warp’ to move the player, but the player still has the same velocity, meaning it sort of jitters up and down on the spot for a while.

2. How do I stop the player from bouncing on the ground? I can set the restitution on the terrain, but not sure how to do so on the player.

Thanks!

Regarding the first, I don’t know bullet at all but in a general physics sense you would have to apply a reverse impulse to affect velocity directly. It should be -currentVelocity * tpf to negate all velocity. Or just make a negative y velocity if you only want to nullify downward movement.

I actually don’t know how bullet implements impulses so I could be wrong.

And because I’m trying to get my brain warmed up…

Here is the math for a one frame force that should negate all downward velocity:

// Acceleration necessary to change the downward component of velocity to 0
Vector3f upAcceleration = new Vector3f( 0, -currentVelocity.y, 0 );

// F = ma
Vector3f force = upAcceleration.mult( mass );

Hmm, that makes perfect sense, but I’m not sure how to apply impulses to BetterCharacterControl - it doesn’t seem possible…?

@davekidd said: Hmm, that makes perfect sense, but I'm not sure how to apply impulses to BetterCharacterControl - it doesn't seem possible...?

I have no idea. Maybe there’s a way to get at the rigid body or something? I know there are ways to apply impulses and forces to physics objects in general because I was in another thread about that.

For 1) try to get the numbers right
For 2) no there currently isn’t unless you extend the BetterCharacterControl yourself
I plan on encapsulating the movement as well as “slope” settings differently and as restitution and y-movement as well as gravity play into this its not yet clear how the parameters will look.

Here’s what I have now. I’ve extended BetterCharacterControl to get the rigid body.

[java]velocityRay.setOrigin(playerNode.getWorldTranslation());
velocityRay.setDirection(playerControl.getVelocity().normalize());

CollisionResults resultsDn = new CollisionResults();
app.getRootNode().collideWith(velocityRay, resultsDn);

if (resultsDn.size() > 0)
{
distance = resultsDn.getClosestCollision().getDistance();

if (playerControl.getVelocity().length() * tpf > distance - playerHeight)
{
// Reverse the velocity and multiply by the player’s mass x gravity
negateForce.set(playerControl.getVelocity().negate().mult(playerControl.getGravity().length()));
playerControl.getRigidBody().applyForce(negateForce, Vector3f.ZERO);
}
}[/java]

Normen, with regards to (2), I’ve tried setting restitution to zero on both the player’s rigid body and the terrain, but there’s no difference - the player still bounces. Is there something else I need to consider?

What you do there is not necessary, get your gravity value right. I also had no success changing the bouncing behavior with restitution yet.

If I change the gravity, then I’ll have to change it back again in the next tick, yeah?

Seems more efficient to just apply the force once, which has the same effect on the node, but I don’t have to worry about readjusting the gravity.

@davekidd said: If I change the gravity, then I'll have to change it back again in the next tick, yeah?

Seems more efficient to just apply the force once, which has the same effect on the node, but I don’t have to worry about readjusting the gravity.

Its the same thing but you cannot turn off the gravity anyway so you can apply that impulse via the gravity.