Sunday, May 19, 2013

Resuming sprite rotate from last angle

Resuming sprite rotate from last angle

I have large circle that I want to rotate with my finger, but I cant figure out how to do it.
I want it to behave this way: I can start rotating the circle from any point I want and then the point of circle I first touched should face my finger as I drag it around. Then when I release my finger it should keep it rotation and then on next touch continue from last angle.
I tried something like this:
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 world = new Vector3(screenX,screenY,0);
        core.getCamera().unproject(world);

        start = center.sub(new Vector2(world.x,world.y));
        prev = 0;
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        prev = 0;
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        Vector3 world = new Vector3(screenX,screenY,0);
        core.getCamera().unproject(world);

        Vector2 current = center.sub(new Vector2(world.x,world.y));

        float deltaAngle = current.angle()-start.angle();

        float angle = rotation.cpy().angle()+(deltaAngle)-prev;
        prev = angle;

        rotation.setAngle(angle);
        circle.setRotation(rotation.angle());
        return false;
    }
this method works only for first touch, after I release my finger and try to rotate it again, it rotates to some other angle and then starts to rotate from there.
does anyone see what is wrong with that method?

No comments:

Post a Comment