[SOLVED] applyForce() stops working with normalize()

I’m having trouble applying force to my player. This is how I’m doing it:

this.velZ = (this.lastLTouchPoint.y - this.LTouchStartPoint.y) * this.movementSensitivity;
this.velX = (this.lastLTouchPoint.x - this.LTouchStartPoint.x) * this.movementSensitivity;

this.force.set(this.velX, 0, this.velZ);
this.player.rigidbody.applyForce(this.force);

The player starts moving faster and faster the longer I move it. I tried to do

this.force.set(this.velX, 0, this.velZ).normalize().scale(this.movementSensitivity);

But then my player stopped moving completely. What am I doing wrong?

Hi @Gamer_Wael,

Are you resetting the lastLTouchPoint values each frame? Even better can you post your full code or a sample project to take a look at?

I’m updating lastLTouchPoint in the onTouchMove function.

Here’s my project. All the touch and movement code is in touch-input.js

Touch the left side of the screen for movement(which isn’t working), right side for camera rotation.

Here’s a live demo:
https://gamerwael.github.io/playcanvas-mobile-game/

Quick q, how much do you know about vectors?

Umm. Not too much… Sorry if I made a silly mistake.

Hi @Gamer_Wael,

I can’t easily test it, since it lives outside the Playcanvas editor, but with a quick look with the browser debugger I think your velocity calc uses the same coordinates for delta. Could you try debugginyour velZ/velX in the console?

You can also check the default Model Viewer Playcanvas template on how it handles panning with touch input, it does something similar with what you are trying to do here.

1 Like

It might be that your this.movementSensitivity is not high enough to move the player.
Considering your vector values for X and Z are correct (are they?) before you normalized the vector, then the only thing can be that after you scale it by the sensitivity value, the resulting force is too small. You can try to multiply the sensitivity value by some scalar after you normalized the vector to test it.

3 Likes

It turns out that that was the problem after all. Thanks a lot for the input from everyone.

I’m actually trying to make a third person camera.

I have the player moving at a constant speed by scaling the force and with LinearDamping 0.99. However when the player falls it falls very slowly. I don’t think it’s possible to to set linearDamping for each axis individually. So I might try to make my player kinematic or something.

You can do the linear damping yourself in code, that way you can apply it only on the axes you want.

1 Like

Do you have an example project that does this? Or can you please explain how the engine handles linearDamping?

You can see how it is done in the physics library here: https://github.com/bulletphysics/bullet3/blob/6985f06e5d90f7a9d5998a8fc2cfdabf116b0a2d/src/BulletDynamics/Dynamics/btRigidBody.cpp#L149

1 Like

Thanks a lot. I got it to work now. This is how I’ve done it

var dampingForceX = -2 * this.player.rigidbody.linearVelocity.x;
var dampingForceZ = -2 * this.player.rigidbody.linearVelocity.z;

this.dampingForce = new pc.Vec3(dampingForceX, 0, dampingForceZ);
    
this.player.rigidbody.applyForce(this.dampingForce);

FYI, that code is frame you’ve written is frame dependent. ie slower/faster framerates may behave differently.

In the bullet library, they use delta time/timestep:

	m_linearVelocity *= btPow(btScalar(1) - m_linearDamping, timeStep);
1 Like