Author Topic: pivot rotation  (Read 1317 times)

Offline haijin

  • byte
  • *
  • Posts: 37
    • View Profile
pivot rotation
« 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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: pivot rotation
« Reply #1 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.

Offline haijin

  • byte
  • *
  • Posts: 37
    • View Profile
Re: pivot rotation
« Reply #2 on: August 18, 2011, 10:17:39 am »
mmm... moving the car instead of the road... a bit radical, but it could work... ;)
the thing is I had a 'mental block' for the road always coming down the screen (so that the car's left and right are always the same), but if I move and rotate the camera relative to the car I should get the same effect...
and when you realize that, the previous alternative is a bit over-complicated (and silly)... sometimes you get obsessed with one way of doing things and it's just a matter of a fresh pair of eyes to solve it :)

thanks!