Rolling problems

Hey all,



I’m trying to make the player, who is modeled after a ball, roll in the direction of the camera view:



[java] public void onAnalog(String name, float value, float tpf) {

GPlayer player = Main.GApp.CurrentWorld.GetUpdater().GetPlayer();

if (tpf > 0 && player != null ) {

if (name.equals(“Forward”)) {

player.myPhysicsObject.GetPhysicsControl().applyTorqueImpulse(player.GetLook().getRotationColumn(0));

return;

}

if (name.equals(“MouseYU”)) {

player.RotateLook(value * MouseSensitivity / tpf, 0f);

return;

}

if (name.equals(“MouseXU”)) {

player.RotateLook(0f, value * MouseSensitivity / tpf);

return;

}

if (name.equals(“MouseYD”)) {

player.RotateLook(-value * MouseSensitivity / tpf, 0f);

return;

}

if (name.equals(“MouseXD”)) {

player.RotateLook(0f, -value * MouseSensitivity / tpf);

return;

}

}

}[/java]



When I tap the “Forward” key, the game acts like I am keeping the “Forward” key pressed for the initial direction (e.g. the torque seems to keep being applied even after the key is released). However, when I run this in debugging mode, and put a breakpoint on that torqueImpulse call, it isn’t being caught when “Forward” isn’t pressed (as expected). So why does my player seem to keep accelerating…? I’ve tried using .applyTorque(…) and the player doesn’t accelerate at all. I’m so confused :frowning: Anyone have any suggestions?



Thank you in advance!

You never check for key press/release, also you cannot in an analog listener… So why would you expect it to stop?

I would expect it to stop when the “Forward” key (W) isn’t being pressed. I only want it to apply the torque as the key is being pressed.

Yeh, then use a KeyListener.

I’m still a bit confused…



In my 3079 game, I used an analog listener to control the character’s movement. As long as the “Forward” key was held, the onAnalog function would be called with the “Forward” argument, and I would add a little force to the player each frame the key stayed held. However, when the key was released, onAnalog stopped being called with “Forward”, so the force stopped being applied.



Here, the control flow seems to be working as expected: applyTorqueImpluse is only being called when the key is being pressed. However, the physics behavior is what is confusing. If I simply just tap the key, my player begins to move slowly in the direction I was looking…



I think I just figured it out as I was typing: when applyTorqueImpulse gets called, the player’s “ball” gets immediately spun in the other direction, but since the player has momentum, the player doesn’t immediately start moving in that new direction. As the “ball” picks up traction, it moves in the direction it is now spinning (even after the key is released).



I guess I need to look into increasing the player’s ball traction… makes sense?

You should not apply forces like this anyway, they are only applied when an actual physics tick is happening (so say at 120fps only half of the time). Use a physicsTickListener to apply the force in the right moment. Maybe this decoupling will also make troubleshooting easier for you.

Ahh, OK.



I suppose I should set flags when a movement key is pressed in a KeyListener, and then check for these flags in the physicsTickListener and apply torques as needed. Doesn’t seem so straightforward though, but necessary, I suppose.



Thanks for the help!