Controlling inertia

https://playcanvas.com/project/482038/overview/testing-grounds

Here is a simple version of another game I made, My question is, how do I control the Inertia of my character.

As you can see, he FLYS around but doesn’t slow down as I’d prefer. I already have the damping at like .99 it feels like it isn’t doing much. Thoughts?

Here is the other game but it has a lot more going in it so it may be easy to get lost in it.

https://playcanvas.com/project/487997/overview/10-seconds

@max gave me some good feedback on this already! Perhaps he’ll have the solution :slight_smile:

First link goes to project which is private, so can’t be seen by public.

doh! should be public now lol.

So I see that UI does not allows to set more precise linear dumping, and rounds it, I will update (EDIT: done) that to allow around 6 numbers after dot.
From the code I’ve set higher linearDumping and increased speeds for inertia, and that worked pretty well.

Alternative way would be instead of applying forces you would set linear speed of rigidbody and calculate speeds yourself, this gives you more control over speeds by math.

OH SHIIIIIII

That makes a big difference. I’ll have to play with the values.

Not sure what you mean by the math though? I’ll have to ponder. Unless you can elaborate with code that I can try to decipher. That’s how I have been learning mostly. Just deciphering all this shiz.

Now you have (pseudo code):

if (keyRight) {
    entity.applyForce(0, 1, 0);
}

What you might try (pseudo code):

// initialize
this.speed = 0;

// update
if (keyRight) {
    this.speed = 1;
} else {
    this.speed = 0;
}

entity.linearVelocity = this.speed;

So this would instead of applying forces and relying on damping of it, you would simply set linear velocity. With example above it will be constant speed when pressed key, and no movement when not.

By changing it to something like:

if (keyRight) {
    if (this.speed < 1.0) {
        this.speed += 2.0 * dt;
    } else {
        this.speed = 1.0;
    }
} else {
    if (this.speed > 0.0) {
        this.speed -= 2.0 * dt;
    } else {
        this.speed = 0.0;
    }
}

So if key is pressed it will increase speed linearly in this case 2.0 units a second, but because it is limited to 1.0, it will get full speed in half a second. And when key is not pressed it will linearly decrease to no speed.

Or one liner:

this.speed = Math.max(0, Math.min(1.0, this.speed + (keyRight ? 2.0 : -2.0) * dt));

This makes perfect sense!

How do I get specific values? Like for linearVelocity’s x,y or z.

Because the way I would do it would be variables like:

this.speedx = 1;

I actually will not use this strategy for my code, since I do want inertia to help with the sense of slowing, TOO MUCH slowing feels too easy. But having some control is valued.

BUT I do want to know the method you suggested because it would be ideal for a platform game I have in mind where I do not want ANY momentum at all. Other than probably in the jumping, but even then I’d probably fake/control the gravity via hand made code.

So I got this to work great btw, The only issue I’m curious about is if I want to control the Y value by not being affected at all when moving using the x value.

So the way I have the code is simple:

var goRight = new pc.Vec3(2,0,0);
var goLeft = new pc.Vec3(-2,NaN,0);

SimpleMoveLinearVelocity.prototype.update = function(dt) {
    
    if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){
        this.entity.rigidbody.linearVelocity = goRight;
        console.log('pressing right');
    } else if(this.app.keyboard.isPressed(pc.KEY_LEFT)){
        this.entity.rigidbody.linearVelocity = goLeft;
        console.log('pressing left');
    }   else { 
        this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
    }
    
};

So this works great for total control of the speed, the only issue is if I want to apply physics again, how do I prevent the y value from being affected by the Vec3. Is there a way to null the y value then allow gravity to do it’s thing?

I have some solutions in mind, but was curious if you had some creative ones.

The ones I have in mind is to have it collision based, meaning if the entity is touching some collision then don’t worry about gravity, but if it’s not… turn off these controls, except for a jump or something. But was wondering if there was a way to snipe values in the Vec3.