ChrisixStudios
ChrisixStudios

Reputation: 39

What is a simple way to stop a Entity when it collides into another?

I have two Entities, a Player and a Block. The player and the block are both squares. I want the player to stop moving when it runs into a block. I have some of the code done but it only work's for horizontal and I was wondering using my existing code if I can add a vertical collision part to it.

I need horizontal and vertical collisions because I need to know which way the player is hitting the block and on what side. This will help me determine if the X or Y velocity should be stopped when it is colliding into the block.

I have already done a lot of research on this topic but nothing really to simple and wasn't explained to well with the Slick2d's Rectangle object. If you have used Slick;s Shape classes you already know that it has a .intersect(Shape s) method. I already use this in my code to see if checking which side it is colliding into and stopping the player is necessary.

Here is the classes I think would be relative to see (If you need another class just ask).

Entity:

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.StateBasedGame;

public abstract class Entity {

    private Shape shape;
    private float velX = 0f, velY = 0f;

    public Entity(Shape shape){
        this.shape = shape;
    }

    //Get Methods
    public Shape getShape(){ return shape; }
    public float getVelocityX(){ return velX; }
    public float getVelocityY(){ return velY; }

    //Set Methods
    public void setVelocityX(float velX){ this.velX = velX; }
    public void setVelocityY(float velY){ this.velY = velY; }

    protected abstract void onUpdate(GameContainer gc, StateBasedGame sbg, int d) throws SlickException;
    protected abstract void onRender(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException;

    public void update(GameContainer gc, StateBasedGame sbg, int d) throws SlickException {
        //Use velocity to change x and y.
        this.shape.setX(shape.getX() + (velX * (float)d));
        this.shape.setY(shape.getY() + (velY * (float)d));

        onUpdate(gc, sbg, d);
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        onRender(gc, sbg, g);
    }

}

WorldEntity:

import org.newdawn.slick.geom.Shape;

public abstract class WorldEntity extends Entity{

    private World world;

    public WorldEntity(World world, Shape shape) {
        super(shape);
        this.world = world;
    }

    //Get Methods
    public World getWorld(){ return world; }

    public abstract boolean collidedWithEntity(Entity e);
    public abstract void remove();

}

Block:

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

public class Block extends WorldEntity{

    private BlockType type;

    public Block(World world, float x, float y, BlockType type) {
        super(world, new Rectangle(x, y, 48, 48));
        this.type = type;
    }

    //Get Methods
    public Color getColor(){ return type.getColor(); }
    public Image getTexture(){ return type.getTexture(); }
    public boolean isSolid(){ return type.isSolid(); }
    public BlockType getType(){ return type; }

    @Override
    public boolean collidedWithEntity(Entity e) {
        return false;
    }

    @Override
    protected void onUpdate(GameContainer gc, StateBasedGame sbg, int d) throws SlickException {

    }

    @Override
    protected void onRender(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        if(type.getTexture() != null){
            if(type.getColor() != null){
                g.drawImage(type.getTexture(), getShape().getX(), getShape().getX(), type.getColor());
            }
            else{
                g.drawImage(type.getTexture(), getShape().getX(), getShape().getX());
            }
        }
        else{
            g.setColor(type.getColor());
            g.fill(getShape());
        }
    }

    @Override
    public void remove() {
        getWorld().getBlocks().remove(this);
    }

}

Player:

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.StateBasedGame;

import us.chrisix.coloroid.world.Block;
import us.chrisix.coloroid.world.Entity;
import us.chrisix.coloroid.world.World;
import us.chrisix.coloroid.world.WorldEntity;

public class Player extends WorldEntity{

    private Color c;
    private float speed = 0.3f;

    public Player(World world, float x, float y, Color c) {
        super(world, new Rectangle(x, y, 48, 48));
        this.c = c;
    }

    public Color getColor(){ return c; }

    @Override
    public boolean collidedWithEntity(Entity e) {
        if(e instanceof Block && e.getShape().intersects(getShape()) && ((Block) e).isSolid()){
            Shape b = e.getShape();
            Shape p = getShape();
            if(b.getX() > p.getX()){
                p.setX(b.getX() - 48.1f);
                setVelocityX(0);
            }
            else if(b.getX() < p.getX()){
                p.setX(b.getX() + 48.1f);
                setVelocityX(0);
            }
            return true;
        }
        return false;
    }

    @Override
    public void remove() {

    }

    @Override
    protected void onUpdate(GameContainer gc, StateBasedGame sbg, int d) throws SlickException {
        Input in = gc.getInput();
        if(in.isKeyDown(Input.KEY_D)){
            this.setVelocityX(speed);
        }
        else if(in.isKeyDown(Input.KEY_A)){
                this.setVelocityX(-speed);
        }
        else{
            this.setVelocityX(0);
        }

        if(in.isKeyDown(Input.KEY_W)){
            this.setVelocityY(-speed);
        }
        else if(in.isKeyDown(Input.KEY_S)){
            this.setVelocityY(speed);
        }
        else{
            this.setVelocityY(0);
        }
    }

    @Override
    protected void onRender(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setColor(c);
        g.fill(getShape());
        g.setColor(Color.black);
        g.draw(getShape());
        g.draw(new Rectangle(getShape().getX() + 1, getShape().getY() + 1, 46, 46));
    }

}

The part of code I hoping to just be imporved is the Player.collidedWithEntity(Entity e).

If you have any questions on how I worded things or on my code, I encourage you to ask for clarification. Thank you for taking the time to read this.

Upvotes: 0

Views: 810

Answers (1)

ChrisixStudios
ChrisixStudios

Reputation: 39

I have solved the problem! I solved it by splitting the X and Y. Here is it broken down.

  • Save both the X and Y coordinates.
  • Move the X depending on the velocity.
  • If the Entity collides into a solid object it undoes the X movement.
  • Move the Y depending on the velocity.
  • If the Entity collides into a solid object it undoes the Y movement.

So now I have it colliding and sliding against the walls! Thanks for people who gave me the articles to read :)

Upvotes: 1

Related Questions