android - Shaky Camera Pan AndEngine -


i have been trying find best way move camera dragging background scene. have not found resource yet. pretty new android programming , using andengine.

here code far

public class mainactivity extends simplebasegameactivity{  private static final int camera_width = 800; private static final int camera_height = 480;  private smoothcamera msmoothcamera; private bitmaptextureatlas mbitmaptextureatlas; private textureregion mfacetextureregion; private sprite face; private float lastxmove; private float lastymove;   @override public engineoptions oncreateengineoptions() {      msmoothcamera = new smoothcamera(0, 0, camera_width-200, camera_height-200, 400, 400, 2);     msmoothcamera.setbounds(0f,0f,camera_width, camera_height);     cameralastx = msmoothcamera.getcenterx();     cameralasty = msmoothcamera.getcentery();     return new engineoptions(true, screenorientation.landscape_fixed, new ratioresolutionpolicy(camera_width, camera_height), msmoothcamera); }  @override public void oncreateresources() {     this.mbitmaptextureatlas = new bitmaptextureatlas(this.gettexturemanager(), 28, 247, textureoptions.bilinear);     this.mfacetextureregion = bitmaptextureatlastextureregionfactory.createfromasset(this.mbitmaptextureatlas, this, "tower.png", 0, 0);     this.mbitmaptextureatlas.load(); }  @override public scene oncreatescene() {     this.mengine.registerupdatehandler(new fpslogger());      final scene scene = new scene();     scene.setbackground(new background(0.09804f, 0.6274f, 0.8784f));      final float centerx = (camera_width-100 - this.mfacetextureregion.getwidth()) / 2;     final float centery = (camera_height-100 - this.mfacetextureregion.getheight()) / 2;      face = new sprite(centerx, centery, this.mfacetextureregion, this.getvertexbufferobjectmanager()) {         @override         public boolean onareatouched(final touchevent pscenetouchevent, final float ptoucharealocalx, final float ptoucharealocaly) {             this.setposition(pscenetouchevent.getx() - this.getwidth() / 2, pscenetouchevent.gety() - this.getheight() / 2);             return true;         }     };      scene.attachchild(face);     scene.registertoucharea(face);     scene.settouchareabindingonactiondownenabled(true);     scene.setonscenetouchlistener(new ionscenetouchlistener() {          @override         public boolean onscenetouchevent(scene pscene, touchevent pscenetouchevent) {             if (pscenetouchevent.isactiondown()) {                 lastxmove = pscenetouchevent.getx();                 lastymove = pscenetouchevent.gety();             }             if (pscenetouchevent.isactionmove()) {                 float differencex = pscenetouchevent.getx() - lastxmove;                 float differencey = pscenetouchevent.gety() - lastymove;                 msmoothcamera.setcenter(msmoothcamera.getcenterx() - differencex, msmoothcamera.getcentery() - differencey);                 lastxmove = pscenetouchevent.getx();                 lastymove = pscenetouchevent.gety();              }             if (pscenetouchevent.isactionup()) {              }             return true;         }     });     return scene; } 

currently code produce limited camera view , when try test , drag background sprite shake. have tried creating threshold small of movement not trigger drag. threshold make not shake sprite starts being little jumpy.

if there better way go or resource have not seen yet better pan camera, nudge in right direction appreciated.

with little bit of playing around , looking answers, found worked. changing part of onscenetouchevent function this:

if (pscenetouchevent.isactiondown()) {         // obtain initial touch coordinates when scene first pressed         minitialtouchx = pscenetouchevent.getx();         minitialtouchy = pscenetouchevent.gety();     }      if(pscenetouchevent.isactionmove()){         if (!touchspritelock && !zoomlock) {             // calculate offset touch coordinates             final float touchoffsetx = minitialtouchx - pscenetouchevent.getx();             final float touchoffsety = minitialtouchy - pscenetouchevent.gety();              // apply offset touch coordinates current camera coordinates              mcamera.setcenter(mcamera.getcenterx() + touchoffsetx, mcamera.getcentery() + touchoffsety);           }     } 

i on complicating it


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -