pan 3D model using libgdx - android

I am trying to pan a 3d object but it does not seem to work. I make a swipe gesture towards the left and right to pan the object along the x-axis. I have added a Log statement and that clearly shows me the pan values in the logcat but still the object doesnt move.
Here is my code
public class 3DTest implements ApplicationListener, GestureListener {
public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;
public Environment environment;
public OrthographicCamera camera;
public CameraInputController camController;
public PerspectiveCamera cam;
public ModelLoader loader;
#Override
public void create() {
// TODO Auto-generated method stub
InputMultiplexer im = new InputMultiplexer();
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
camera = new OrthographicCamera(1280, 720);
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(1f, 1f, 1f);
cam.lookAt(0,0,0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();
camController = new CameraInputController(cam);
loader = new ObjLoader();
model = loader.loadModel(Gdx.files.internal("data/cube.obj"));
instance = new ModelInstance(model);
im.addProcessor(new GestureDetector(this));
im.addProcessor(camController);
Gdx.input.setInputProcessor(im);
}
#Override
public void dispose() {
// TODO Auto-generated method stub
modelBatch.dispose();
model.dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void render() {
// TODO Auto-generated method stub
camController.update();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instance, environment);
modelBatch.end();
}
#Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public boolean fling(float arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean longPress(float arg0, float arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean pan(float arg0, float arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
Log.i("App","pan" +arg2);
camera.translate(arg2, 0);
camera.update();
return false;
}
#Override
public boolean panStop(float arg0, float arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean pinch(Vector2 arg0, Vector2 arg1, Vector2 arg2, Vector2 arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean tap(float arg0, float arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(float arg0, float arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean zoom(float arg0, float arg1) {
// TODO Auto-generated method stub
return false;
}
}

cam.position.set(1f, 1f, -10f);
Change your "z" (the third parameter) in cam.
It works fine.

Related

TabPageIndicator.scrollTo is not working

I want to implement interactive TabPageIndicatot in android. I am using ViewPager.TabPageIndicator to make object of indicator.I am trying to implement scrollTo in TabPageIndicator class.
This is the code in my class. indicator is object of TabPageIndicator.
public void onPageScrolled(int pageNo, float arg1, final int arg2) {
// TODO Auto-generated method stub
indicator.scrollTo(arg2, 0);
}
This is the code in TabPageIndicator class.
private void animateToTab(final int x) {
if (mTabSelector != null) {
removeCallbacks(mTabSelector);
}
mTabSelector = new Runnable() {
public void run() {
// final int scrollPos = tabView.getLeft() - (getWidth() - tabView.getWidth()) / 2;
smoothScrollTo(x, 0);
mTabSelector = null;
}
};
post(mTabSelector);
}
#Override
public void scrollTo(int x, int y) {
// TODO Auto-generated method stub
super.scrollTo(x, y);
//Log.i("Hi", Integer.toString(x));
animateToTab(x);
}
Here animateToTab() function is called infinite times. Why?

Frames in live wallpaper not changing

Currently I am working on live wallpaper and I am stuck at a point. Actually, my live wallpaper only works when I touch the screen, but it doesn't change continuously. There is a problem in my loop.
class Diwali extends Engine {
private boolean mVisible;
private final Runnable diwaliImg = new Runnable() {
public void run() {
drawFrame();
}
};
int i=0;
int[] pirates = {
R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4,
R.drawable.a5, R.drawable.a6,
R.drawable.a7, R.drawable.a8,
R.drawable.a9, R.drawable.a10,
R.drawable.a11, R.drawable.a12,
R.drawable.a13, R.drawable.a14
};
#Override
public void onCreate(SurfaceHolder holder){
super.onCreate(holder);
}
#Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(diwaliImg);
}
#Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
drawFrame();
} else {
mHandler.removeCallbacks(diwaliImg);
}
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
drawFrame();
}
#Override
public void onSurfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceCreated(holder);
}
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(diwaliImg);
}
#Override
public void onOffsetsChanged(float xOffset, float yOffset, float xStep,float yStep, int xPixels, int yPixels) {
drawFrame();
}
#Override
public void onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
}
private void drawFrame() {
// TODO Auto-generated method stub
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
drawPirate(c);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(diwaliImg);
}
private void drawPirate(Canvas c) {
// TODO Auto-generated method stub
Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]);
i++;
if (i == 13) {
i = 0;
}
Matrix matrix = new Matrix();
c.drawBitmap(icon, matrix, null);
icon.recycle();
}
}
After adding this
if (mVisible) {
Handler.postDelayed(diwaliImg,80);
}
At the end draw frame() my problem was solved.
There is no loop in your code. You need to call drawFrame() repeatedly.
To achieve this you may use separate thread for animation. I recommend you my wallpaper template available on GitHub.

Killing a Sprite in AndEngine

Hi im having a bit of problems getting my sprite to fade back in. Im using paralell entity modifiers, scaling and fading after which the sprite gets new X and Y cordinates and fades in and scales back up. It doesnt work and i think it might have something to do with the onUpdate method.
XOsprite = new Sprite(x, y, XO, engine.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent te, final float xVal,
final float yVal) {
XOsprite.registerEntityModifier(downOut); //kill sprite
isKilled = true;
XOsprite.registerUpdateHandler(new IUpdateHandler() {
#Override
public void reset() {
// TODO Auto-generated method stub
}
#Override
public void onUpdate(float pSecondsElapsed) {
// TODO Auto-generated method stub
totalElapsedTime += pSecondsElapsed;
if(totalElapsedTime >= 3.0f){
BaseGameActivity gameActivity = (BaseGameActivity) activity;
gameActivity.runOnUpdateThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(isKilled){
reviveSprite();
isKilled = false;
}
}
});
}
//reset();
}
});
return true;
}
};
XOsprite.registerEntityModifier(inUp);
gameScene.attachChild(XOsprite);
gameScene.registerTouchArea(XOsprite);
return gameScene;
}
-edit- more code
public void reviveSprite(){
setSprites();
XOsprite.unregisterEntityModifier(downOut);
XOsprite.registerEntityModifier(inUp);
}
public void setSprites(){
if (rand.nextInt(2) == 0) {
XO = X;
} else {
XO = O;
}
x = rand.nextInt(MainActivity.CAM_WIDTH);
y = rand.nextInt(MainActivity.CAM_HEIGHT);
}
you are already on the UpdateThread, so no need to do this
gameActivity.runOnUpdateThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
if(isKilled){
reviveSprite();
isKilled = false;
}
}
});
just do this
if(isKilled){
reviveSprite();
isKilled = false;
}

scene2D in libgdx -Animation not working

I want an animated splash screen like fade in fade out for which I am doing in resize method as follows
public class SplashScreen extends GamePlayScreen {
#Override
public void resize(int width, int height) {
super.resize(width, height);
stage.clear();
Drawable splashDrawable = new TextureRegionDrawable(region);
splashImage = new Image(splashDrawable, Scaling.stretch);
splashImage.setFillParent(true);
splashImage.getColor().a = 0f;
splashImage.addAction(Actions.sequence(Actions.fadeIn(0.75f),
Actions.delay(1.75f), Actions.fadeOut(0.75f),
new Action() {
#Override
public boolean act(float delta) {
// the last action will move to the next screen
System.out.println("moving to next screen");
splashGameObj.setScreen(new GamePlayScreen(
splashGameObj));
return true;
}
}));
stage.addActor(splashImage);
}
}
Try changing your new Action() to new RunnableAction(){ public void run(){.....
Herer are more actions also some other explanation how they work. refare to an other question.
->Actions
Also take a look at:
screen2D, libgdx wiki about actions
Declare this variable
private Stage stage;
#Override
public void render(float delta) {
// TODO Auto-generated method stub
update();
draw(delta);
}
private void draw(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw()
}
#Override
public void resize(int width, int height) {
stage.setViewport( width, height, true );
}
#Override
public void show() {
// TODO Auto-generated method stub
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Image splashImage = new Image(region);
splashImage.addAction( Actions.sequence( Actions.fadeOut( 0.0001f ), Actions.fadeIn( 5f ),
Actions.run(onSplashFinishedRunnable) ) );
stage.addActor(splashImage);
}
Runnable onSplashFinishedRunnable = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
splashGameObj.setScreen(new GamePlayScreen(splashGameObj));
}
};

thread is throwing error (frame animation)

I am working on frame animation in which i am using thread but thread is throwing me error .
my code is as follow ....
public class Newton_LawsActivity extends Activity implements OnTouchListener, OnGestureListener,OnDoubleTapListener {
/** Called when the activity is first created. */
OnTouchListener l;
ImageView animation;
ImageView hideimage;
TextView t;
Thread timer;
int Rid;
double pixel;
String law="null";
private GestureDetector gestureDetector;
float x,y;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this);
// the frame-by-frame animation defined as a xml file within the drawable folder
animation = (ImageView)findViewById(R.id.image);
//animation.setBackgroundResource(R.drawable.anatomy_5);
hideimage=(ImageView)findViewById(R.id.imagein);
t=(TextView) findViewById(R.id.t);
animation.setBackgroundResource(R.anim.startanimation);
final AnimationDrawable newtonAnimation = (AnimationDrawable) animation.getBackground();
AnimationStart(newtonAnimation);
animation.setOnTouchListener(this);
}
public void AnimationStart(final AnimationDrawable newanimation){
timer=new Thread(){
#Override
public void run(){
try{
}catch(Exception e){}
finally{
Newton_LawsActivity.this.runOnUiThread(new Runnable() {
public void run(){
newanimation.start();
}});
}
}
};
timer.start();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(this, "animation", Toast.LENGTH_LONG).show();
return gestureDetector.onTouchEvent(event);
}
private void animationswitch(float x, float y) {
// TODO Auto-generated method stub
pixel = getmaskpixel(x,y,animation,hideimage);
Log.w("DEBUG","which is null:pixel" + pixel );
if (pixel==-583672){
animation.setBackgroundResource(R.anim.firstlaw_01);
final AnimationDrawable firstlaw_01 = (AnimationDrawable) animation.getBackground();
law="firstlaw_02";
firstlaw_01.start();
animationstop_01();
}
t.setText(Double.toString(pixel));
}
private double getmaskpixel(float x, float y, ImageView view,ImageView hideimage) {
// TODO Auto-generated method stub
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.touchnewtonfinal415);
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) view.getWidth()) / width;
float scaleHeight = ((float) view.getHeight()) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// matrix.postRotate(90);
// recreate the new Bitmap
Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height,matrix, false);
Log.w("DEBUG","which is null:image " + hideimage.getWidth() + " OR " +hideimage.getHeight() );
double bmWidth = view.getWidth();
double bmHeight = view.getHeight();
if ( x < 0 || y < 0 || x > hideimage.getWidth() || y >hideimage.getHeight()){
return 0; //Invalid, return 0
}else{
//Convert touched x, y on View to on Bitmap
int xBm = (int)(x * (bmWidth / hideimage.getWidth()));
int yBm = (int)(y * (bmHeight / hideimage.getHeight()));
return bitmap.getPixel((int)xBm,(int) yBm);
}
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
// TODO Auto-generated method stub
}
#Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y= e.getY();
Log.w("DEBUG","which is null:image " + x + " OR " +y);
animationswitch(x, y);
return true;
}
void animationstop_01(){
if(law=="firstlaw_02"){
timer=new Thread(){
#Override
public void run(){
try{
Thread.sleep(8750);
}catch(Exception e){}
finally{
Log.w("debug","it is firstlaw_02");
animation.setBackgroundResource(R.anim.firstlaw_02);
final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground();
law="firstlaw_03";
AnimationStart(firstlaw_02);
}};
};
timer.start();
}
}
}
it is throwing me following error..
06-06 14:15:00.668: E/AndroidRuntime(966): FATAL EXCEPTION: Thread-10
06-06 14:15:00.668: E/AndroidRuntime(966): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.invalidate(View.java:5139)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.setBackgroundDrawable(View.java:7486)
06-06 14:15:00.668: E/AndroidRuntime(966): at android.view.View.setBackgroundResource(View.java:7395)
06-06 14:15:00.668: E/AndroidRuntime(966): at Newtons.law.Newton_LawsActivity$2.run(Newton_LawsActivity.java:183)
Change your animationstop_01 method to:
void animationstop_01(){
if(law=="firstlaw_02"){
timer=new Thread(){
#Override
public void run(){
try{
Thread.sleep(8750);
}catch(Exception e){
}finally{
Log.w("debug","it is firstlaw_02");
Newton_LawsActivity.this.runOnUiThread(new Runnable() {
public void run(){
animation.setBackgroundResource(R.anim.firstlaw_02);
final AnimationDrawable firstlaw_02 = (AnimationDrawable) animation.getBackground();
law="firstlaw_03";
AnimationStart(firstlaw_02);
}
});
}
};
};
timer.start();
}
}
Error is inside the mehod name animationstop_01() where you are trying,
animation.setBackgroundResource(R.anim.firstlaw_02);
put that inside runOnUiThread()
try this:
animation.post(new Runnable() {
public void run() {
animation.setBackgroundResource(R.anim.firstlaw_02);
}
});
In that way you are posting it to the UI thread. View can only be changed from the original thread that created the View.

Categories

Resources