0

プレイヤーとブロックの 2 つのエンティティがあります。プレイヤーもブロックも正方形です。ブロックにぶつかったら動きを止めてほしい。いくつかのコードを完成させましたが、それは水平に対してのみ機能し、既存のコードを使用して垂直衝突部分を追加できるかどうか疑問に思っていました。

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).

私の言葉遣いやコードについて質問がある場合は、説明を求めることをお勧めします。これを読んでいただきありがとうございます。

4

1 に答える 1

1

私は問題を解決しました!XとYを分割して解決しました。これが分解されたものです。

  • X座標とY座標の両方を保存します。
  • 速度に応じてXを移動します。
  • エンティティがソリッドオブジェクトに衝突すると、Xの動きが元に戻ります。
  • 速度に応じてYを移動します。
  • エンティティがソリッドオブジェクトに衝突すると、Yの動きが元に戻ります。

だから今、私はそれを壁にぶつけて滑らせています!読んでくれる記事をくれた人に感謝します:)

于 2013-03-03T19:56:30.987 に答える