Hello
I was going to post in my previous topic but thought to create a new one because this is a completely different problem.
Project Link: https://playcanvas.com/editor/scene/1204572
What I am trying to achieve is smoother falling so when a player enters the falling state a force is applied and as the player falls even more it reaches a max force (terminal velocity). I understand that drag in Playcanvas translates to linearDamping, but 0 linearDamping on -9.8 gravity is still really slow when falling. I want to keep my global gravity as -9.8 while simulating a smooth falling. I did not want to just apply a constant force on the Y axis when the player was falling as that is not natural. I made a picture of what I am trying to achieve below.
So far the code I wrote to calculate this is:
if (this.playerFalling) {
let airResistance = 10000;
let linearVelocity = this.entity.rigidbody.linearVelocity;
let fallVelocity = linearVelocity.normalize().y * airResistance * dt;
let coeff = (1 - dt * airResistance);
let force = (fallVelocity / coeff) * this.entity.rigidbody.mass;
this.entity.rigidbody.applyForce(0, fallVelocity + force, 0);
console.log('velocity: ' + linearVelocity.y);
console.log('coeff: ' + coeff);
console.log('force: ' + force);
}
The terminal velocity appears to be working (atleast I think it does as the velocity increases from a value between -0.0 and -1 which then caps as the max). Below is a screen shot of debugging the velocity value which finally caps at -1 once it hits the terminal velocity.
But the problems are:
-
The fall starts off really slow. So when the player enters the falling state it floats down and then gets faster over time slowly. I want the terminal velocity to be achieved quicker but without breaking jumping.
So instead of this: jump → float → fall → fall faster
I want it to be: jump → fall → fall faster -
I am currently having to enter airResistance to 10000+ which does not seem like an optimal value (it’s too large but somehow works at the moment).
I feel like I am very close somehow but can’t figure out the last step, or I might have made some miscalculations. If somebody could check the code/project and help me simulate a nice natural falling at -9.8 I would really appreciate it!