How to use setLinerVelocity when using Cannon.js


I’m trying to prevent a physics object from going to fast.

At lines 280 - 288 I try to prevent the player from going to fast, but I’m not sure how set linear velocity works.

Thanks for any help!


There are a few errors in the scene itself. First, vector3.x (or y, or z) is a number, and doesn’t have the function length(). (lines 280 and up). Second, the mousemoveCallback function is missing, so i cant quite get it to work.
Want to fix this and explain the problem? would also be great if you filter the problem down to a few lines of code (if possible) so we can concentrate on the issue at hand :slight_smile:

1 Like

Simple and easy.

This freezes the scene

But this works
I implemented it here but holding w and space breaks the speed barrier.

Heya Givo. Got your LV working? Might want to mark as solved.

When airborne, ya got no ground friction, and thus, no “traction”. Sooooo… you know… no ‘w’ key processing allowed when in-air. :wink: LV has too much affect… when airborne, right? ZOOM!

You could setLinearDamping when airborne, and remove damping when back onto ground (trying to precisely-match air-damping… to the linear damping caused by ground friction). But, that will partially kill your jump height. It will be like “air-friction” :slight_smile:

You can build-your-own linear damping that DOESN’T affect jump-height.

When in air… constantly multiply player linearVelocity * new BABYLON.Vector3(.9, 1, .9). By doing this, you reduce linearVelocity by 10% each frame or physics-step, but ONLY on X and Z axis (just like ground friction does). Y-axis is un-affected.

You might be able to still keep ‘w’ key active when airborne, then. (.8, 1, .8 is even MORE speed reduction when in air… but ONLY do the multiplies when in the air. When on-ground, don’t do it anymore… or set your home-made LV-multiplier damper to (1,1,1) - no damping at all.

Ok, that’s all I got… party on. :slight_smile:

1 Like

I don’t have it working, but thanks for the ideas. I have the linear damping set to 0 when in air so I can program the bhop. I want you to have a constant speed in mid air while not holding w, which I have, but if you hold w you will hit a speed barrier. In the source engine (where bhop was popularized) I you hold w while in are you gain speed, but hit a speed barrier. I you don’t hold w, you stay the same speed. If you air strafe (hold a and look left, hold d and look right) you gain speed while rotating you trajectory a bit. The playground I made for @RaananW was to show that the sphere when falling can stay at the same speed. (in the playground it goes to -50 then back to -25 to show it isn’t frozen) but when I put it in my main code the players linear velocity doesn’t stay at 20 (which is what it should stay at)

Any ideas? I hope that all makes sense.

Erf. I don’t understand much of that, but I DID improve the linear velocity display.

You know how this “programming stuff” works, G-wizard. console.log/display EVERYTHING. @JohnK taught me a method that works fairly consistently. Branch-away from your main “saves” folder, and start a new folder. In THAT project, shrink your scene just as TINY as possible… rip out EVERYTHING that doesn’t involve the actual problem. De-beautify 100%. Keep doing that, save after save… making smaller and smaller versions… and eventually… the PRECISE problem shows it’s face.

GUI festival…

Let’s pretend you steal the “bottom panel” from that playground, and OWN IT for yourself. Besides using it for readouts, put “flag indicators” on it… so you can “watch” your lines 133-140 flags/booleans. Maybe an isVertical = false stackPanel full of labeled checkboxes. When a flag goes true, the checkbox sets itself checked. When flag goes false, checkbox un-checks. With a really nice lower/upper panel, you can monitor your stuff while you drive the sim. It takes a while to set-up a live-monitoring system for your flags, but after its done you’ll feel more powerful and have a nice tool that will be your friend in the future. You’ll need a new function: setFlag(whichflag, true/false). You ALWAYS use this function… to set your flags. Inside that function, you update your GUI checkboxes, too.

Just some ideas to help you be successful. Nothing specific… sorry.

TOO much code… too many isDoingSomething flags and conditions, and it can make trouble-finding a real nightmare… and it can get discouraging. Squeeze it like sponge, wring it out. Branch-away from your mainline dev folder… go adventuring in the make_it_tiny side-folder. :slight_smile:

1 Like

Heh. @RaananW said the same thing. Shrink the playground to as small as possible. But this is what confuses me.
In this playground, lines 51 is what I want. if the sphere is falling at a rate > -50 per second, it sets it’s y speed to -25. All is good in the world and it works.

Lines 282 - 290 are essentially a copy of line 51 of the last playground. but this time the speed of the player doesn’t change. It keeps growing.

this works, for some odd reason. I copy-pasted line 51 from the first pg to the second pg, and changed the detection to the z, and the value to 50. I don’t know why it works. It’s weird.

1 Like

if (mf == true) translate(player, new BABYLON.Vector3(0, 0, 2+spood*howmuchairmove), transpower);

Man, using mesh translates on a physics-active mesh… that’s just scary. Apparently it works… but… yikes. I would do player.physicsImpostor.setLinearVelocity.x/z THERE, instead of translate. ApplyForce and applyImpulse would work, too. Same with jump… thrust the player into the air with a jet engine coming out of his impostor’s butt (applyForce/applyImpulse or setLinVel)… instead of translating player into the air.

That’s just me. With translate… you are sort of “pulling the physicsImpostor-around by its mesh”. Safer to pull the mesh-around… by its physicsImpostor. shrug.

Your “new” keypresses won’t really “set” a linearVelocity, but instead… ADD-to a CURRENT LV. So, you know, ya gotta “add” your jump-movement… “politely”:

playerImp.setLinearVelocity(playerImp.getLinearVelocity().add(new BABYLON.Vector3(0, 1, 0)))

(a jump, done politely, without disturbing any CURRENTLY-happening LV.) :slight_smile:

1 Like

Seems like it all works as expected, no?
I dont quite follow what the problem is in #10, but to me it seems like it is working wonderfully :slight_smile:

1 Like

Yeah, that was all confusing. If you go back to , You can see the sphere y speed. after it passes -50, it gets set back to -25.If I was to set it so that after it hits -25 it gets set to -25, I would essentially make a speed barrier or limit.

I my main pg holding w and space made you go SUPER fast, which is not what I want. I wanted to incorporate what worked in the above pg into my main one. I did, but in #10, it doesn’t work. In #11, it does, but only because I copy-pasted the working code fron the above pg into my main pg. I modified the values and it works.

Wingnut is telling about issues I might have with the movement, and how it can be improved.

Thanks for helping!

I just realized why it wasn’t working!
It’s looking for the ANGULAR velocity. not the LINEAR velocity. MAJOR FACEPALM.

Now it works. I just have to account for the players y rotation.