java - Android. How to move object in the direction it is facing (using Vector3 and Quaternion) -
i'm using libgdx (quite new in fact) , android. , want move 3d object in direction facing (using speed). thinks it's basic question can't find straight nswer it. have quaternion representing object rotation(direction) , have vector3 representing object position. question how update position vector3 using info quaternion in order move object in direction represented quaternion. (an alternative extract roll pitch , yaw quaternion , new coords applying trigonometric calculations. think there must way achieve using vector3 , quat.)
quaternion used specify rotation. first need specify direction when no rotation applied. example if want move along x axis when no rotation applied:
vector3 basedirection = new vector3(1,0,0);
make sure base direction normalized (length = 1),you can use nor()
method safe:
vector3 basedirection = new vector3(1,0,0).nor();
next you'll need rotate direction using quaternion:
vector3 direction = new vector3(); quaternion rotation = your_quaternion; direction.set(basedirection); direction.mul(rotation);
now have direction, can scale amount want move it. you'll want every frame , depending on time elapsed since last frame.
final float speed = 5f; // 5 units per second vector3 translation = new vector3(); translation.set(direction); translation.scl(speed * gdx.graphics.getdeltatime());
finally need add translation position
position.add(translation);
of course, depending on actual implementation can batch multiple operations, e.g.:
translation.set(basedirection).mul(rotation).scl(speed * gdx.graphics.getdeltatime()); position.add(translation);
update: adding working code xoppa's comment:
translation.set(basedirection).rot(modelinstance.transform).nor().scl(speed * delta)
Comments
Post a Comment