# libgdx sphere rotation after movement

I'm moving my sphere on the screen using this method:

Matrix4 m = myBall.body.getCenterOfMassTransform();
Vector3 translation = new Vector3();
m.getTranslation(translation);

float multiplier = delta * 4 * game.platform.accelerometerFactor();
movement = new Vector3(Gdx.input.getAccelerometerY() * multiplier, 0, Gdx.input.getAccelerometerX() * multiplier);

m.setTranslation(translation);
myBall.body.setCenterOfMassTransform(m);


Then I would like to rotate my sphere during movement. So I counted angle which sphere should be rotated and then applied it before I call myBall.body.setCenterOfMassTransform(m) using:

float x = movement.x;
float z = movement.z;
Vector3 axis = new Vector3(z, 0, -x);
m.rotate(axis, angle);


But the sphere is rotating strangely. Where is the problem in my process of rotation? • What do you mean by "strangely"? Does it rotate around your chosen axis a = (z,0,-x) or not? – mvw Mar 10 '16 at 11:28
• Strangely means it does not move like in normal world when it is moving on surface. Yes it doesn't move correctly around axis = (z,0,-x) – Adrian Śliwa Mar 10 '16 at 12:27
• I wonder if, depending on your multiplication conventions, the rotation is being applied with respect to the ball's local x & z axes (which wobble around as it rolls to produce a chaotic result) rather than the world's x & z axes. Switching to one of the matrix's premultiplication methods would be a way to test for this. – DMGregory Jul 8 '17 at 1:02

a = v x (-e_z) = (vx, vy, vz) x (0, 0, -1) = (-vy, vx, 0)

where v is the velocity vector, e_z the unit vector in z direction and x the vector product.