I'm a beginner, but I want to create a game. Now I am doing the moving function for my hero. When i touch the image and move it to the right side, the hero will go to the right side from the original position by 100dp per second, but its can't work.
Below is the code that i use as my moving function :
public View.OnTouchListener move = new View.OnTouchListener() {
private float x, y;
private int mx, my, hx, hy;
public boolean onTouch(final View v, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_DOWN)) {
x = event.getX();
y = event.getY();
mx = (int) (event.getRawX() - x);
my = (int) (event.getRawY() - y);
} else if ((event.getAction() == MotionEvent.ACTION_MOVE)) {
hx = (int) (event.getRawX() - x);
hy = (int) (event.getRawY() - y);
v.layout(hx, hy, hx + v.getWidth(), hy + v.getHeight());
} else if ((event.getAction() == MotionEvent.ACTION_UP)) {
while (hx > mx) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mx += 100;
v.layout(mx, my, mx + v.getWidth(), my + v.getHeight());
}
}, 1000);
}
}
Log.e("x =", String.valueOf(x) + " y =" + String.valueOf(y));
Log.e("hx =", String.valueOf(hx) + " hy =" + String.valueOf(hy));
Log.e("mx =", String.valueOf(mx) + " my =" + String.valueOf(my));
return true;
}
};
Do this in ACTION_MOVE and ACTION_UP:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
//edit layout params according to your need
view.setLayoutParams(layoutParams);
Related
I am trying to create a simple find the difference game in android by using touch coordinates of each difference but when I note down the coordinates in one device then it's working fine. and when I use some other device those coordinates aren't there. so is there any way in which I can work this out
here's some of the code:
image1.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint({"ClickableViewAccessibility", "UseCompatLoadingForDrawables"})
#Override
public boolean onTouch(View v, MotionEvent event) {
int X =(int) event.getX();
int Y =(int) event.getY();
String msg = "Coordinates are " + X + "and" + Y;
int eventAction = event.getAction();
if(eventAction == MotionEvent.ACTION_DOWN){
int x = (int) event.getX();
int y = (int) event.getY();
if(checkPoint(X,Y) == true){
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
private boolean checkPoint(int x, int y) {
int touchX = (int) x;
int touchY = y;
float centerX = 950, centerY = 600;
float radius = 100;
if (Math.pow(centerX-touchX , 2)
+ Math.pow(centerY-touchY , 2) < Math.pow(radius, 2))
{
Toast.makeText(MainActivity.this,"points inside circe",Toast.LENGTH_SHORT).show();
return true;
}
else
{
Toast.makeText(MainActivity.this,"points outside circle",Toast.LENGTH_SHORT).show();
return false;
}
return false;
}
Can anyone tell me why my movement class x, y, and theta values are showing the values expected. But when it's emulated/run the 'nib' is mirroring all the movements instead of being right under the touch point. I don't want to change the math for the values if I don't have to, I just need to have the view show the same thing that the math is saying.
public class MovementDial extends AppCompatImageView {
public float x, y, r, Radius, _theta, xShift, yShift, offset;
private double distance, distanceAdj;
private RectF _knobRect = new RectF();
private PointF nibCenter, touchPoint;
private RectF nibRect = new RectF();
OnAngleChangedListener _angleChangedListener = null;
public interface OnAngleChangedListener {
void onAngleChanged(float theta);
}
public MovementDial(Context context) {
super(context);
nibCenter = new PointF(_knobRect.centerX(), _knobRect.centerY());
}
public float getTheta() {
return _theta;
}
public void setTheta (float theta){
_theta = theta;
invalidate();
}
public void setOnAngleChangedListener(OnAngleChangedListener listener) {
_angleChangedListener = listener;
}
private int map(double x, double in_min, double in_max, double out_min, double out_max) //#author: Aaron Pabst
{
int mapVal = (int) ((int)(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
return mapVal;
}
#Override
public boolean onTouchEvent(MotionEvent e) {
yShift = e.getY();
xShift = e.getX();
touchPoint = new PointF();
touchPoint.x = (int)(e.getX() - _knobRect.centerX());
touchPoint.y = map(yShift,0,(_knobRect.height()-_knobRect.centerY()),(_knobRect.height()-_knobRect.centerY()), 0);
x = touchPoint.x;
y = touchPoint.y;
float theta = mapThetaCoords(x,y); //maps out theta values between 0 to 360 using a traditional unit circle layout.
setTheta(theta);
double Radian = Math.toRadians(_theta);
final double PIValue = Math.PI/180;
Log.i("Touch", "initial Xvalue is :" +touchPoint.x);
Log.i("Touch", "initial Yvalue is :" +touchPoint.y);
Log.i ("Touch", "Touch point 3 changed to: " + theta);
distance = (float) Math.hypot(x, y); //polar coordinate radius (r = sqrt x^2 + y^2)
if(e.getAction() == MotionEvent.ACTION_DOWN) {
Log.i("Touch", "Xvalue touch is :" +touchPoint.x);
Log.i("Touch", "Yvalue touch is :" +touchPoint.y);
Log.i ("Touch", "Touch point 6 changed to: " + theta);
if(yShift == Radian)
if(distance <= Radius) {
nibCenter.x = _knobRect.centerX() - (float)(Radius * Math.cos(Math.toRadians(theta)));
nibCenter.y = _knobRect.centerY() - (float)(Radius * Math.sin(Math.toRadians(theta)));
x = touchPoint.x;
y = touchPoint.y;
Log.i("Touch", "Xvalue touch2 is :" +touchPoint.x);
Log.i("Touch", "Yvalue touch2 is :" +touchPoint.y);
Log.i("Touch", "Xvalue nib2 touch is :" +nibCenter.x);
Log.i("Touch", "Yvalue nib2 touch is :" +nibCenter.y);
} else if(distance > Radius) {
Log.i("Touch", "Xvalue touch3 is :" +touchPoint.x);
Log.i("Touch", "Yvalue touch3 is :" +touchPoint.y);
nibCenter.x = _knobRect.centerX() - (float)(Radius * Math.cos(Math.toRadians(theta)));
nibCenter.y = _knobRect.centerY() - (float)(Radius * Math.sin(Math.toRadians(theta)));
x = touchPoint.x;
y = touchPoint.y;
Log.i("Touch", "Xvalue nib1 touch is :" +nibCenter.x);
Log.i("Touch", "Yvalue nib1 touch is :" +nibCenter.y);
}
invalidate();
return true;
} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
Log.i("Touch", "Xvalue2 move is :" + touchPoint.x);
Log.i("Touch", "Yvalue2 move is :" + touchPoint.y);
Log.i ("Touch", "Move value point 7 changed to: " + theta);
distanceAdj = distance;
distance = Math.min(distance, distanceAdj); //comparing changing polar radius values as coordinates change place.
if(distance <= Radius) { //check to see if polar radius coordinate is less or equal to the radius value
nibCenter.x = _knobRect.centerX() - (float)(distance * Math.cos(Math.toRadians(theta)));
nibCenter.y = _knobRect.centerY() - (float)(distance * Math.sin(Math.toRadians(theta)));
x = touchPoint.x;
y = touchPoint.y;
Log.i("Touch", "Xvalue move1 is :" + x);
} else if (distance > Radius){
Log.i("Touch", "Yvalue (greater than) move is :" + y);
nibCenter.x = _knobRect.centerX() - (float)(distance * Math.cos(Math.toRadians(theta) - 360));
nibCenter.y = _knobRect.centerY() - (float)(distance * Math.sin(Math.toRadians(theta) - 360));
x = touchPoint.x;
y = touchPoint.y;
Log.i("Touch", "Xvalue move2 is :" + x);
Log.i("Touch", "Yvalue move2 is :" + y);
}
invalidate();
return true;
} else if (e.getAction() == MotionEvent.ACTION_UP){
reset();
}
return false;
}
public float mapThetaCoords(float x, float y) {
double atan2 = Math.atan2(y , x);
float theta;
if (x >= 0 && y >= 0) { // Q 1
theta = (float) Math.toDegrees(atan2);
return theta;
} else if (x < 0 && y > 0) { // Q 2
theta = (float) Math.toDegrees(atan2);
return theta;
} else if (x < 0 && y < 0) { // Q 3
theta = (float) Math.toDegrees(atan2) + 360;
return theta;
} else if (x > 0 && y < 0) { // Q 4
theta = (float) Math.toDegrees(atan2) + 360;
return theta;
}
return 0;
}
public void reset(){
x = _knobRect.centerX()-(_knobRect.width()/2);
y = _knobRect.centerY()-(_knobRect.height()/2);
nibCenter.x = x;
nibCenter.y = y + _knobRect.centerY();
_theta = 0f;
distance = 0f;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
_knobRect.left = getPaddingLeft();
_knobRect.top = getPaddingTop();
_knobRect.right = getWidth()- getPaddingRight();
_knobRect.bottom = _knobRect.width();
offset = (getHeight() - _knobRect.height()) * 0.5f;
_knobRect.top += offset;
_knobRect.bottom += offset;
Radius = _knobRect.width() * 0.35f;
nibCenter.x = x + _knobRect.centerX();
nibCenter.y = y + _knobRect.centerY();
float nibRadius = Radius * 0.2f;
nibRect.left = nibCenter.x - nibRadius;
nibRect.top = nibCenter.y - nibRadius;
nibRect.right = nibCenter.x + nibRadius;
nibRect.bottom = nibCenter.y + nibRadius;
Paint knobPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
knobPaint.setColor(Color.BLACK);
Paint nibPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
nibPaint.setColor(Color.MAGENTA);
canvas.drawOval(_knobRect, knobPaint);
canvas.drawOval(nibRect, nibPaint);
}
I'm having a problem moving an image from a place, where I touch it with a finger on a screen. It means when I touch it, let say in a center of a button and want to move it, it gets me to teh left a top corner of a button and from that point, I can move it around, up, left, right and so on...I want to have a possibility to move a button from wherever user put a finger on a button
Code:
public class Joystick extends RelativeLayout implements View.OnTouchListener {
private Context context;
private ImageView backgroundImageView;
private ImageView buttonImageView;
float xx = 0;
float yy = 0;
private static final String TRANSLATIONX = "setTranslationX";
public Joystick(Context context) {
super(context);
this.context = context;
initSlider();
if(buttonImageView != null) {
buttonImageView.setOnTouchListener(this);
}
this.setClipChildren(false);
}
public ImageView setView ( String imageName) {
ImageView image = new ImageView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(layoutParams);
// load image
try {
// get input stream
InputStream ims = getContext().getAssets().open(imageName);
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
image.setImageDrawable(d);
} catch (IOException ex) {
}
return image;
}
public void initSlider() {
backgroundImageView = setView("background.png");
buttonImageView = setView("jostick.png");
addView(backgroundImageView);
addView(buttonImageView);
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float x = motionEvent.getRawX();
float y = motionEvent.getRawY();
float cx = buttonImageView.getWidth() / 2.f;
float cy = buttonImageView.getHeight() / 2.f;
float w = backgroundImageView.getWidth();
float h = backgroundImageView.getHeight();
double r = cx / 2.;
double dx = x - cx;
double dy = y - cy;
double hypot = Math.hypot(dx, dy);
double cos = dx / hypot; // cos
double sin = dy / hypot; // sin
double rcos = r * cos;
double rsin = r * sin;
double rdx = Math.abs(dx) < Math.abs(rcos) ? dx : rcos; // if,else
double rdy = Math.abs(dy) < Math.abs(rsin) ? dy : rsin;
Log.d("VALUES", "RAW X:" + motionEvent.getRawX() + ", X:" + motionEvent.getX() + ", CX:" + cx + ", CY:" + cy + ", dx:" + dx + ", dy:" + dy + ", Hypo:" + hypot + ", cos:" + cos + ", sin" + sin);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
view.setX(xx);
view.setY(yy);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
float transX = (float) (cx + rdx - w / 2.);
float transY = (float) (cy + rdy - h / 2.);
buttonImageView.setTranslationX(transX);
buttonImageView.setTranslationY(transY);
Log.d(TRANSLATIONX,"X:" + transX + ", Y:" + transY);
break;
}
return true;
}
}
Somewhere is a small bug. I hope for any help. Thanks
If anyone is interested or looking for some answer how to make a Joystick Button moving inside box, I have found a solution:
private Point calculate (float x, float y) {
float cx = buttonImageView.getWidth() / 2.f;
float cy = buttonImageView.getHeight() / 2.f;
double r = cx / 2.; // vrednost radius
double dx = x;
double dy = y;
double hypot = Math.hypot(dx, dy); // izracun hipotenuze
double cos = dx / hypot; // cos
double sin = dy / hypot; // sin
double rcos = r * cos;
double rsin = r * sin;
double rdx = Math.abs(dx) < Math.abs(rcos) ? dx : rcos; // if,else
double rdy = Math.abs(dy) < Math.abs(rsin) ? dy : rsin;
return new Point((int)rdx, (int)rdy);
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
final float x = motionEvent.getRawX(); // x točko
final float y = motionEvent.getRawY(); // y točka
//Log.d("VALUES", "RAW X:" + motionEvent.getRawX() + ", RAW Y:" + motionEvent.getRawY() + ", X:" + motionEvent.getX() + ", CX:" + cx + ", CY:" + cy + ", dx:" + dx + ", dy:" + dy + ", Hypo:" + hypot + ", cos:" + cos + ", sin" + sin);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
xDelta = view.getX() - x;
yDelta = view.getY() - y;
break;
case MotionEvent.ACTION_UP:
doBounceAnimation(buttonImageView);
doVibration();
view.setX(xx);
view.setY(yy);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
final float transX = (float) x;
final float transY = (float) y;
thread = new Thread() {
#Override
public void run() {
Point newPoint = calculate(transX+ xDelta,transY + yDelta);
buttonImageView.setX(newPoint.x);
buttonImageView.setY(newPoint.y);
}
};
thread.start();
Log.d(TRANSLATIONX,"X:" + transX + ", Y:" + transY);
break;
}
return true;
}
Enjoy coding!
I want to detect if the user clicks a llama and increase the money but it won't work(nothing happens if I click the (moving) llama). I didn't find a solution for this in the internet.
I added this in render():
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
if(lama.lamarect.contains(Gdx.input.getX(), Gdx.input.getX())){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + Gdx.input.getX() + " Y touched: " + Gdx.input.getY());
}
}
}
EDIT:
#Override
public void create() {
spawnLama();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/lamination.pack"));
animation = new Animation(1/15f, textureAtlas.getRegions());
camera = new OrthographicCamera(1280, 720);
...
private void spawnLama() {
Lama lama = new Lama();
lama.x = MathUtils.random(-1000, -200);
lama.y = MathUtils.random(-350, 100);
lama.speedx = MathUtils.random(-5, 5);
if(lama.speedx >= -1 && lama.speedx <=1){
lama.speedx = 2;
}
lama.speedy = MathUtils.random(-5, 5);
if(lama.speedy >= -1 && lama.speedy <=1){
lama.speedy = 2;
}
Rectangle livinglama = new Rectangle();
livinglama.x = lama.x;
livinglama.y = lama.y;
livinglama.width = 64;
livinglama.height = 64;
lama.lamarect = livinglama;
lamas.add(lama);
lastLamaTime = TimeUtils.nanoTime();
}
#Override
public void render() {
Gdx.gl.glClearColor(110 / 255F, 211 / 255F, 43 / 255F, 1 / 255F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
elapsedTime += Gdx.graphics.getDeltaTime();
if(spawnlama == true){
spawnLama();
spawnlama = false;
}
for (Lama lama : lamas) {
if(lama.x <= -1000){
lama.speedx = -lama.speedx;
}
else if(lama.x >= -200){
lama.speedx = -lama.speedx;
}
if(lama.y <= -350){
lama.speedy = -lama.speedy;
} else if(lama.y >=100){
lama.speedy = -lama.speedy;
}
if(lama.x == -500){
if(lama.speedx < 0){
lama.speedx --;
}
if(lama.speedx >= 0){
lama.speedx++;
}
}
lama.move();
}
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
batch.begin();
for (Lama lama : lamas) {
TextureRegion currentFrame;
currentFrame = animation.getKeyFrame(elapsedTime, true);
if(!currentFrame.isFlipX()) {
if (lama.speedx >= 0) {
currentFrame.flip(true, false);
}
}else{
if (lama.speedx < 0) {
currentFrame.flip(true, false);
}
}
batch.draw(currentFrame, lama.x, lama.y, currentFrame
.getRegionWidth(), currentFrame.getRegionHeight());
}
elapsedTime += Gdx.graphics.getDeltaTime();
Lama class:
public class Lama {
public int x, y, speedx, speedy;
public Rectangle lamarect;
// float delay = 1; // seconds
void move() {
x += speedx;
y += speedy;
lamarect.x = x;
lamarect.y = y;
}
}
Gdx.input.getX and Gdx.input.getY give screen coordinates and these need to be converted back to world coordinates. If you are using a camera you can use camera.unproject(touchVector) if you are working with a stage without a specified camera you can get the camera by stage.getCamera().
If you do not have either then you have to convert it back yourself, I believe you have to flip the y coordinate first then add them to the bottom left point you are looking at in your game world. But you make your life easier by implementing a camera.
if(Gdx.input.justTouched()){
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
for (Lama lama : lamas) {
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
Other then that you are passing Gdx.input.getX() two times in the contains method.
I am writing an Android game right now and I would need some help in the collision of the wall on screen. When I drag the ball in the top and right it able to collide in wall but when I drag it faster it was able to overlap in the wall
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
// if the player moves
case MotionEvent.ACTION_MOVE: {
if (playerTouchRect.contains(x, y)) {
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
boolean canMove = false;
boolean foundFinish = false;
if (x != pLastXPos) {
if (x < pLastXPos) {
left = true;
} else {
right = true;
}
pLastXPos = x;
}
if (y != pLastYPos) {
if (y < pLastYPos) {
up = true;
} else {
down = true;
}
pLastYPos = y;
}
plCellRect = getRectFromPos(x, y);
newplRect.set(playerRect);
newplRect.left = x - (int) (playerRect.width() / 2);
newplRect.right = x + (int) (playerRect.width() / 2);
newplRect.top = y - (int) (playerRect.height() / 2);
newplRect.bottom = y + (int) (playerRect.height() / 2);
int currentRow = 0;
int currentCol = 0;
currentRow = getRowFromYPos(newplRect.top);
currentCol = getColFromXPos(newplRect.right);
if(!canMove){
canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] == Cell.wall;
canMove =true;
}
finishTest = mapManager.getCurrentTile().pMaze[currentRow][currentCol];
foundA = finishTest == Cell.valueOf(letterNotGet + "");
canMove = mapManager.getCurrentTile().pMaze[currentRow][currentCol] != Cell.wall;
canMove = (finishTest == Cell.floor || finishTest == Cell.pl) && canMove;
if (canMove) {
invalidate();
setTitle();
}
if (foundA) {
mapManager.getCurrentTile().pMaze[currentRow][currentCol] = Cell.floor;
// finishTest
letterGotten.add(letterNotGet);
playCurrentLetter();
/*sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);*/
foundS = letterNotGet == 's';
letterNotGet++;
}if(foundS){
AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
builder.setTitle(mainActivity.getText(R.string.finished_title));
LayoutInflater inflater = mainActivity.getLayoutInflater();
View view = inflater.inflate(R.layout.finish, null);
builder.setView(view);
View closeButton =view.findViewById(R.id.closeGame);
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View clicked) {
if(clicked.getId() == R.id.closeGame) {
mainActivity.finish();
}
}
});
AlertDialog finishDialog = builder.create();
finishDialog.show();
}
else {
Log.d(TAG, "INFO: updated player position");
playerRect.set(newplRect);
setTouchZone();
updatePlayerCell();
}
} // end of (CASE) if playerTouch
break;
} // end of (SWITCH) Case motion
}//end of Switch
return true;
}//end of TouchEvent
private void finish() {
// TODO Auto-generated method stub
}
public int getColFromXPos(int xPos) {
val = xPos / (pvWidth / mapManager.getCurrentTile().pCols);
if (val == mapManager.getCurrentTile().pCols) {
val = mapManager.getCurrentTile().pCols - 1;
}
return val;
}
/**
* Given a y pixel position, return the row of the cell it is in This is
* used when determining the type of adjacent Cells.
*
* #param yPos
* y position in pixels
* #return The cell this position is in
*/
public int getRowFromYPos(int yPos) {
val = yPos / (pvHeight / mapManager.getCurrentTile().pRows);
if (val == mapManager.getCurrentTile().pRows) {
val = mapManager.getCurrentTile().pRows - 1;
}
return val;
}
/**
* When preserving the position we need to know which cell the player is in,
* so calculate it from the centre on its Rect
*/
public void updatePlayerCell() {
plCell.x = (playerRect.left + (playerRect.width() / 2))
/ (pvWidth / mapManager.getCurrentTile().pCols);
plCell.y = (playerRect.top + (playerRect.height() / 2))
/ (pvHeight / mapManager.getCurrentTile().pRows);
if (mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] == Cell.floor) {
for (int row = 0; row < mapManager.getCurrentTile().pRows; row++) {
for (int col = 0; col < mapManager.getCurrentTile().pCols; col++) {
if (mapManager.getCurrentTile().pMaze[row][col] == Cell.pl) {
mapManager.getCurrentTile().pMaze[row][col] = Cell.floor;
break;
}
}
}
mapManager.getCurrentTile().pMaze[plCell.y][plCell.x] = Cell.pl;
}
}
public Rect getRectFromPos(int x, int y) {
calcCell.left = ((x / cellWidth) + 0) * cellWidth;
calcCell.right = calcCell.left + cellWidth;
calcCell.top = ((y / cellHeight) + 0) * cellHeight;
calcCell.bottom = calcCell.top + cellHeight;
Log.d(TAG, "Rect: " + calcCell + " Player: " + playerRect);
return calcCell;
}
public void setPlayerRect(Rect newplRect) {
playerRect.set(newplRect);
}
private void setTouchZone() {
playerTouchRect.set(
playerRect.left - playerRect.width() / TOUCH_ZONE,
playerRect.top - playerRect.height() / TOUCH_ZONE,
playerRect.right + playerRect.width() / TOUCH_ZONE,
playerRect.bottom + playerRect.height() / TOUCH_ZONE);
}
public Rect getPlayerRect() {
return playerRect;
}
public Point getPlayerCell() {
return plCell;
}
public void setPlayerCell(Point cell) {
plCell = cell;
}
}*
This is an architectural problem. You can read more about it here.
In essence, your physics simulation (as simple as might be) is coupled to your framerate (which is capped at 60fps). Any events occurring faster than 60Hz cannot be processed accurately hence your bug.
The solution is to run the collision detection on an independent thread and draw the state it calculates at 60fps.
Also, take a look at these gamedev questions that refer to the same article.