Can't move character in direction it's facing?

Does anyone know in PhysiJS how I can make my three.js character move in the direction it is facing? I tried using setLinearVelocity, and it doesn’t work with rotation.

Thanks!

A simple way to do it:

var tempVec = new THREE.Vector3();
...
tempVec.set( 0, 0, - velocity ); // Forward local direction is -Z
character.localToWorld( tempVec );

// Here you apply velocity tempVec to character physics object...
// (I don't know PhisiJS interface)
characterPhys.setLinearVelocity( tempVec );

But, the correct way to move an object (Non-Player Character) is to use forces or an existing controller, not by setting the velocity in each frame. Obviously it is far more complex…

this didn’t work.

I don’t know if setLinearVelocity is the correct API method and what parameters does it have, you’ll have to check.

this is what i have in my up key & it won’t do anything :

this.__velocityZ = 3.0;

this.__currentVelocity = this.__velocityZ;
this.__tempVec = new THREE.Vector3 ( );

// Forward local direction is -Z

this.__tempVec.set ( 0.0, 0.0, -this.__currentVelocity );
this.__entity.localToWorld ( this.__tempVec );

You miss the last line, the setLinearVelocity…? Without it the code doesn’t do anything.

this.__velocityZ = 3.0;

this.__currentVelocity = this.__velocityZ;
this.__tempVec = new THREE.Vector3 ( );

// Forward local direction is -Z

this.__tempVec.set ( 0.0, 0.0, -this.__currentVelocity );
this.__entity.localToWorld ( this.__tempVec );

this.__entity.setLinearVelocity ( new THREE.Vector3 ( this.__entity.getLinearVelocity( ).x, this.__entity.getLinearVelocity( ).y, this.__currentVelocity ) );

Just use tempVec for the velocity:

this.__velocityZ = 3.0;

this.__currentVelocity = this.__velocityZ;
this.__tempVec = new THREE.Vector3 ( );

// Forward local direction is -Z

this.__tempVec.set ( 0.0, 0.0, -this.__currentVelocity );
this.__entity.localToWorld ( this.__tempVec );
this.__entity.setLinearVelocity ( this.__tempVec );

it’s still not moving, i tried adding console log :

this.__velocityZ = 30.0;

this.__currentVelocity = this.__velocityZ;
this.__tempVec = new THREE.Vector3 ( );

// Forward local direction is -Z

this.__tempVec.set ( 0.0, 0.0, -this.__currentVelocity );
this.__entity.localToWorld ( this.__tempVec );
this.__entity.setLinearVelocity ( this.__tempVec );

console.log ( this.__entity.position.z );

Ummm, the __entity is a three object or a physics object?
Do you see errors in the console?

its a physics object

Then you have to do .localToWorld on the Three object, not on the physics object.

it has to be a physi mesh or the gravity won’t work.

Ok, so this kinda’ works :

this.__currentVelocity = this.__velocityZ;
this.__tempVec = new THREE.Vector3 ( );

// Forward local direction is '+Y'???

this.__tempVec.set ( 0.0, this.__currentVelocity, 0.0 );
this.__entity.localToWorld ( this.__tempVec );

this.__entity.setLinearVelocity

(

    this.__tempVec

);

Problem is, when it’s dropping down & I hit Up arrow, it allows for it to slowly gain velocity on the ‘+Y’ axis until I let go of key & then it drops back down, if it’s on the ground, it moves in the ‘-Z’ direction even though ‘this.__tempVec.set ( )’ is set to the ‘+Y’ direction. Also, if I turn in the air & THEN move, it will move in the direction it is facing, but it won’t do this when it is on ground. :frowning:

Anyone? :frowning:

These are the typical problems you encounter when modifying objects velocities. The correct approach is to add forces or impulses in each frame (or using a controller that suits your needs and is already implemented in the physics engine)

There isn’t any controller for this in physijs

Then you will have to write your own controller. It must apply forces or impulses on the rigid body. The related Ammo rigid bodies functions are:

applyForce(btVector3 force, btVector3 rel_pos);
applyCentralForce(btVector3 force);
applyCentralLocalForce(btVector3 force);
applyTorqueImpulse(btVector3 torque);
applyImpulse(btVector3 impulse, btVector3 rel_pos);
applyCentralImpulse(btVector3 impulse);

An impulse is an instantaneous increment in momentum (force * deltaTime)
On the other hand, a force given with the previous methods is applied during the next physics timestep.

I suggest you to search the bullet documentation and forum.

Can someone please tell me why this is moving my Character in the +Y direction instead of the +Z direction?

// Forward local direction is '+Z'

this.__tempVec.set ( 0.0, 0.0, this.__currentVelZ );

this.__entity.localToWorld ( this.__tempVec );

this.__entity.setLinearVelocity ( this.__tempVec );

Thank you!


~Aerion

Can someone please be of assistance?