Hi this forum seems pretty great and android mail lists pretty useless, my name is Neil, i am a bit bonkers.
I have a game i am tring to convert to android and soup up a little bit.. it is an 8 way isometric pacman clone with semi rubbish ghost AI ( used to be totally rubbish but i just improved it )
works fine enough with the DPAD, but i want it to work with the ball and touch as well, ball i havnt even looked at yet and not sure how i would test this until i get my hands on my brothers old android device
my main issues right now is getting the touch input to work, that game runs in a 320x240 play area so you can see the whole level at once, which i have in a surfaceview set in layout as being centered, so... for touch input i want to have it so
tx ty where touched, centerx centery center position of the screen
void dotouch(int tx, int ty) {
if (tx > centerx && ty > centery) {
ur_down = true;
}
if (tx < centerx && ty < centery) {
dl_down = true;
}
if (tx < centerx && ty > centery) {
ul_down = true;
}
if (tx > centerx && ty < centery) {
dr_down = true;
}
}
so, fair enough, apart from i just cant find/figure out -
a) the best way to find the center of the screen for centerx centery
b) where the heck the coordiantes of a touch event start from, top left of the screen? top left of my main view area? top left of the surfaceview i have centered in the view?
i have just robbed and pasted roberts code of the event queue, all is good apart from i'm not sure what is going on with it but will attempt to get that going once i figure out a and b above
thanks!
Neil
P.S. oh you can see the game i am talking about which originally in C and can get SDL version of it, crazeeman, here http://www.cloudsprinter.com/software/ now much improved from that version just for android ;)
Thanks, i eventually got this to behave, i did want diagonal movement as it is an isometric game, and as i suspected the surfaceview coordiantes do not take into account the tool bar and where the surfaceview is, and as in my layout i set the surface view in the center of the screen i need the center of the whole screen, so to find the center of the screen i used this in my OnCreate, after the views had been set
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mThread.setscreenxy(display.getWidth(),display.getHeight());
which gives me the center of the whole screen.
i appear to have your loverly touch screen codes running and was wondering how i can adapt it for track balls and dpads, i did attempt to make it accept key inputs but failed twice :(
anyways, thanks for all the help! without you many of us would be lost in a pit of android despair!
neil.
If you're drawing 2d and using a surfaceview, onSurfaceChanged will provide the viewport size. Just hang on to the width and height given there.
What your posted touch code will do is divide the screen into the 4 corners, and unless your player normally goes diagonally, you will want to rotate things 45 degrees.
An easy way to do it is to use a 2d vector. This code will give you a good up, down, left, right on the screen response. The middle of the screen will be very sensitive so you may want to only respond if the length of vector is greater than a certain scaled distance.
Try something like this:
onTouch(int x, int y) {
int centerX = surfaceWidth / 2;
int centerY = surfaceHeight / 2;
int vx = x - centerX;
int vy = y - centerY;
float angleRads = FloatMath.atan2(vx, vy);
// now check the angle of the vector, I use radians but to start, use degrees til you're used to them
float degrees = Math.toDegrees(angleRads);
// your value will be in the range -180 to 180, check every 90 degrees at 45 diagonals
if (degrees < -135 || degrees > 135) {
// respond to whichever direction this is (can't remember offhand)
} else if (degrees > 45 && degrees <= 135) {
// respond
} else if (degrees > -45 && degrees <= 45) {
// response
} else {
// must be 4th direction
}
}
If you want to add the distance threshold, just get the length of vector with FloatMath.sqrt(vx * vx + vy * vy) but remember that different screens have different lengths so scale accordingly!