Collision detection and prevent player movement

Greedings :slight_smile: its been a while and i’ve been working on Java programming again and i had some questions that i can’t really make out how do it. So lets begin :

Here is the GetInput() code where you check the keys and depending on what you press you set the yMove or xMove equal to speed so the player will move. Although if i set the yMove and xMove to 0 where there is collision then it never moves again. Any solution?

	public void getInput() {
		xMove = 0;
		yMove = 0;

		if (game.getKeys().up) {
			yMove = -speed;
		}
		if (game.getKeys().down) {

			yMove = speed;
		}
		if (game.getKeys().left) {

			xMove = -speed;
		}
		if (game.getKeys().right) {

			xMove = speed;
		}

		if (x < 0) {
			x = 0;
		}
		if (x > 250) {
			x = 250;
		}
		if (y < 0) {
			y = 0;
		}
		if (y > 250) {
			y = 250;
		}

		rectup.setBounds((int) x, (int) y, (int) width, (int) height);
		rectdown.setBounds((int) x, (int) y, (int) width, (int) height);
		rectleft.setBounds((int) x, (int) y, (int) width, (int) height);
		rectright.setBounds((int) x, (int) y, (int) width, (int) height);
		collision();
	}

Here is the Collision Class where i check for the distance of the player and the enemies. The collision works perfectly but i don’t know how to tell the Player Entity to stop moving.

public void collision() {
		if (isColliding()) {
			lastX = getX();
			LastY = getY();
		}
		if (enemies.size() != 0) {
			for (int i = 0; i < enemies.size(); i++) {
				double diffX = getX() - enemies.get(i).getX();
				double diffY = getY() - enemies.get(i).getY();
				double distanceToPlayer = Math.sqrt((diffX * diffX)
						+ (diffY * diffY));

				if (distanceToPlayer < 44) {
					x = lastX;
					y = LastY;

				}
			}
		}

	}

Thanks for any help :slight_smile:

Shouldn’t you be checking preemptively if you can move (are colliding) and then move if you are not?

Line 3 of the collision method is the problem I think, you are saving lastX/Y after you have moved, so if you have collided, you don’t change anything.

I would change the collision method to return a boolean, taking in the projected X/Y coords (where you want to move to). This changed method should be the called after getting the projected coords in the getInput method. An excerpt of the new getInput():


int projectedPositionX = x;
int projectedPositionY = y;

if (game.getKeys().up) {
    projectedPositionY -= speed;
}

if willCollide(projectedPositionX, projectedPositionY) return;

x = projectedPositionX;
y = projectedPositionY;
// set bounds




private boolean willCollide(int projectedPositionX, int projectedPositionY) {
    for (Enemy enemy : enemies) {
        double diffX = getX() - enemy.getX();
        double diffY = getY() - enemy.getY();
        double distanceToPlayer = Math.sqrt((diffX * diffX) + (diffY * diffY));
        if (distanceToPlayer < 44) {
            return true;
    }
    return false;
}


You could also put these checks:


if (x < 0) {
    x = 0;
}

into the relevant key check like so:


if (game.getKeys().left) {
    xMove = -speed;
    if (x < 0) {
        x = 0;
    }
}

Check for collision before the movement.
For example if you wanna go to the right:

if(place_free(x+spd,y)) //I can move

This way you can use only one rectangle to check for collisions

Are you from the GameMaker world? I remember that as a GML function.

Hahaha yeah When i wanna build some game faster i use gms otherwise java! :wink:

Actually your reply doesn’t help him anyways, he is not using GMS and not GML, he is using Java. What you need to actually reply is what the function is, and how you can implement that function in his game. That is what you should say when you want to help him.