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

physijs
#1

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!

0 Likes

#2

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…

0 Likes

#3

this didn’t work.

0 Likes

#4

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

0 Likes

#5

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 );

0 Likes

#6

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

0 Likes

#7
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 ) );
0 Likes

#8

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 );
0 Likes

#9

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 );

0 Likes

#10

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

0 Likes

#11

its a physics object

0 Likes

#12

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

0 Likes

#14

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

0 Likes

#15

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:

0 Likes

#16

Anyone? :frowning:

0 Likes

#17

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)

0 Likes

#18

There isn’t any controller for this in physijs

0 Likes

#19

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.

0 Likes

#20

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

0 Likes

#22

Can someone please be of assistance?

0 Likes