# www.jpct.net

## jPCT-AE - a 3d engine for Android => Support => Topic started by: haijin on August 17, 2011, 01:56:01 pm

Title: pivot rotation
Post by: haijin on August 17, 2011, 01:56:01 pm
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:
Code: [Select]
`    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?
Title: Re: pivot rotation
Post by: EgonOlsen on August 18, 2011, 07:15:53 am
Rotation pivot is an object space location, not a world space one. They only match if the model isn't transformed in any way. Personally, i find it pretty hard to think in this "the road moves while the car stands still"-way. Why don't you transform the car instead and let the road stand still...that's how it actually works in RL and that's how our brains are wired...at least mine.
Title: Re: pivot rotation
Post by: haijin on August 18, 2011, 10:17:39 am