insanity design NeHe lesson 7 port question

Problems with Canvas, OpenGL, etc...

insanity design NeHe lesson 7 port question

Postby jbh » Fri Dec 31, 2010 5:28 pm

I have a simple problem that I am trying to figure out for the NeHe port lesson 7... all i want to do is be able to drag the cube around the screen ONLY when the user touches the cube... i have sucessfully gotten the cube to move but the user can be anywhere on the screen... how can I distinguish whether the persons finger is on the cube or not...

my variables xp and yp are my representation of the cubes x and y position on the screen gl.glTranslatef(xp,yp,z);

i have very little expierience in java but a bit in C/C++ so I may be on the wrong track completely... any help would be great.

/**
* Override the touch screen listener.
*
* React to moves and presses on the touchscreen.
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//
float x = event.getRawX(); //<- should this be getRawX or just getX ??
float y = event.getRawY(); // <-- same as above

//If a touch is moved on the screen
if(event.getAction() == MotionEvent.ACTION_MOVE) {
//Calculate the change
float dx = x - oldX;
float dy = y - oldY;
//Define an upper area of 10% on the screen
int upperArea = this.getHeight() / 10;

//Zoom in/out if the touch move has been made in the upper
if(y < upperArea) {
z -= dx * TOUCH_SCALE / 2;


} else {
if (y >= yp+0.1f || y <= yp-0.1f ) //<-- here is where i think the code should be corrected
{
yp -= dy * TOUCH_SCALE;

}
}

//A press on the screen
} else if(event.getAction() == MotionEvent.ACTION_UP) {
//Define an upper area of 10% to define a lower area
int upperArea = this.getHeight() / 10;
int lowerArea = this.getHeight() - upperArea;

//Change the light setting if the lower area has been pressed
if(y > lowerArea) {
if(light) {
//light = false;
} else {
//light = true;
}
}
}
jbh
Freshman
Freshman
 
Posts: 7
Joined: Fri Dec 31, 2010 4:46 pm

Top

Re: insanity design NeHe lesson 7 port question

Postby jonbonazza » Mon Jan 03, 2011 12:27 am

You need to convert the cube's x and y coordinates to from world space to screen space. Once you have that, you can easily compare the touch location to see if it falls inside of the cube's screen coordinates.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: insanity design NeHe lesson 7 port question

Postby jbh » Mon Jan 03, 2011 7:16 pm

so if the sprites x,y coord's are xp and yp... the would the compare function be something like this.getWidth() - xp+sprite_size and this.getHeight() -yp+ sprite_size ??

am i even on the right track???

could you possible provide a small snippet of code with your reply? i am 110% new to java and android though I have had some expierience with NeHe and C++

thanks for your time and thanks again in advance! :D

-jb
jbh
Freshman
Freshman
 
Posts: 7
Joined: Fri Dec 31, 2010 4:46 pm

Re: insanity design NeHe lesson 7 port question

Postby jbh » Mon Jan 03, 2011 9:23 pm

i would like to add that my sprite is represented as a 2D sprite with a set z position at -10.0f i have not yet understood orthographic projections on ES just yet soooo... the sprite at x,y,z position is centered in the screen at startup of the app... i hope this helps more... im still working on this my self so anyone please feel free to add a comment
jbh
Freshman
Freshman
 
Posts: 7
Joined: Fri Dec 31, 2010 4:46 pm

Re: insanity design NeHe lesson 7 port question

Postby jbh » Tue Jan 04, 2011 2:55 am

if I just set an ortho2d projection would I still run into this problem or would where my finger is would be the same as my sprite position?, I'm at work on my phone so I can't try it out till I get off.
jbh
Freshman
Freshman
 
Posts: 7
Joined: Fri Dec 31, 2010 4:46 pm

Re: insanity design NeHe lesson 7 port question

Postby jonbonazza » Tue Jan 04, 2011 7:02 am

The easiest way is to use the gluProject method:
http://developer.android.com/reference/ ... l/GLU.html
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Re: insanity design NeHe lesson 7 port question

Postby jbh » Tue Jan 04, 2011 8:15 am

Is there any good examples of this I can study?
jbh
Freshman
Freshman
 
Posts: 7
Joined: Fri Dec 31, 2010 4:46 pm

Re: insanity design NeHe lesson 7 port question

Postby jonbonazza » Tue Jan 04, 2011 7:25 pm

examples? it's one line of code... the parameters are given in the link to the documentation i provided. all of the parameters are easily obtained.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests