[SOLVED] Best way to push object along its "forward" axis?

Is this the best way to push something forward, or is my maths/method completely off?

speedvector = -this.entity.forward * (this.drive_currentSpeed);
this.entity.rigidbody.applyImpulse(speedvector.x,speedvector.y,speedvector.z);

I think in theory this should push the object along it’s forward vector by whatever drive_currentSpeed is set to.

But in reality I get strange behavior, the object is jittery and then sails upwards into the sky? (The object in question is a collision capsule, with rigid body and the script attached. So it’s middle point should be directly in the center of the object, from my understanding?)

Given an object’s rotation, and the this.entity.forward vector, how can I “push” the rigidbody with the correct impulse direction? I keep trying different formulas but end up either not moving the object at all, or having it spazz out into space.

Check out the section in the User Manual on forces and impulses:

http://developer.playcanvas.com/en/user-manual/physics/#moving-rigid-bodies-with-forces

As you can see from the docs, you really ought to be applying a force rather than an impulse.

Also, you can’t use a ‘*’ (multiplication) operator on a pc.Vec3. Instead, you should do something like:

this.force.copy(this.entity.forward).scale(strength);
this.entity.rigidbody.applyForce(this.force);

You would preallocate the ‘this.force’ vector in your initialize function (to avoid runtime allocation of the vector) as follows:

initialize: function () {
    this.force = new pc.Vec3();
},
1 Like

Thanks will, that seems to be working.

A new issue with the above is that turning the object doesn’t actually change where it’s current velocity is going, only where it is being pushed from. This leads to a “sliding on ice” feeling to the movement. I actually solved that myself using this line instead:

this.entity.rigidbody.linearVelocity = this.force;

Though it is certainly not a perfect solution, overwriting the linearVelocity directly seems to do the trick for now(force the object in the direction desired while still respecting walls in the way). I will need to figure out some code to handle bouncing off of walls and such, as the above rightfully gets the object “stuck” to any walls it hits. But collisions are a whole other topic entirely. You’ve gotten me off in the right direction, at least. :wink:

EDIT: LOL, I just discovered this new method of movement also eliminates the effect of gravity on the object. Now my character floats over all the wonderful holes I left for them to jump over. Figured I would mention that for anyone else trying the above. :unamused:

You can use linear damping and friction to compensate for the sliding.

http://developer.playcanvas.com/en/user-manual/packs/components/rigidbody/

Linear velocity is normally derived by the physics engine and you shouldn’t normally set it directly yourself. Exceptions are when you want to reset a body, or maybe if you want to cap a body’s linear velocity to some maximum (although as I mentioned before, you can do this with careful selection of damping and friction values).

This is a good project to look at:

https://playcanvas.com/project/359952/overview/tutorial-first-person-movement

1 Like

Hi all,

I am late to the discussion, but moving the entity along the forward axis is really important in the game that I am developing currently. I am not quite following the discussion; it would really help if you could send the whole code.

Rgds,

Are you using physics or just want to move an entity forward?

If the former, the first person movement project does a pretty good of showing that: https://playcanvas.com/project/359952/overview/tutorial-first-person-movement

If the latter, then you can access the forward vector via the entity object (https://developer.playcanvas.com/en/api/pc.Entity.html#forward) and can use that to move the entity along the forward axis.

Physics, I initially tried apply force along with apply torque but it did not work.

It works when using translateLocal and rotateLocal, but not with a dynamic rigidbody

1 Like

Can you show an example project of what you were trying? Bear in mind that apply force/torque only works on ‘dynamic rigid bodies’ and not static or kinematic.

My rigid body is of dynamic type, can you tell me how the example above that @darkgriffin and @will were discussing ?