Java 2D游戏:不能正常碰撞

时间:2017-01-16 17:31:38

标签: java 2d collision-detection

好的,所以我正在开发一个Java2D游戏,而且我已经接近了正确的碰撞。我有一个玩家类,一个块类,以及玩家的xvel,yvel等。 y碰撞工作正常,但是当我试图让X碰撞工作时,它就完全搞砸了。此外,即使Y碰撞工作正常,玩家也只是在街区上下轻微地上下摆动。 我为我的播放器设置了一个碰撞盒。我究竟做错了什么? 这是我的代码(我知道它不是最好的):

修改 好吧,所以我设法阻止玩家上下摆动,我改变了碰撞的检查方式。我删除了播放器的hitbox,我只是使用了.png精灵,我调整了它的大小以便运行良好。所以,问题是,即使播放器没有遇到块,“if(player.xvel< 0){}”和“if(player.xvel> 0){}”仍在执行水平。如何在播放器水平运行/走进块中时执行代码?

Block.java:

import java.awt.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.Rectangle;
class Block {
    Image block_sprite;
    Image dirt;
    int x;
    int y;
    int height;
    int width;
    int i = 0;
    public Block(int X, int Y, int Width, int Height, int block_type) {
        x = X * 32;
        y = Y * 32;
        width = Width;
        height = Height;
        try {
            dirt = ImageIO.read(new File("img/dirt.png"));
        } catch(IOException e) {
            e.printStackTrace();
        }
        if (block_type == 1) {
            block_sprite = dirt;
        }
    }

    void draw(Graphics g) {
        g.fillRect(x + Display.graphics_panel.WorldX, y, width, height);
    }
    void collide(Player player) {  
        Rectangle player_hb = new Rectangle((int) player.x, (int) player.y, (int) player.width, (int) player.height);
        Rectangle block_hb = new Rectangle(x + Display.graphics_panel.WorldX, y, width, height);

        if (player_hb.intersects(block_hb)) {
            Rectangle intersection = (Rectangle) player_hb.createIntersection(block_hb);  

            if (player.yvel > 0) {
                player.jumping = false;
                player.y -= intersection.getHeight();
            }
            if (player.yvel < 0) {
                player.y += intersection.getHeight(); 
            }
            if (player.xvel > 0) {
                System.out.println("You shouldn't see this unless you run into a block!" + i++);
            }
            if (player.xvel < 0) {
                System.out.println("You shouldn't see this unless you run into a block!" + i--);
            }
        }
    }   
}

Player.java:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.imageio.*;
import java.io.*;
class Player extends Entity {

    float x = Display.WINDOW_WIDTH / 2;
    float y = 430;
    float width = 48;
    float height = 103;

    float xvel = 0;
    float yvel = 0;
    float gravity = 0.4f;

    float xvel_max = 2;
    float yvel_max = 8;

    float xvel_min = -2;
    float yvel_min = -8;

    String dir = "left";

    float sword_offsetX = 49;
    float sword_offsetY = 32;

    Boolean jumping = false; 

    Image sprite;
    Image sword_sprite;
    Image sprite_r;
    Image sprite_r_a1;
    Image sprite_r_a2;

    Image sprite_l;
    Image sprite_l_a1;
    Image sprite_l_a2;

    Image ssprite_r;
    Image ssprite_l;
    Player() {
        try {
            sprite_r = ImageIO.read(new File("img/player_r.png"));
            sprite_r_a1 = ImageIO.read(new File("img/player_r_a1.png"));
            sprite_r_a2 = ImageIO.read(new File("img/player_r_a2.png"));

            sprite_l = ImageIO.read(new File("img/player_l.png"));
            sprite_l_a1 = ImageIO.read(new File("img/player_l_a1.png"));
            sprite_l_a2 = ImageIO.read(new File("img/player_l_a2.png"));
        ssprite_r = ImageIO.read(new File("img/sword_r.png"));

            ssprite_l = ImageIO.read(new File("img/sword_l.png"));
            if (dir == "left") {
                sprite = sprite_r;
            }
            sword_sprite = ssprite_r;
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    Graphics2D g2d;
    void draw(Graphics g) {
        g2d = (Graphics2D) g;

        g2d.drawImage(sprite, (int) x, (int) y, null);
        g2d.drawImage(sword_sprite, (int) x + (int) sword_offsetX, (int) y + (int) sword_offsetY, null);
    }

    void update() {

        jumping = true;
        yvel += gravity;
        Display.graphics_panel.WorldX += xvel;  
        y += yvel;

        for (int i = 0; i < Display.graphics_panel.world.blocks.length; i++) {
            Display.graphics_panel.world.blocks[i].collide(this);
        }

        if (Display.keys[65] == 65 || Display.keys[68] == 68) {
                if (dir == "left") {
                    sprite = sprite_r;
                    sword_sprite = ssprite_r;
                    sword_offsetX = 49;
                } else if (dir == "right") {
                    sprite = sprite_l;
                    sword_sprite = ssprite_l;
                    sword_offsetX = -18;
                }
                if (xvel < 0) {
                    if (Display.graphics_panel.frameCount / 10 % 2 == 0) {
                        sprite = sprite_r_a1;
                    } else {
                        sprite = sprite_r_a2;
                    }
                }
                if (xvel > 0) {
                    if (Display.graphics_panel.frameCount / 10 % 2 == 0) {
                        sprite = sprite_l_a1;
                    } else {
                        sprite = sprite_l_a2;
                    }
                }
        }
        if (xvel == 0 && dir == "left") {
            sprite = sprite_r;
        }
        if (xvel == 0 && dir == "right") {
            sprite = sprite_l;
        }
        if (Display.keys[65] == 65) {
            xvel += 0.25;
            dir = "right";
        } else if (Display.keys[68] == 68) {

            xvel -= 0.25;
            dir = "left";
        } else {
            xvel = 0;
        }

    if (Display.keys[87] == 87 && jumping == false) {
        jumping = true;
        yvel = -8;
    }
        if (xvel > xvel_max) {
            xvel = xvel_max;
        } else if (xvel < xvel_min) {
            xvel = xvel_min;
        }
        if (yvel > yvel_max) {
            yvel = yvel_max;
        } else if (yvel < yvel_min) {
            yvel = yvel_min;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

所以,我实际上是用hitbox制作了一个小平台游戏。您可能需要的部分是在碰撞发生之前了解碰撞。 可能我的游戏中的这个例子可以帮助你。

         level.material=array of points from the level
         w=width of hitbox
         h=height of hitbox
         x=upper left x
         y=upper left y
         onground=false;
         double maxy=1000;

         for (Point p:level.material) {
              if (p.x >= (int)x && p.x <= (int)x+w) { //Is the player over the point ? (is the point between player x and player x+w)
                      if (p.y >= (int)(y+h) && (int)(y+h+yvel) >= p.y && p.y-h-1 < maxy) { //Would he go through a platform if the yvel would be added ? And is this the earliest point where this happens ?
                          maxy=p.y-p2.y-1d; //Set new earliest point
                          onground=true; //He is onground
                      }
                  }
              }
         }

         if (onground==true) {
             y=maxy; //Dont fall through the floor ! Stop at the earliest point !
             yvel=0; //There isnt any velocity downwards anymore.
             xvel=xvel-xvel/4; //Friction
         }

您应该可以轻松地将其复制到所有其他路线(左,右,下)。这是&#34; onground&#34;(over)。所以玩家可以站在平台上。

答案 1 :(得分:0)

如果您只想检查rects之间的碰撞,这里是一个矩形类:

class Rect {
     public Point[] points=new Point[4];
     public Point start;
     public Point dim;
     public Rect(double x,double y,double w,double h) {
         start=new Point((int)x,(int)y);
         dim=new Point((int)w,(int)h);
         points[0]=new Point((int)x,(int)y);
         points[1]=new Point((int)x+(int)w,(int)y);
         points[2]=new Point((int)x+(int)w,(int)y+(int)h);
         points[3]=new Point((int)y,(int)y+(int)h);
     }
     public boolean collidepoint(Point p) {
         if (p.x >= start.x && p.x <= start.x+dim.x && p.y >= start.y && p.y <= start.y+dim.y) {
             return true;
         }
         else {
             return false;
         }
     }
     public boolean colliderect(Rect rect2) {
         boolean retvar=false;
         for (Point p:rect2.points) {
              if (collidepoint(p)==true) {
                  retvar=true;
              }
         }
         if (retvar==false) {
         for (Point p:points) {
              if (rect2.collidepoint(p)==true) {
                  retvar=true;
              }
         }
         }
         if (retvar==false) {
         if (rect2.start.y >= start.y && rect2.start.y <= start.y+dim.y || rect2.start.y+rect2.dim.y >= start.y && rect2.start.y+rect2.dim.y <= start.y+dim.y) {
             if (rect2.start.x >= start.x && rect2.start.x <= start.x+dim.x || rect2.start.x+rect2.dim.x >= start.x && rect2.start.x+rect2.dim.x <= start.x+dim.x) {
                 retvar=true;
             }
         }
         }
         if (retvar==false) {
         if (start.y >= rect2.start.y && start.y <= rect2.start.y+rect2.dim.y || start.y+dim.y >= rect2.start.y && start.y+dim.y <= rect2.start.y+rect2.dim.y) {
             if (start.x >= rect2.start.x && start.x <= rect2.start.x+rect2.dim.x || start.x+dim.x >= rect2.start.x && start.x+dim.x <= rect2.start.x+rect2.dim.x) {
                 retvar=true;
             }
         }
         }
         return retvar;
     }
}

希望它对你有所帮助。