简体   繁体   中英

Collision Detection

I am new to Java, and I have basic programming knowledge. I have recently started developing a game(with the help of tutorials). The game is an RPG. It is set so that the player's avatar is constantly in the center of the screen, but the background scrolls. I know this has been asked before, but none of the answers make sense to me. Basically, how do I create collision detection for multiple objects? I want it to be so that my player stops moving when he hits a wall. I currently have collision for the boundaries set, but how do I set collision for houses, walls, etc.? A copy of my code is below. Thank you in advance. Also, I am using Slick2D.

package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;
//Need "extends etc." for creating basic screen/state
public class Play extends BasicGameState{

public Rectangle lake1;

Animation avatar, movingUp, movingDown, movingLeft,  movingRight;
Image worldMap;
boolean quit = false;
int[] duration = {200,200};
float avatarPositionX = 0;
float avatarPositionY = 0;
float shiftX = avatarPositionX + 250;
float shiftY = avatarPositionY + 180;

public Play(int state){
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{   
    worldMap = new Image("res/WaterMap.png");
    Image[] walkUp = {new Image("res/CharBack.png"), new 
Image("res/CharBack.png")};
    Image[] walkDown = {new Image("res/CharFront.png"), new 
Image("res/CharFront.png")};
    Image[] walkLeft = {new Image("res/CharLeft.png"), new  
Image("res/CharLeft.png")};
    Image[] walkRight = {new Image("res/CharRight.png"), new
Image("res/CharRight.png")};

    movingUp = new Animation(walkUp, duration, false);
    movingDown = new Animation(walkDown, duration, false);
    movingLeft = new Animation(walkLeft, duration, false);
    movingRight = new Animation(walkRight, duration, false);
    avatar = movingDown;
}
//Takes gc and StateBasedGame and Graphics
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws    
SlickException{
    lake1 = new Rectangle(132, 277, 87, 54);
    worldMap.draw(avatarPositionX, avatarPositionY);
    avatar.draw(shiftX, shiftY);
    g.drawString("Your X: "+avatarPositionX+"\nYour Y: "+avatarPositionY, 350,  
20);

    if(quit==true){
        g.drawString("Resume (R)", 200, 100);
        g.drawString("Main Menu (M)", 200, 150);
        g.drawString("Quit (Q)", 200, 200);
        if(quit==false){
            g.clear();
        }
    }
}
//Updates images on screen; creates animation
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws   
SlickException{
    Input input = gc.getInput();
    //up
    if(input.isKeyDown(Input.KEY_UP)){
        avatar = movingUp;
        avatarPositionY += delta * .1f;
        //+= changed to -=
        if(avatarPositionY>180){
            avatarPositionY -= delta * .1f;
        }
    }
    //down
    if(input.isKeyDown(Input.KEY_DOWN)){
        avatar = movingDown;
        avatarPositionY -= delta * .1f;
        if(avatarPositionY<-497){
            avatarPositionY += delta * .1f;
        }
    }
    //left
    if(input.isKeyDown(Input.KEY_LEFT)){
        avatar = movingLeft;
        avatarPositionX += delta * .1f;
        if(avatarPositionX>249){
            avatarPositionX -= delta * .1f;
        }
    }
    //right
    if(input.isKeyDown(Input.KEY_RIGHT)){
        avatar = movingRight;
        avatarPositionX -= delta * .1f;
        if(avatarPositionX<-433){
            avatarPositionX += delta * .1f;
        }
    }
    //escape
    if(input.isKeyDown(Input.KEY_ESCAPE)){
        quit = true;
    }
    //when menu is up
    if(quit == true){
        if(input.isKeyDown(Input.KEY_R)){
            quit = false;
        }
        if(input.isKeyDown(Input.KEY_M)){
            sbg.enterState(0);
        }
        if(input.isKeyDown(Input.KEY_Q)){
            System.exit(0);
        }   
    }
}

public int getID(){
    return 1;
}
}

Before you actually move the character, check if the next position is 'empty' and inside the boundaries (how you actually implement this depends on how you implement your houses and walls) and only move the character if it is.

For the implementation you probably want to iterate over every 'object' and check if the new position of the character is inside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM