Android Can't get two virtual joysticks to move independently and at the same time
Posted
by
Cole
on Game Development
See other posts from Game Development
or by Cole
Published on 2013-10-20T08:11:17Z
Indexed on
2013/10/20
10:19 UTC
Read the original article
Hit count: 439
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
float r = 70;
float centerLx = (float) (screenWidth*.3425);
float centerLy = (float) (screenHeight*.4958);
float centerRx = (float) (screenWidth*.6538);
float centerRy = (float) (screenHeight*.4917);
float dx = 0;
float dy = 0;
float theta;
float c;
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
int pid = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int fingerid = event.getPointerId(pid);
int x = (int) event.getX(pid);
int y = (int) event.getY(pid);
c = FloatMath.sqrt(dx*dx + dy*dy);
theta = (float) Math.atan(Math.abs(dy/dx));
switch (actionCode) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
//if touching down on left stick, set leftstick ID to this fingerid.
if(x < screenWidth/2 && c<r*.8) {
lsId = fingerid;
dx = x-centerLx;
dy = y-centerLy;
touchingLs = true;
}
else if(x > screenWidth/2 && c<r*.8) {
rsId = fingerid;
dx = x-centerRx;
dy = y-centerRy;
touchingRs = true;
}
break;
case MotionEvent.ACTION_MOVE:
if (touchingLs && fingerid == lsId) {
dx = x - centerLx;
dy = y - centerLy;
}else if (touchingRs && fingerid == rsId) {
dx = x - centerRx;
dy = y - centerRy;
}
c = FloatMath.sqrt(dx*dx + dy*dy);
theta = (float) Math.atan(Math.abs(dy/dx));
//if touching outside left radius and moving left stick
if(c >= r && touchingLs && fingerid == lsId) {
if(dx>0 && dy<0) { //top right quadrant
lsX = r * FloatMath.cos(theta);
lsY = -(r * FloatMath.sin(theta));
Log.i("message", "top right");
}
if(dx<0 && dy<0) { //top left quadrant
lsX = -(r * FloatMath.cos(theta));
lsY = -(r * FloatMath.sin(theta));
Log.i("message", "top left");
}
if(dx<0 && dy>0) { //bottom left quadrant
lsX = -(r * FloatMath.cos(theta));
lsY = r * FloatMath.sin(theta);
Log.i("message", "bottom left");
}
else if(dx > 0 && dy > 0){ //bottom right quadrant
lsX = r * FloatMath.cos(theta);
lsY = r * FloatMath.sin(theta);
Log.i("message", "bottom right");
}
}
if(c >= r && touchingRs && fingerid == rsId) {
if(dx>0 && dy<0) { //top right quadrant
rsX = r * FloatMath.cos(theta);
rsY = -(r * FloatMath.sin(theta));
Log.i("message", "top right");
}
if(dx<0 && dy<0) { //top left quadrant
rsX = -(r * FloatMath.cos(theta));
rsY = -(r * FloatMath.sin(theta));
Log.i("message", "top left");
}
if(dx<0 && dy>0) { //bottom left quadrant
rsX = -(r * FloatMath.cos(theta));
rsY = r * FloatMath.sin(theta);
Log.i("message", "bottom left");
}
else if(dx > 0 && dy > 0) {
rsX = r * FloatMath.cos(theta);
rsY = r * FloatMath.sin(theta);
Log.i("message", "bottom right");
}
}
else {
if(c < r && touchingLs && fingerid == lsId) {
lsX = dx;
lsY = dy;
}
if(c < r && touchingRs && fingerid == rsId){
rsX = dx;
rsY = dy;
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
if (fingerid == lsId) {
lsId = -1;
lsX = 0;
lsY = 0;
touchingLs = false;
} else if (fingerid == rsId) {
rsId = -1;
rsX = 0;
rsY = 0;
touchingRs = false;
}
break;
}
return true;
}
There's a left joystick and a right joystick. Right now only one will move at a time. If someone could set me on the right track I would be incredibly grateful cause I've been having nightmares about this problem.
© Game Development or respective owner