【问题标题】:Creating simple collision detection创建简单的碰撞检测
【发布时间】:2015-03-14 03:50:27
【问题描述】:

我创建了一个非常简单的程序,其中我使用油漆绘制了一个迷宫(fillRect 方法用于创建迷宫的墙壁),并且我创建了一个使用keyListener 移动的精灵。我想实现一个简单的(因为我在通勤科学的第一年)碰撞检测,以防止精灵穿过迷宫的墙壁。因为迷宫是用将近 400 行代码绘制的,所以我不会包含它。

import java.awt.*;
import java.applet.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class IndeProj extends Applet implements KeyListener {


    //KEY LISTENER INFO FOR MY SPRITE
    public int x = 10;
    public int y = 575;
    public boolean keyUp;  
    public boolean keyDown;  
    public boolean keyLeft;  
    public boolean keyRight;


    public void paint(Graphics g) {
        //drawMaze
        drawMazeHorizontalLines(g);
        drawMazeVerticalLines(g);

        //SPRITE STUFF
        addKeyListener(this);
        this.MoveRect(g,x,y);

    }

    public void drawMazeHorizontalLines(Graphics g)
    {
        //This method draws the horizontal lines of the maze using the method `fillRect(x,y,w,h)`
    }

    public void drawMazeVerticalLines (Graphics g)
    {
        //This method draws the vertical lines of the maze using `fillRect(x,y,w,h)`
    }


    public void MoveRect(Graphics g, int x, int y) //Draws Sprite   
    {
        g.setColor(Color.green);
        g.fillRect(x,y,20,20);
        g.setColor(Color.yellow); //Sprite body
        g.fillRect(x,y,20,20);
        g.setColor(Color.green); //Sprite eyes
        g.fillRect(x,y,7,7);
        g.fillRect((x+13),y,7,7);
        g.setColor(Color.blue); //Sprite pants
        g.fillRect(x,(y+13),20,7);
        g.setColor(Color.black); //Sprite mouth
        g.fillRect((x+6),(y+9),8,2);
    }

    public void keyPressed(KeyEvent e) //Moves Sprite
    {
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            y+=1;
            y+=0;
        } if (e.getKeyCode() == KeyEvent.VK_UP) {
            y-=1;
            y-=0;
        } if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            x-=1;
            x-=0;
        } if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            x+=1;
            x+=0;
        }
        repaint();
    }
    public void keyReleased(KeyEvent e) //Stops Sprite  
    {   
         keyUp = keyDown = keyLeft = keyRight = false;
    }
}

我想让精灵在撞到墙上时停止(使用xy 坐标),精灵将停止移动。

【问题讨论】:

    标签: java collision-detection


    【解决方案1】:

    这是一种简单的碰撞检测方法,地图是一个整数网格,每个整数都有 它包含的墙。 如果你做错了,你会得到神奇的单向墙。

    /* could use enums for this */
    public static int WALL_LEFT = 1;
    public static int WALL_RIGHT = 2;
    public static int WALL_TOP = 4;
    public static int WALL_BOTTOM = 8;
    
    
    public int[][] createSimpleMap(){
        int[][] map = new int[2][2];
        map[0][0] = WALL_LEFT | WALL_RIGHT | WALL_TOP;
        map[0][1] = WALL_LEFT | WALL_RIGHT | WALL_TOP;
        map[1][0] = WALL_LEFT | WALL_BOTTOM;
        map[1][1] = WALL_RIGHT | WALL_BOTTOM;
        return map;
    }
    

    要进行碰撞检测,只需检测墙壁是否存在。

    public boolean canMoveUp(x,y){
        return (this.map[x][y] & WALL_TOP) ==0;
    }
    

    【讨论】:

      【解决方案2】:

      如果您的小程序是双缓冲的,那么您可以在它们移动之前执行以下操作:

      Color c = new Color(buffer.getRGB(desiredPlayerX, desiredPlayerY));
      if(c.equals(<Whatever color you used for the maze walls>)){
          // Don't allow the movement of the player
      }else{
          x = desiredPlayerX;
          y = desiredPlayerY;
      }
      

      虽然这种方法看起来有点“hacky”,但我确信有更好的方法来实现它,但这是一个可能的快速解决方案。

      【讨论】:

        【解决方案3】:

        我这样做的方法是使用矩形碰撞盒。 playerX 和 playerY 是玩家的坐标:

        while(true) {
            while(!((playerX > 50 && playerX < 100) && (playerY > 50 && playerY < 100))){
                //Put code to let they players walk here, and boundaries will be enforced.
            }
        }
        

        那将是一个矩形框,你不能进入它的边界内。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-26
          • 1970-01-01
          相关资源
          最近更新 更多