android - LibGDX Rotate 2D image with touchDragged() event -


objective: rotate image in center of screen movement equal left or right touchdragged event.

right have basic stage created , adds actor (centermass.png) stage. created , rendered this:

public class application extends applicationadapter { stage stagegameplay;  @override public void create () {     //setup game stage variables     stagegameplay = new stage(new screenviewport());     stagegameplay.addactor(new centermass(new texture(gdx.files.internal("centermass.png"))));      gdx.input.setinputprocessor(stagegameplay);  }  @override public void render () {     gdx.gl.glclearcolor(255f/255, 249f/255, 236f/255, 1f);     gdx.gl.glclear(gl20.gl_color_buffer_bit);     //before drawing, updating actions have changed     stagegameplay.act(gdx.graphics.getdeltatime());     stagegameplay.draw();      } } 

i have separate class file contains centermass class, extending image. familiar enough know extend actor, not sure benefit gain using actor vs image.

in centermass class create texture, set bounds, set touchable , center on screen.

inside centermass class have inputlistener listening events. have override set touchdragged trying x , y of drag, , use set rotate actions accordingly. class looks this:

//extend image vs actor classes public class centermass extends image { public centermass(texture centermasssprite) {     //let parent aware     super(centermasssprite);     setbounds(getx(), gety(), getwidth(), getheight());     settouchable(touchable.enabled);     setposition(gdx.graphics.getwidth()/2, gdx.graphics.getheight()/2);     setrotation(90f);       addlistener(new inputlistener(){         private int dragx, dragy;         private float duration;         private float rotateby = 30f;          @override         public void touchdragged(inputevent event, float x, float y, int pointer) {             //get             float dx = (float)(x-dragx)/(float)gdx.graphics.getwidth();             float dy = (float)(dragy-y)/(float)gdx.graphics.getheight();              duration = 1.0f; // 1 second              actions.sequence(                     actions.parallel(                             actions.rotateby(rotateby, duration),                             actions.moveby( dx, dy, duration)                     )             );           }     }); }  @override protected void positionchanged() {     //super.positionchanged(); }  @override public void draw(batch batch, float parentalpha) {     //draw needs available changing color , rotation, think     batch.setcolor(this.getcolor());     //cast texture because use image vs actor , want rotate , change color safely     ((textureregiondrawable)getdrawable()).draw(batch, getx(), gety(),             getoriginx(), getoriginy(),             getwidth(), getheight(),             getscalex(), getscaley(),             getrotation()); }  @override public void act(float delta) {     super.act(delta);     } } 

the problem: have not been able rotate way like. have been able shift around in unpredictable ways. guidance appreciated.

as code seems good. except don't set origin of image. without setting origin default set 0,0.(bottom left of image) if yow want rotate image origin centre have set origin imagewidth/2. imageheight/2.

 setorigin(imagewidth/2,imageheight/2)// 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -