Hi,
I've been a while trying to figure this one out for a prototype I'm working on: I have a racing car that remains static, with a top view camera on it and the ground moves as necessary. The ground (racetrack) needs to translate according to speed and rotate according to bearing. The rotation needs to be made relative to the point over the racetrack where the car is, so I understand I need to change the pivot point. I set the new pivot point relative to the transformed center (car started at center of world, and background was instanced and never relocated, so it should initially have it's center at world(0,0,0)). I think I'm close to solve to problem, and I guess it relates to the transformed center inversion, but after trying many variations I just can't find the issue... maths/geometry is not my thing.
So the piece of code below has a test bit to change steering by 45 (Math.PI/4) degrees every 20 ticks and places a cube at the calculated pivot point. First iteration works fine with pivot point set to (0, -20, 0), no previous rotations so only the translation is relevant. Second iteration, after the previous 45 degrees rotation, the pivot point is calculated correctly aprox. (-14, -34, 0), the test cube is placed on the right place but the background kind of translates twice, so the car appears off by double the distance on the previous direction... and then it just gets worse:
public void tick(Car car){
count++;
if (count%20 == 0){
car.angleChangeRatio = (float) (Math.PI / 4);
}
model.translate(0, car.speed, 0);
if (car.angleChangeRatio != 0){
model.getTransformedCenter(tmpVector);
//tmpVector.scalarMul(-1);
tmpVector.y = -tmpVector.y;
model.setRotationPivot(tmpVector);
model.rotateZ(car.angleChangeRatio);
Utils.log(model.getRotationPivot().toString());
// model.rotateMesh();
car.angleChangeRatio = 0;
Object3D show = Primitives.getCube(1);
show.translate(model.getRotationPivot());
model.addChild(show);
Utils.world.addObject(show);
}
}
any suggestions?