[SOLVED] Linear factor in rigid body is not working

I wanted to restrict the motion of cube in only x-z plane so i set Linear factor’s y component to zero but it doesn’t stop cube from moving in y axis.Is linear factor broken or am i doing some thing wrong? by the way i am adding linear velocity to the cube

Can you post a link to your project?

As it is a private project i can not do that what i did is set resultant velocity to entity by
pickedEntity.rigidbody.linearVelocity = resultant Distance;
and set linear factor by
pickedEntity.rigidbody.linearFactor = new pc.Vec3(1,0,1);

It is bad to construct vector on runtime, especially as linearFactor is already a vector. You better do set to it.
Very effective way is to create small public project to replicate an issue.

Actually, doing this is wrong:

pickedEntity.rigidbody.linearFactor.set(1, 0, 1);

pickedEntity.rigidbody.linearFactor returns a vector which the call to set updates. But the engine receives no notification that the vector has changed!

The correct, and most performant approach is to cache a vector, say in the initialize function and then assign it in the update function. But always use the ‘=’ operator to assign to vectors (and colors) on components.

I have set the y component of linear factor to zero in editor but no result

take a look at sample project, cube still moves in y axis


So after some investigation it seems that linear factor only affects forces. If you manually set the linear velocity yourself, then linear factor will be skipped. That’s the way Ammo (or Bullet Physics) work. What you could do perhaps if you wanted to avoid that behavior is scale the linear velocity you apply yourself. Something like:

var linearFactor = this.entity.rigidbody.linearFactor;
var linearVelocity = (new pc.Vec3(0, 1, 0)).mul(linearFactor); // creates new pc.Vec3 every frame - don't do this in real life
this.entity.rigidbody = linearVelocity;
1 Like

Thank you vaios that worked wonderfully