Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

More specifically, I have rotation working in a way I don't like as follows:

    //Draw the triangle using this draw method
    //batch.draw(texture, x,                            y,                                          originX,                 originY,                                        width,               height,      scaleX, scaleY, rotation, srcX, srcY, srcWidth,   srcHeight, flipX, flipY)
    batch.draw(triangle, 240 - triangle.getWidth() / 2, (float) (400 - triangle.getWidth() * .289), triangle.getWidth() / 2, (float) ((double) triangle.getWidth() * 0.288), triangle.getWidth(), triangle.getHeight(), 1, 1, rotate, 0, 0, triangle.getWidth(), triangle.getHeight(), false, false);

    batch.end();

    //Touch input
    if(Gdx.input.isTouched()){
        Vector3 touchpos = new Vector3();
        touchpos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchpos);
        rotate = touchpos.x;
    }

This rotates the triangle around the center of the triangle that is at the center of the screen (the 0.289 corresponds to the height of the middle of an equilateral triangle according to its side length).

The rotation will be equal to the touch position on the screen which works great when the finger is dragged. However, what this code does is moves the rotation to the specific location whenever I touch the window with the mouse. What I would like it to do is to rotate the triangle when the mouse is dragged, but only based on its previous rotation, not its absolute rotation. For example, if I click and drag the mouse from the left side of the window to the right, the triangle will rotate accordingly. When I lift the mouse then click and drag it the same way a second time, I don't want it to reset it's position to my 2nd click, I want it to move the amount I dragged my mouse from it's previous rotation.

Can anyone help with this? I've been trying to wrap my head around it for hours. Let me know if you need any clarifications.

share|improve this question
add comment

2 Answers

rotate += touchpos.x;

You may want to store the x of where the touch "starts" and do something like:

rotate += touchpos.x - startTouch.x;

(that way a drag that starts at the middle of the screen doesn't imply a large rotation ...). To capture the touch start, use justTouched() to determine if this is a new touch.

share|improve this answer
    
Thanks, this got me closer, but I couldn't get it to work. The triangle would spin even when I touched the window at all (I may have been implementing your advice incorrectly). Regardless, I found a way to do it with libGDX's input.getDeltaX function. Works exactly as I wanted. Thanks for your help. –  Etai Dvora Apr 2 '13 at 4:56
add comment

I fixed my problem by using libGDX's built in input function getDeltaX(). It does exactly what I described that I want it to do. My isTouched() if statement now looks like this:

    if (Gdx.input.isTouched()){
        Vector3 touchpos = new Vector3();
        touchpos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchpos);
        rotate += Gdx.input.getDeltaX();
        myTriangle.setAngle((int) rotate);
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.