Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple tutorial onTouchEvent #428

Closed
sandrotra opened this issue Feb 26, 2013 · 17 comments
Closed

Simple tutorial onTouchEvent #428

sandrotra opened this issue Feb 26, 2013 · 17 comments
Labels

Comments

@sandrotra
Copy link

I would like a simple tutorial on how to move a camera or rotate an object when a touch event occurs.
Thanks!

@Davhed
Copy link
Member

Davhed commented Feb 26, 2013

You will have to write this behavior on your own.

Once you have onTouchEvent working its just a matter of capturing the values you want and applying them to the camera position. There are lots of tutorials around for handling mouse or touch events, and I think that's the best place to start. You can use the RajawaliExamples accelerometer example to get you going.

@sandrotra
Copy link
Author

It's obvious! But seeing these threads #258 and #133
i'm a little confused since there are only code snippets or interpretations from min3d.
Sorry but i'm new to rajawali and i haven't tested all the capability of this framework.
So i'm look for i clear and simple tutorial.

@Davhed
Copy link
Member

Davhed commented Feb 26, 2013

onTouchEvent is not Rajawali exclusive, so the Android tutorials apply in most cases.

The code I posted in #258 is complete, so you can also copy and paste that, tie in a few variables, and start picking that apart.

Aside from those options you will have to research and test this on your own. There is no tutorial written up for this in Rajawali.

@sandrotra
Copy link
Author

I tried to use your code #258 but doesn't work for me. Was i wrong or your code is part of the renderer class? Or maybe should i implement the OnTouchListener and use the onTouch method still in the renderer class?

@Davhed
Copy link
Member

Davhed commented Feb 27, 2013

Put my code in the renderer

@sandrotra
Copy link
Author

I have already done, but doesn't work!

@ToxicBakery
Copy link
Member

https://github.com/ToxicBakery/Mandelbrot/blob/master/src/com/ToxicBakery/apps/mandelbrot/MainActivity.java

That implements touch handling and gestures. It is the same way you implement basic touch handling for wallpapers.

@sandrotra
Copy link
Author

@ToxicBakery I have not still tried your solution but i wonder way the solution of Davhed doesn't work, since for him it is ok.

@ToxicBakery
Copy link
Member

Without seeing your code we can only speculate.

@Davhed
Copy link
Member

Davhed commented Feb 28, 2013

Since the only thing you need to do it "override" onTouchEvent() maybe you should begin there, and see if you can even get a response from the touch input. From there it's a simple matter of using the touch data to so what you want it to.

This is a complete example:

import javax.microedition.khronos.opengles.GL10;

import rajawali.renderer.RajawaliRenderer;
import android.content.Context;

public class WallpaperRenderer extends RajawaliRenderer{

    public WallpaperRenderer(Context context) {
        super(context);
        setFrameRate(60);
    }

    @Override
    public void initScene() {
          //Scene building happens here
    }

    @Override 
    public void onTouchEvent(MotionEvent me) {
          //The MotionEvent "me" has all of the data you need to make animation based on touch
           if (me.getAction() == MotionEvent.ACTION_DOWN) { //Called once at the start of the touch
                System.out.println(me.getAction() + "  --  " + me.getX() + ", " + me.getY()); 
           }
           if (me.getAction() == MotionEvent.ACTION_MOVE) { //Called repeatedly as touch point moves
                System.out.println(me.getAction() + "  --  " + me.getX() + ", " + me.getY());
           }
           if (me.getAction() == MotionEvent.ACTION_UP) { //Called once at the end of the touch
                System.out.println(me.getAction() + "  --  " + me.getX() + ", " + me.getY());
           }    
           try {
               Thread.sleep(15); //Small delay to keep touch events from overflowing and decreasing performance
           } catch (Exception e) {
           }
    }

    @Override
    public void onDrawFrame(GL10 glUnused) {
        super.onDrawFrame(glUnused);
    }
}

And there is a ton of data you can get from the MotionEvent.

@sandrotra
Copy link
Author

OK, the above code works fine, because it is simple. Now, the feat for me is to apply simple actions like move or rotate a camera or an object when the finger is moving. I'm trying to adapt your code #258 that is more articulated and little different then I would like but it's hard.

@Davhed
Copy link
Member

Davhed commented Mar 1, 2013

Try something fresh and new. My code is hacked together cause I'm not a mathematician.

@sandrotra
Copy link
Author

Please. In the swipeCamera() method there is a variable call "numScreens". What it refers to? Maybe to the number of wallpaper's home screens. How can i get it?

@Davhed
Copy link
Member

Davhed commented Mar 2, 2013

Its a preference I created. The user chooses from a list of three options. If you are looking to intimately tie your home screen animation to your scene animation you can try using onOffsetsChanged instead of onTouchEvent. It isn't as widely supported, but it's easier to use.

@Davhed
Copy link
Member

Davhed commented Mar 2, 2013

This is how I used it in that same app -

    @Override  //This method moves the camera using the Android home screen swipe output. It's a better way, but not always supported
    public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) {
                float zOffset = (50*xOffset)-25;
            mCamera.setX(25-(-((curZPos + zOffset)/50)));
            mCamera.setZ(curZPos + zOffset);
    }

@sandrotra
Copy link
Author

I tried onOffsetsChanged but it moves the camera only in one way.

@sumitsahoo
Copy link

Easy way to achieve it by using ArcballCamera

    ArcballCamera arcball = new ArcballCamera(mContext, ((Activity)mContext).findViewById(R.id.linear_layout));
            arcball.setTarget(object3D); //your 3D Object
            arcball.setPosition(0,0,4); //optional
            //arcball.setScreenMappingRatio(-1); // Not yet merged at master branch
    
    getCurrentScene().replaceAndSwitchCamera(getCurrentCamera(), arcball);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants